You don't need FormValidator for this.
A plain IValidator will do the job.
FormValidator should be used when you need to make some more complex
validation which involves several form components.
So just do:
TextField<Integer> textField = new TextField<>("someId", someModel,
Integer.class);
textField.add(new IValidator<Integer>() {
public void validate(IValidatable<Integer> validatable) {
if (!isValidNumber(validatable.getValue())) {
validatable.error(new ValidationError("The input is not valid"));
}
}
});
AjaxButton submitButton = new AjaxButton("btn", form) {
// onSubmit() ...
protected void onError(AjaxRequestTarget target, Form form) {
// 1. if the form is in ModalWindow then
target.add(feedbackPanel);
// 2. If you want to show Modal with the error then:
modalWindow.setContent(new
SomePanelWithExplanation(ModalWindow.CONTENT_ID));
modalWindow.open(target);
}
}
There may be typos. I haven't written this code in IDE.
On Sun, Apr 28, 2013 at 10:43 PM, Taro Fukunaga <[email protected]>wrote:
> When validation fails I need to display a validation error message in the
> ModalWindow. This code is from my validator.
>
> public class MyValidator
> extends AbstractFormValidator
> implements Serializable {
>
> private final TextField<Integer> textField;
>
> public MyValidator( TextField<Integer> textField ) {
> this.textField = textField;
> }
>
> @Override
> public FormComponent<?>[] getDependentFormComponents() {
>
> return new FormComponent[] { textField };
> }
>
> @Override
> public void validate( Form<?> form ) {
>
> Integer value = textField.getConvertedInput();
>
> if ( !isValidNumber( value.intValue() ) ) {
>
> error( textField, ""invalid" );
> }
> }
>
> And in my panel I add it to my text field:
>
> add( new MyFormValidator( textField ) );
>
> However when I run my code and submit the request, I get an NPE when
> value.initValue() is called because getConvertedInput() returns null. There
> is no value in the textField at this moment. That's why I thought that I
> need to possibly attach a behavior to the text field in order to get the
> value.
>
>
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Form-not-displaying-messages-correctly-tp4658351p4658365.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
--
Martin Grigorov
Wicket Training & Consulting
http://jWeekend.com <http://jweekend.com/>