WicketのカスタムNotEqualInputValidator

WicketのカスタムNotEqualInputValidator

Wicketには、開発者向けの多くの組み込みバリデーター(EqualInputValidatorなど)が含まれています。2つのフォームコンポーネントのIDを比較するのに最適です。 ただし、Wicketは、等しくないバリデーターのバリデーターを提供していません。

NotEqualInputValidator

ここでは、EqualInputValidatorを参照として使用し、「NotEqualInputValidator」という新しいカスタムバリデーターを作成します。これは、2つのフォームコンポーネントのコンパレーターが等しくないためです。

package com.example.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));

Note
この「http://www.example.com/wicket/create-custom-validator-in-wicket/[Wicketでカスタムバリデーターを作成する方法]」を読むとよいでしょう。 。