wicket 6 custom error message instead of The value of x is not a valid int - wicket

I am using Wicket 6, I want to add custom error message for a specific component (TextField).
This field should only accept number within specific value (0-59). This validation works perfectly. However, when the user enters a non-number... it seems to return a default validation error message. Something like The value of x is not a valid int.
Below is the sample code on how I set the validation/error message when the entered value is greater than 59 or less than zero:
...
form.add(new ReportErrorControlGroup("minutesLateGroup", new TextField("minutesLate",
new PropertyModel<String>(m_attendanceInfo, "minutesLate")).setRequired(false).add(new IValidator<Integer>() {
private static final long serialVersionUID = 1L;
#Override
public void validate(IValidatable<Integer> validatable) {
Integer input = validatable.getValue();
if (input < 0 || input >= 60) {
validatable.error(new ValidationError("Please enter a valid minute(s) value, a value between 0 to 59."));
}
}
})));
...
When the user entered a non-number value, it will return a validation/error message The value of 'minutesLate' is not a valid int.
What I want is to replace this error with something like Please enter valid minute value. and to be applied only to minutesLate.
In my other project, I can set this via messages.properties then something like field.error.incorrect.expected.values={0} is not a valid value.
But this project is Apache Wicket 6 and I have no idea on how to setup/configure messages.properties and use specific message to be displayed when validation fails.
I am thinking of using minutesLate.value.invalid=Please enter valid minute value.
How can I make it see the added messages.properties and use minutesLate.value.invalid for invalid (non-number) values.

The default messages are defined at https://github.com/apache/wicket/blob/c6fde82ce9637c43753b59620b2f5551ad7f20fe/wicket-core/src/main/java/org/apache/wicket/Application.properties#L16
So, you need to override it in MyApplication.properties (must be next to MyApplication.class)
minutesLate.IConverter=Please enter valid minute value.

Related

Codeeffect Rule Engine - How to define rules to check length of string

I have to define a rule like <> length is < 5 or >5 AND not equal to 'N/A'.
I am not able to get Len() function in the list of operators for the string data type.
Please suggest if any field level attributes needs to define.
In your source class define a method like this:
public int Length(string str)
{
return string.IsNullOrEmpty(str) ? 0 : str.Length;
}
Then run your project, open the rule editor and create your rule like this:
Check if YourString has any value and Length(YourString) is greater than [5]
Details on in-rule methods can be found here

How to get Label from IValidatable in Wicket 7

Is it possible to get the label of my component inside a validation? I need this label for a custom error message in my validation. It looks like:
"The value may not be less than {0}."
If my component has a label then i want to write it before like:
"LabelName: The value may no be less than {0}."
My component BigDecimalValidator doesn't know the BigDecimalTextfield.
tfiGV = new BigDecimalTextField("tfiGV", new Model<BigDecimal>());
tfiGV.setLabel(Model.of(Const_Labels.GV));
tfiGV.add(BigDecimalValidator.minimum(0));
The validatable of BigDecimalValidator can't reach the necessary label.
#Override
public void validate(IValidatable<BigDecimal> validatable) {
// Doesn't work ((FormComponent<BigDecimal>) validatable).getLabel();
if (((BigDecimal) validatable.getValue()).compareTo(BigDecimal.valueOf(minimum, 3)) == -1) {
ValidationError valError = new ValidationError();
valError.setMessage(getErrorMessageMin(minimum));
validatable.error(valError);
}
}
I know that the label can be reached by a constructor for BigDecimalValidator but this isn't a nice solution.
You can use {label} in your i18n message and Wicket will replace it with the form component's label.
E.g. {label}: The value may not be less than {0}.

Richfaces 4 dynamic select options when user type

