Reputation: 2364
How do I get my validator to raise an error on a null value? I can see through my logs that when I try to submit a null field it's calling the validator, but it never raises onError so I cannot handle this. I want to display an error message in a feedback panel when someone tries to submit an empty field instead of having to do a check in onSubmit of my form.
@Override
protected void onValidate(IValidatable<String> validatable) {
String value = validatable.getValue();
if (value == null) {
ValidationError error = new ValidationError();
error.addMessageKey("messageKey");
validatable.error(error);
}
}
@Override
public void validateOnNullValue() {
return true;
}
Edit
If the above is not easily possible, is there a way that I can easily register an error message with a feedback panel when component.setRequired
is true and the form is submitted with an empty field? I just don't want there to be no feedback to the user when they try to submit the form with an empty field, but I also don't want to have logic for this in the onSubmit handler.
edit 2 SOme more context
Here are two of the fields I'm trying to validate:
final TextField<String> currentLpnField = new TextField<String>("currentLpn", Model.of(""));
currentLpnField.setOutputMarkupId(true);
currentLpnField.setRequired(true);
currentLpnField.add(new BarcodeLPNValidator());
final TextField<String> upcField = new TextField<String>( "upc", new Model<String>() );
upcField.setOutputMarkupId(true);
upcField.setRequired(true);
upcField.add(new BarcodeUPCValidator());
Here is what's in my properties file:
upc.null=UPC cannot be null
upc.Required=UPC is required
currentLpn.null=LPN cannot be null
currentLpn.Required=LPN is required
I get the *.Required message only after i've entered info in the field, validated it, then delete it and try validating again. If I just load the form and submit it without ever entering any data, I get nothing.
Upvotes: 1
Views: 3051
Reputation: 55
If you just want to validate a field if it is not empty then set the field required:
component.setRequired(true);
Then add a label for you error message
component.setLabel(Model.of("Username"));
Then add a a FeedbackPanel
FeedbackPanel feedback = new FeedbackPanel("feedback");
feedback.setOutputMarkupId(true);
form.add(feedback);
To finish, you have to @Override the onError method in your AjaxSubmitLink :
add(new AjaxSubmitLink("save", form) {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
super.onSubmit(target, form);
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
super.onError(target, form);
target.add(feedback);
}
});
Then if your field will be empty the error message will be:
The field 'Username' cannot be empty.
Upvotes: 0
Reputation: 16151
If you just want to validate a field if it is not empty then set the field required:
component.setRequired(Boolean.TRUE);
Then if you want to provide feedback to the user you have to use a FeedbackPanel
. Configure the feedbackPanel to be visible when there are any messages:
private FeedbackPanel feedbackPanel;
feedbackPanel = new FeedbackPanel("feedbackPanel") {
@Override
protected void onConfigure() {
super.onConfigure();
setVisible(anyMessage());
}
};
feedbackPanel.setOutputMarkupPlaceholderTag(true);
Then if you submit your form using AJAX you have to add the feedbackPanel to the AjaxRequestTarget
to that it will get re-rendere and if there are any (error)messages the feedbackPanel will display the messages:
AjaxButton btn = new AjaxButton("button") {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
super.onSubmit(target, form);
target.add(feedbackPanel);
}
};
Additionally to get a good error message set a label for your component:
component.setLabel(Model.of("Username"));
Then if your field will be empty the error message will be:
The field 'Username' cannot be empty.
Upvotes: 3