在本教程中,您将创建一个自定义密码验证器,并将其附加到密码字段。
请参阅创建自定义验证器的摘要步骤:
1.实施IValidator
。
import org.apache.wicket.validation.IValidator;
public class StrongPasswordValidator implements IValidator<String>{
...
}
2.覆盖validate(IValidatable validatable)
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.将自定义验证器附加到表单组件。
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());
//...
}
}
完整的例子
请参阅下面的Wicket示例以创建自定义密码验证器,如果密码与预定义的模式不匹配,则显示错误消息。
1. StrongPasswordValidator
自定义密码验证器。
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);
}
}
文件:package.properties
StrongPasswordValidator.not-strong-password = Password required at least ... (omitted)
2.附加到组件
将上述自定义验证程序附加到密码字段。
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.演示
开始并访问-http:// localhost:8080 / WicketExamples /
键入一个弱密码,从自定义验证器返回错误消息并显示。
下载它– Wicket-Custom-Validator-Example.zip (9KB)
参考文献
翻译自: https://mkyong.com/wicket/create-custom-validator-in-wicket/