I have a Wicket Textfield which contains an Integer value
currentValueTextField = new TextField<IntParameter>("valueText", new PropertyModel<IntParameter>(model, "value"));
I'm attaching a custom validator to this, as follows
currentValueTextField.add(new IntegerValidator());
The validator class is
class IntegerValidator extends AbstractValidator<IntParameter> {
private static final long serialVersionUID = 5899174401360212883L;
public IntegerValidator() {
}
@Override
public void onValidate(IValidatable<IntParameter> validatable) {
ValidationError error = new ValidationError();
if (model.getValue() == null) {
AttributeAppender redOutline = new AttributeAppender("style", new Model<String>("border-style:solid; border-color:#f86b5c; border-width: 3px"), ";");
currentValueTextField.add(redOutline);
currentValueTextField.getParent().getParent().add(redOutline);
validatable.error(error);
}
}
}
However if I type nothing in the textfield, my onValidate()
method is not being called.
What is the recommended way to check for null values in this case? I would also like to do range checking on the value entered.
just call
currentValueTextField.setRequired(true);
to mark the field as required and have Wicket handle null values on it's own. You can easily combine multiple validators per input field.
Any special error handling, like adding red borders or displaying of error messages can be implemented in the onError
method of the form or by adding FeedbackBorder
s to the appropriate fields.
Override validateOnNullValue()
that is false
by default.
@Override
public boolean validateOnNullValue()
{
return true;
}
This is the description of validateOnNullValue()
method:
Indicates whether or not to validate the value if it is
null
. It is usually desirable to skip validation if the value isnull
, unless we want to make sure the value is in factnull
(a rare use case). Validators that extend this and wish to ensure the value isnull
should override this method and returntrue
.
currentValueTextField.setRequired(true);
Now you need to customise the error message. So subclass FeedbackPanel.
you can find more information in the following link
Add this class to your form or component
A better (and reusable) way to do this is to override the isEnabled(Component)
method of the behavior:
public class HomePage extends WebPage {
private Integer value;
public HomePage() {
add(new FeedbackPanel("feedback"));
add(new Form("form", new CompoundPropertyModel(this))
.add(new TextField("value")
.setRequired(true)
.add(new ErrorDecorationBehavior()))
.add(new Button("submit") {
@Override
public void onSubmit() {
info(value.toString());
}
}));
}
}
class ErrorDecorationBehavior extends AttributeAppender {
public ErrorDecorationBehavior() {
super("style", true, Model.of("border-style:solid; border-color:#f86b5c; border-width: 3px"), ",");
}
@Override
public boolean isEnabled(Component component) {
return super.isEnabled(component) && component.hasErrorMessage();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With