Andrew Fielden
Andrew Fielden

Reputation: 3899

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

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.

Upvotes: 5

Views: 6322

Answers (4)

Rupa Evangeline
Rupa Evangeline

Reputation: 31

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

Upvotes: 2

Nicktar
Nicktar

Reputation: 5575

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.

Upvotes: 4

tetsuo
tetsuo

Reputation: 10896

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();
    }
}

Upvotes: 1

jordeu
jordeu

Reputation: 6821

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.

Upvotes: 3

Related Questions