NotEqualInputValidator in Wicket
Written on September 25, 2009 at 5:13 am by
mkyong
Wicket is providing many build-in validators for developers, EqualInputValidator is best for compare two form components identity. However Wicket’s didn’t provide any validator for form component not equal checking. Here i take EqualInputValidator as a reference and create a new custom validator “NotEqualInputValidator“, so that we can compare the not equal of two form components.
NotEqualInputValidator in Wicket
import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.FormComponent; import org.apache.wicket.markup.html.form.validation.AbstractFormValidator; import org.apache.wicket.util.lang.Objects; public class NotEqualInputValidator extends AbstractFormValidator { /** * */ private static final long serialVersionUID = 1L; /** form components to be checked. */ private final FormComponent<?>[] components; /** * Construct. * * @param formComponent1 * a form component * @param formComponent2 * a form component */ public NotEqualInputValidator(FormComponent<?> formComponent1, FormComponent<?> formComponent2) { if (formComponent1 == null) { throw new IllegalArgumentException("argument formComponent1 cannot be null"); } if (formComponent2 == null) { throw new IllegalArgumentException("argument formComponent2 cannot be null"); } components = new FormComponent[] { formComponent1, formComponent2 }; } public FormComponent<?>[] getDependentFormComponents() { return components; } public void validate(Form<?> form) { // we have a choice to validate the type converted values or the raw // input values, we validate the raw input final FormComponent<?> formComponent1 = components[0]; final FormComponent<?> formComponent2 = components[1]; if (Objects.equal(formComponent1.getInput(), formComponent2.getInput())) { error(formComponent2); } } }
How to use
private PasswordTextField passwordTF; private PasswordTextField cpasswordTF; //...initialize the text field add(new NotEqualInputValidator(oldpasswordTF,passwordTF));
This article was posted in Wicket category.
Oracle Magazine - Free Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more.
Securing & Optimizing Linux: The Hacking Solution - Free Guide
A comprehensive collection of Linux security products and explanations in the most simple and structured manner on how to safely and easily configure and run many popular Linux-based applications and services.
The Windows 7 Guide: From Newbies to Pros - Free Guide
In this 46 page guide you will be introduced to Windows 7 and what it has to offer. This guide will go over the software compatible issues, you will learn about the new taskbar, how to use and customize Windows Aero, what Windows 7 Libraries are all about, what software is included in Windows 7, and how easy networking is with Windows 7 along with other topics.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
hi,
can u please provide some more examples on wicket which is usefull for beginner like me.
Regards
Santosh M