Simple form validation in Wicket

      No Comments on Simple form validation in Wicket

I used to add custom FormValidators to forms where multi-field validation was required (like checking if two copies of an entered password match.

However, this approach has some problems – I had to list the dependent components and if some of them were hidden at validation time, the validator didn’t work.

Reading around, I saw the recommendation to use the onValidate() method of the form to do validation and this seems a lot more straightforward.

Here’s a sample wicket form with some validation logic to check if the current password matches and if the two copies of the new password are the same.

[code language=”java”]
final Form form = new Form("form", new CompoundPropertyModel(user)) {
private static final long serialVersionUID = 1L;

protected void onValidate() {

User user = model.getObject();
String current_password_entered = currentPasswordField.getConvertedInput();
String new_password_entered = newPasswordField.getConvertedInput();
String confirm_new_password_entered = confirmNewPasswordField.getConvertedInput();

if (current_password_entered != null
&& !User.encryptPassword(current_password_entered).equals(user.getPassword()))

if (new_password_entered != null && !new_password_entered.isEmpty()) {
if (new_password_entered.equals(current_password_entered)) {
} else {
if (!new_password_entered.equals(confirm_new_password_entered))

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.