I have a form that I would like to validate. The rule is simple, only one
field can be submitted on the form. All other fields should be null. Here's
an example of how I'm currently doing it...
 
validation.xml:
 
            <field property="firstName" depends="singleFieldOnly">
                <arg0 key="label.firstName"/>
                <var>
                    <var-name>singleFieldOnly</var-name>
                    <var-value>lastName,cardNumber</var-value>
                </var>            
            </field>
            <field property="lastName" depends="singleFieldOnly">
                <arg0 key="label.firstName"/>
                <var>
                    <var-name>singleFieldOnly</var-name>
                    <var-value>firstName,cardNumber</var-value>
                </var>            
            </field>
            <field property="cardNumber" depends="singleFieldOnly">
                <arg0 key="label.cardNumber"/>
                <var>
                    <var-name>singleFieldOnly</var-name>
                    <var-value>firstName,lastName</var-value>
                </var>            
            </field>
 
here's my custom validator code:
 
 public static boolean validateSingleFieldOnly(Object bean,
   ValidatorAction va, Field field, ActionMessages errors,
   HttpServletRequest request) {
  String value = ValidatorUtil
    .getValueAsString(bean, field.getProperty());
  String sProperty2 = field.getVarValue("singleFieldOnly");
 
  if (!GenericValidator.isBlankOrNull(value)) {
 
   // tokenize the fields into an array
   String[] fieldNameArray = sProperty2.split(",");
 
   for (int i = 0; i < fieldNameArray.length; i++) {
    // check each field for null
    try {
     // get the field value
     String value2 = ValidatorUtil.getValueAsString(bean,
       fieldNameArray[i]);
     // if the field value is not null, return false
     if (!GenericValidator.isBlankOrNull(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;
 }
 
Basically, I check each field that I pass in with the singleFieldOnly <var>
to see if they're null. It gets pretty messy maintaining the validation.xml
when I have a huge form. I'm thinking this is a common scenario and there's
a more elegant solution out there.
 
I just picked up struts so any info would be appreciated. Thanks!



"MMS <safeway.com>" made the following annotations.
------------------------------------------------------------------------------
Warning: 
All e-mail sent to this address will be received by the Safeway corporate 
e-mail system, and is subject to archival and review by someone other than the 
recipient.  This e-mail may contain information proprietary to Safeway and is 
intended only for the use of the intended recipient(s).  If the reader of this 
message is not the intended recipient(s), you are notified that you have 
received this message in error and that any review, dissemination, distribution 
or copying of this message is strictly prohibited.  If you have received this 
message in error, please notify the sender immediately. 
  
==============================================================================


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to