In this tutorial, you will create a custom password validator, and attach it to a password field.

See summary steps to create a custom validator :

1. Implements IValidator.

import org.apache.wicket.validation.IValidator;
 
public class StrongPasswordValidator implements IValidator<String>{
	...
}

2. Override validate(IValidatable validatable).

public class StrongPasswordValidator implements IValidator<String>{
	...
	@Override
	public void validate(IValidatable<String> validatable) {
 
		//get input from attached component
		final String field = validatable.getValue();
 
	}
}

3. Attached custom validator to form component.

public class CustomValidatorPage extends WebPage {
 
	public CustomValidatorPage(final PageParameters parameters) {
 
  	    final PasswordTextField password = new PasswordTextField("password",Model.of(""));
		//attached custom validator to password field
		password.add(new StrongPasswordValidator());
 
		//...
	}
 
}

Full Example

See following Wicket example to create a custom password validator, and display a error message if password didn’t match a pre-defined pattern.

1. StrongPasswordValidator

A custom password validator.

package com.mkyong.user;
 
import java.util.regex.Pattern;
 
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
 
public class StrongPasswordValidator implements IValidator<String> {
 
	private final String PASSWORD_PATTERN 
                              = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
 
	private final Pattern pattern;
 
	StrongPasswordValidator() {
		pattern = Pattern.compile(PASSWORD_PATTERN);
	}
 
	@Override
	public void validate(IValidatable<String> validatable) {
 
		final String password = validatable.getValue();
 
		// validate password
		if (pattern.matcher(password).matches() == false) {
 
			//Message from key "StrongPasswordValidator.not-strong-password"
			error(validatable, "not-strong-password");
 
		}
 
	}
 
	private void error(IValidatable<String> validatable, String errorKey) {
		ValidationError error = new ValidationError();
		error.addMessageKey(getClass().getSimpleName() + "." + errorKey);
		validatable.error(error);
	}
 
}

File : package.properties

StrongPasswordValidator.not-strong-password = Password required at least ... (omitted)

2. Attach to component

Attach above custom validator to a password field.

package com.mkyong.user;
 
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.Model;
 
public class CustomValidatorPage extends WebPage {
 
	public CustomValidatorPage(final PageParameters parameters) {
 
		add(new FeedbackPanel("feedback"));
 
		final PasswordTextField password 
                                = new PasswordTextField("password",Model.of(""));
 
		//attached custom validator to password field
		password.add(new StrongPasswordValidator());
 
		Form<?> form = new Form<Void>("form") {
			@Override
			protected void onSubmit() {
				info("Done");
			}
		};
 
		add(form);
		form.add(password);
 
	}
 
}
<html>
<head>
<style>
.feedbackPanelERROR {
	color: red;
}
</style>
</head>
<body>
	<h1>Wicket custom validator example</h1>
 
	<div wicket:id="feedback"></div>
	<form wicket:id="form">
		<p>
			<label>Password</label>: 
                        <input wicket:id="password" type="password" size="20" />
		</p>
		        <input type="submit" value="Register" />
	</form>
 
</body>
</html>

3. Demo

Start and visit – http://localhost:8080/WicketExamples/

Key in a weak password, error message is returned from custom validator and displayed.

References

  1. Wicket IValidator Javadoc
  2. Wicket PasswordTextField example
  3. RegEx for strong password
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts