Search code examples
javavalidationstrutsstruts-1struts-validation

Struts: Validate two fields at once


I am new with struts and I have a problem that I can't seem to fix.

The thing is, I have two date fields in a .jsp page, and both are required. 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


Solution

  • The validation of two or more fields too easy if you use ValidatorForm and validate method.

    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;
        }
    
    }