简体   繁体   English

Struts:一次验证两个字段

[英]Struts: Validate two fields at once

I am new with struts and I have a problem that I can't seem to fix. 我是Struts的新手,但有一个我似乎无法解决的问题。

The thing is, I have two date fields in a .jsp page, and both are required. 问题是, .jsp页面中有两个日期字段,并且两个字段都是必需的。 In short, I have two fields that are required, but I cannot recieve two error messages in case that they are both empty. 简而言之,我有两个必填字段,但是如果它们都是空的,我将无法收到两个错误消息。 The only message that must show up is "please insert the date interval", regardless of wich one is empty(or, if they are both empty). 唯一必须显示的消息是“请插入日期间隔”,而不管哪一个为空(或者,如果两个都为空)。

I'm using validation.xml , Struts version 1.3 我正在使用Struts版本1.3的validation.xml

The validation of two or more fields too easy if you use ValidatorForm and validate method. 如果使用ValidatorFormvalidate方法,则两个或多个字段的验证太容易了。

To use declarative custom validator you need to read this reference guide that has links and example of custom validator to validate two fields. 要使用声明式自定义验证器,您需要阅读参考指南,其中包含用于验证两个字段的链接和自定义验证器的示例。

This is an example of how you could compare two fields to see if they have the same value. 这是一个示例,您可以如何比较两个字段以查看它们是否具有相同的值。 A good example of this is when you are validating a user changing their password and there is the main password field and a confirmation field. 一个很好的例子是,当您验证用户更改密码时,有主密码字段和确认字段。

<validator name="twofields"
       classname="com.mysite.StrutsValidator"
       method="validateTwoFields"
       msg="errors.twofields"/>

<field property="password"
       depends="required,twofields">
          <arg position="0" key="typeForm.password.displayname"/>
          <var>
             <var-name>secondProperty</var-name>
             <var-value>password2</var-value>
          </var>
</field>
public class CustomValidator {

    // ------------------------------------------------------------ Constructors

    /**
     * Constructor for CustomValidator.
     */
    public CustomValidator() {
        super();
    }

    // ---------------------------------------------------------- Public Methods

    /**
     * Example validator for comparing the equality of two fields
     *
     * http://struts.apache.org/userGuide/dev_validator.html
     * http://www.raibledesigns.com/page/rd/20030226
     */
    public static boolean validateTwoFields(
        Object bean,
        ValidatorAction va,
        Field field,
        ActionMessages errors,
        HttpServletRequest request) {

        String value =
            ValidatorUtils.getValueAsString(bean, field.getProperty());
        String property2 = field.getVarValue("secondProperty");
        String value2 = ValidatorUtils.getValueAsString(bean, property2);

        if (!GenericValidator.isBlankOrNull(value)) {
            try {
                if (!value.equals(value2)) {
                    errors.add(
                        field.getKey(),
                        Resources.getActionMessage(request, va, field));

                    return false;
                }
            } catch (Exception e) {
                errors.add(
                    field.getKey(),
                    Resources.getActionMessage(request, va, field));
                return false;
            }
        }
        return true;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM