Wicket password field example

Wicket tutorial to show you how to create 2 password fields – “password” and “confirm password“, attach a strong password validator, and pass the password value to next page.


//Java 
import org.apache.wicket.markup.html.form.PasswordTextField;
...
final PasswordTextField password = new PasswordTextField("password", Model.of(""));
form.add(password);
 
//HTML
<input wicket:id="password" type="password" size="20" />

1. Wicket password example

An user page to render two password fields. Attached two validators, PatternValidator and EqualPasswordInputValidator for password checking.

File : UserPage.java


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.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.Model;
import org.apache.wicket.validation.validator.PatternValidator;

public class UserPage extends WebPage {

	//1 digit, 1 lower, 1 upper, 1 symbol "@#$%", from 6 to 20
	private final String PASSWORD_PATTERN 
                          = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";

	public UserPage(final PageParameters parameters) {

		add(new FeedbackPanel("feedback"));

		//create a password field
		final PasswordTextField password = new PasswordTextField("password",
				Model.of(""));
		//for properties file
		password.setLabel(Model.of("Password")); 

		final PasswordTextField cpassword = new PasswordTextField("cpassword",
				Model.of(""));
		cpassword.setLabel(Model.of("Confirm Password"));

		password.add(new PatternValidator(PASSWORD_PATTERN));

		Form<?> form = new Form<Void>("userForm") {
			@Override
			protected void onSubmit() {
				//get the entered password and pass to next page
				PageParameters pageParameters = new PageParameters();
				pageParameters.add("password", password.getModelObject());
				setResponsePage(SuccessPage.class, pageParameters);

			}
		};

		add(form);
		form.add(password);
		form.add(cpassword);
		form.add(new EqualPasswordInputValidator(password, cpassword));

	}
}

File : UserPage.html


<html>
<head>
<style>
label {
	background-color: #eee;
	padding: 4px;
}

.feedbackPanelERROR {
	color: red;
}
</style>
</head>
<body>
	<h1>Wicket password Example</h1>

	<div wicket:id="feedback"></div>
	<form wicket:id="userForm">
		<p>
			<label>Password</label>: 
                        <input wicket:id="password" type="password" size="20" />
		</p>
		<p>
			<label>Confirm Password</label>: 
                        <input wicket:id="cpassword" type="password" size="20" />
		</p>
		<input type="submit" value="Register" />
	</form>

</body>
</html>

2. package.properties

Puts string in a “package.properties“, so that it can share among the other pages.

File : package.properties


password.Required = ${label} is required
cpassword.Required = ${label} is required
password.PatternValidator = ${label} should contains at least 1 digit, ... (omitted) 
cpassword.EqualPasswordInputValidator = "${label} did not match!"

3. Demo

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

If password is not meet the regular expression pattern :

wicket pattern error

If password and confirm password is not match :

wicket password error
Download it – Wicket-password-example.zip (8KB)

References

  1. Validate password with regular expression
  2. Wicket PasswordTextField Javadoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
balaRich LivingstoneCreate custom validator in Wicket Recent comment authors
newest oldest most voted
trackback
Create custom validator in Wicket

[…] Wicket PasswordTextField example […]

Rich Livingstone
Guest
Rich Livingstone

Very handy as I was having trouble getting my error message validation resource bundle to work – the Wicket examples are incorrect and suggest that the name .RequiredValidator is the name of the properties whereas what actually works is what you’ve said here i.e. .Required

Ta very much!

bala
Guest
bala

Could you let me know how to make a post request in wicket? the value of password is displayed in url