Wicket FileUpload validator is not execute?
Written on
September 18, 2009 at 6:18 am by
mkyong
Wicket’s contains many build-in validators, but it’s may not suit all our needs. Often times we need to implement our own validator for FileUpload component. For example,
FileUploadField fileUpload = new FileUploadField("fileupload",new Model<FileUpload>()); fileUpload .add(new AbstractValidator() { protected void onValidate(IValidatable validatable) { FileUpload fileUpload = (FileUpload) validatable.getValue(); } protected String resourceKey() { return "yourErrorKey"; } } );
Above validation wont work! No matter how many time you click on the submit button, the onSubmit() method will execute and ignore the file upload validation checking, this is because the AbstractValidator didn’t validate on null value.
Class AbstractValidator
* @see IValidator#validate(IValidatable) */ public final void validate(IValidatable<T> validatable) { if (validatable.getValue() != null || validateOnNullValue()) { onValidate(validatable); } }
In order to run your file upload validation, we need to override the validateOnNullValue() method, as follow
FileUploadField fileUpload = new FileUploadField("fileupload",new Model<FileUpload>()); fileUpload .add(new AbstractValidator() { public boolean validateOnNullValue(){ return true; } protected void onValidate(IValidatable validatable) { FileUpload fileUpload = (FileUpload) validatable.getValue(); } protected String resourceKey() { return "yourErrorKey"; } } );
Done.

