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

4

5

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.

Redcoat answered 25/4, 2012 at 14:24 Comment(1)
I don't know about Wicket but in other web frameworks that I know the validation is not called for empty text fields. Some provide additional flags for their tags to enable/disable required checks while in others you might have to do the check when putting the form values into the model (or check the model afterwards).Suffer
M
3

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.

Ministration answered 25/4, 2012 at 14:44 Comment(3)
Overriding validateOnNullValue() does cause my validator to be called, but the interesting thing is that the value in my model is not set to null. So my validator code won't raise an error because it thinks the textfield has a non-null value.Redcoat
Mmm... But the null has to be here validatable.getValue(), when you pass throw the validator the value is still not set on the model.Ministration
Commenting to note that now you want to use IValidator directly for most cases, and INullAcceptingValidator for this case in Wicket 6.Sandler
U
4

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.

Universal answered 25/4, 2012 at 14:44 Comment(3)
Problem here is I have no control over how the error is indicated. I would like to add a red border to the textfield, but wicket will put an error message in a Feedback panel. I don't want to use a Feedback panel.Redcoat
@AndrewFielden This can be archived by implementing onError() for the form. I extended my answer to cover this.Universal
Thanks. The guys who wrote Wicket have thought of everything.Redcoat
M
3

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.

Ministration answered 25/4, 2012 at 14:44 Comment(3)
Overriding validateOnNullValue() does cause my validator to be called, but the interesting thing is that the value in my model is not set to null. So my validator code won't raise an error because it thinks the textfield has a non-null value.Redcoat
Mmm... But the null has to be here validatable.getValue(), when you pass throw the validator the value is still not set on the model.Ministration
Commenting to note that now you want to use IValidator directly for most cases, and INullAcceptingValidator for this case in Wicket 6.Sandler
M
2
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

Mic answered 26/4, 2013 at 8:44 Comment(0)
O
1

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();
    }
}
Oyler answered 25/4, 2012 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.