You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jeremy Nix <Je...@sfsltd.com> on 2003/04/15 16:58:00 UTC

Validator: one or more of a particular set of form fields set

Is there an easy way to add validation for the case where one or more of
a particular set of fields must be set?  I'm comparing 5 separate fields
to see that at least 1 is set.

_________________
Jeremy Nix
Senior Application Developer
Southwest Financial Ltd.
Jeremy.Nix@sfsltd.com
(513) 621-6699 ext 1158


Re: Validator: one or more of a particular set of form fields set

Posted by Joey Ebright <je...@attbi.com>.
No there is no easy way.  Most will tell you that you can do this using 
the requiredif validator. (see 
http://jakarta.apache.org/struts/userGuide/dev_validator.html).  What 
they won't tell you is that this is overly complex and only works server 
side.  I achieved at best a workaround.  Here it is, it makes sure out 
of two fields, both are specified.  You can easily modify it to check 
for your five fields.
     ### validation.xml snippet ###
     <form name="fileUploadForm">
         <field property="file1"
                depends="requireTwoAsGroup">
                     <arg0 key="label.file"/>
                     <arg1 key="label.category"/>
                     <var>
                       <var-name>secondProperty</var-name>
                       <var-value>categories[0]</var-value>
                     </var>
         </field>
      </form>

      ### validator-rules.xml snippet
      <validator name="requireTwoAsGroup"
            classname="odysseus.validator"
               method="validateRequireTwoAsGroup"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionErrors,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.requireTwoAsGroup">
        <javascript><![CDATA[
          function validateRequireTwoAsGroup(form) {
            var bValid = true;
            var focusField = null;
            var i = 0;
            var fields = new Array();
            oRequireTwoAsGroup = new requireTwoAsGroup();
            var value = '';

            for (x in oRequireTwoAsGroup) {

              var field = form[oRequireTwoAsGroup[x][0]];
              var field2 = form[oRequireTwoAsGroup[x][2]("secondProperty")];
              var value;
              var secondValue;

              // get fields value
              if(field.type == 'select-one'){
                var si = field.selectedIndex;
                value = field.options[si].value;
              }
              else {
                value = field.value;
              }

              // get fields value
              if(field.type == 'select-one'){
                var si = field2.selectedIndex;
                secondValue = field2.options[si].value;
              }
              else {
                secondValue = field2.value;
              }

              if(((value != '')&&(secondValue == '')) ||
                ((value == '')&&(secondValue != ''))){
                if (i == 0) {
                  focusField = field;
                }
                fields[i++] = oRequireTwoAsGroup[x][1];
                bValid = false;
              }

            }
            if (fields.length > 0) {
               focusField.focus();
               alert(fields.join('\n'));
            }
            return bValid;
          }
          ]]>
        </javascript>
      </validator>

      ###ApplicationResources.properties snippet ###
      errors.requireTwoAsGroup=Both {0} and {1} are required

      ### validator.java snippet ###
      //needed for server side validation in case javascript is disabled:
  public static boolean validateRequireTwoAsGroup(Object bean, 
ValidatorAction va,
    Field field, ActionErrors errors, HttpServletRequest request) {
    System.out.println("validator.validateRequireTwoAsGroup called");

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

    try {
      if((GenericValidator.isBlankOrNull(value)) && 
(!GenericValidator.isBlankOrNull(value2)) ||
        ((!GenericValidator.isBlankOrNull(value)) && 
(GenericValidator.isBlankOrNull(value)))){
        errors.add(field.getKey(), 
StrutsValidatorUtil.getActionError(request, va, field));
        return false;
      }
    }
    catch (Exception e) {
      errors.add(field.getKey(), StrutsValidatorUtil.getActionError(
        request, va, field));
      return false;
    }

    return true;
  }//validateRequireTwoAsGroup 

I also made validators to ensure one out of a group of fields is at 
least specified and a validator to compare two fields match exactly if 
you need help in this area.  All of which are not the best solution, but 
the best I could do with my limited javascript experience...

Also, check out this link as it is a good source for validation: 
http://www.manning.com/husted/chap12.pdf

Jeremy Nix wrote:

>Is there an easy way to add validation for the case where one or more of
>a particular set of fields must be set?  I'm comparing 5 separate fields
>to see that at least 1 is set.
>
>_________________
>Jeremy Nix
>Senior Application Developer
>Southwest Financial Ltd.
>Jeremy.Nix@sfsltd.com
>(513) 621-6699 ext 1158
>
>
>  
>




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Validator: one or more of a particular set of form fields set

Posted by James Turner <tu...@blackbear.com>.
Currently, you'd use the requiredif validator.  However, just introduced
under the wire into 1.1 is the validwhen rule, which (assuming the
fields are called a, b, c, d and e) would let you say:

<field property="a" depends="validwhen">
   <arg0 key="error.mustSelectOneField"/>
   <var>
      <var-name>test</var-name>
      <var-value>(((((a != null) or (b != null)) or (c != null)) or (d
!= null)) or (e != null))</var-value>
</field>

Look for it in a nightly build (and RC2) once Craig gets back and I can
get the jar file needed for the parser added to the build process.

James

> -----Original Message-----
> From: Jeremy Nix [mailto:Jeremy.Nix@sfsltd.com] 
> Sent: Tuesday, April 15, 2003 10:58 AM
> To: struts-user@jakarta.apache.org
> Subject: Validator: one or more of a particular set of form fields set
> 
> 
> Is there an easy way to add validation for the case where one 
> or more of a particular set of fields must be set?  I'm 
> comparing 5 separate fields to see that at least 1 is set.
> 
> _________________
> Jeremy Nix
> Senior Application Developer
> Southwest Financial Ltd.
> Jeremy.Nix@sfsltd.com
> (513) 621-6699 ext 1158
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org