Wicket中的自定义NotEqualInputValidator

Wicket包含许多用于开发人员的内置验证器,例如EqualInputValidator ,最好是比较两个表单组件标识。 但是,Wicket没有为不相等的验证器提供任何验证器。

NotEqualInputValidator

在这里,我以EqualInputValidator作为参考,并创建了一个新的自定义验证器“ 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));

注意
您可能想读一读“ 如何在Wicket中创建自定义验证器 ”。

翻译自: https://mkyong.com/wicket/notequalinputvalidator-in-wicket/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值