Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check for null values in a Wicket Textfield?

Tags:

java

wicket

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.

like image 419
Andrew Fielden Avatar asked Apr 25 '12 14:04

Andrew Fielden


4 Answers

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 FeedbackBorders to the appropriate fields.

like image 113
Nicktar Avatar answered Nov 19 '22 17:11

Nicktar


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 is null, unless we want to make sure the value is in fact null (a rare use case). Validators that extend this and wish to ensure the value is null should override this method and return true.

like image 25
jordeu Avatar answered Nov 19 '22 19:11

jordeu


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

like image 2
Rupa Evangeline Avatar answered Nov 19 '22 18:11

Rupa Evangeline


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();
    }
}
like image 1
tetsuo Avatar answered Nov 19 '22 18:11

tetsuo