I am using rich faces select component.
I want dynamic values when user manually type some thing in the select component.
<rich:select enableManualInput="true" defaultLabel="start typing for select" value="#{supplierSearchBean.userInput}">
<a4j:ajax event="keyup" execute="#this" listener="#{supplierSearchBean.userInputChange}"/>
<f:selectItems value="#{supplierSearchBean.selectOptions}" />
</rich:select>
Java code as follows
public void userInputChange(ActionEvent ae){
Map map = ae.getComponent().getAttributes();
System.out.println(map.toString());
}
public void setUserInput(String userInput) {
System.out.println("userINput = " + userInput);
this.userInput = userInput;
}
Here i found 2 issues
1st: setUserINput always print empty string when user type value
2nd: listener method never get call.
any help ?
The problem is most probably that there is no selected value while the user types, and this component restricts the allowed values to the specified select items. A partial input is thus not valid and cannot be bound to your bean.
I think you could get the expected behavior if you use a rich:autocomplete instead. However, if you want to restrict the allowed values, maybe you can keep your rich:select and listen for the selectitem event.
Override getItems function in richfaces-utils.js file in richfaces-core-impl-4.0.0.Final.jar under richfaces-core-impl-4.0.0.Final\META-INF\resources folder.
Change the condition of pushing items to be
if(p != -1)
instead of
if(p == 0)
This should fix the issue.

Grails Domain class property Set failed Validation

I have problem with a setter in grails. I have two properties beforeTax and afterTax. I only want to store one property in the db beforeTax. In the ui I want the user to enter either before or after tax. So I made the afterTax a transient property like this:
double getAfterTax(){
return beforeTax * tax
}
void setAfterTax(double value){
beforeTax = value / tax
}
When I now enter the after tax value and want to save the object the validation fails (before tax can not be an empty value)
What am I doing wrong?
If I understand your question correctly, you want to compute beforeTax based on the value of afterTax?
You could use the event handler methods beforeXXX() where XXX is Validate, Insert, and Update to compute beforeTax. Then beforeTax's constraint can be nullable:false.
def beforeValidate() {
computeBeforeTax()
}
You have to flag one property as transient, in order to prevent GORM from trying to save this variable into DB. Try to add this line into your domain class, which contains afterTax.
static transients = ['afterTax']

Passing string parameters MVC 3

I'm trying to pass a string parameter in MVC3.
The URL that gets generated is:
http://localhost:50164/Property/Browse/Oregon
This appears to be the Property controller class in the function "Browse" with parameter "Oregon".
The right function gets called, but the string appears to be empty.
public ViewResult Browse(string location)
{
//counted_properties: 3 counted_properties_here: 0
ViewData["counted_properties"] = db.Properties.Count(); // debug 3 total
ViewData["counted_properties_here"] = db.Properties.Where(p => p.location == "Oregon").Count(); // debug 1 (the right answer!)
//return View(db.Properties.Where(p => p.location == tmp_location).ToList()); // 0 (bad!)
ViewData["the_location"] = location;
int size = tmp_location.Length; // unhandled null exception when tmp_location = location
ViewData["location_length"] = size;
// return View(db.Properties.Where(p => p.location == "Oregon").ToList()); // Right! but hardcoded
return View(db.Properties.Where(p => p.location == location).ToList());
}
When I return the lambda using the hard coded string (Oregon), I get the right answer.
When I return the lambda using the parameter (string location) I get zero results.
When I try to display ViewData["the_location"] which is a direct copy of the parameter it is blank. Maybe I'm not trying to display it correctly. Here is my view:
Location: #Html.Encode(ViewData["the_location"] )
When I call location.Length I get an unhandled null exception, but this works when I call Length on a hard coded parameter like "Oregon".
It looks like somehow the string is empty, but this doesn't make sense since I see "Oregon" in the URL when it gets to the property/browse function.
I'm beginning to wonder if I am allowed to pass strings as arguments in MVC as I haven't seen any examples with string parameters.
This is a continuation of an unsolved problem:
C#/ASP.NET lambda conversion
By the way, I'm not trying to promote Oregon here. I've never been there. Maybe it's a good place, though.
Check your routing. Traditionally URLs like the one you give will map the value ("Oregon") to the "id" parameter, rather than the location parameter that you're trying to use. Either rename the parameter or change the route to use "{controller}/{action}/{location}".