WicketのカスタムNotEqualInputValidator
Wicketには、開発者向けの多くのビルドインバリデータが含まれています。たとえば、 `EqualInputValidator`のように、2つのフォームコンポーネントのアイデンティティを比較するのに最適です。しかし、Wicket’sは、同等のバリデータではないバリデータを提供していませんでした。
NotEqualInputValidator
ここで私は `EqualInputValidator`を参照として取得し、2つのフォームコンポーネントの等価コンパレータでないため、” NotEqualInputValidator “という新しいカスタムバリデータを作成します。
package com.mkyong.user; 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); } } }
どのように使用するのですか?
それを使用するには、通常のバリデータのように接続します。
private PasswordTextField passwordTF; private PasswordTextField cpasswordTF; add(new NotEqualInputValidator(oldpasswordTF,passwordTF));