1

If i have 3 text fields on a form how can i use the struts validator to check that a compination of all 3 fields should form a valid date.

For example

<form action="/test">

  <input type=text name=day> 

  <select name=month>
  <option value=JAN>JAN</option>
  <option value=JAN>JAN</option>
  <option value=JAN>FEB</option>
     ---
    ---
  <option value=DEC>DEC</option>
 </select>

 <input type=text name=year> 

</form>

In the struts validator i can do something like this

 <field
                property="day"
                depends="required,mask">
                    <arg key="myForm.day"/>
                    <var>
                        <var-name>mask</var-name>
                        <var-value>^[0-9]*$</var-value>
                    </var>
            </field>         

But how can i check that day+Month+year is a valid date? including Februaries and leap years?

ziggy
  • 15,677
  • 67
  • 194
  • 287

1 Answers1

1

Seeing the Struts documentation Struts Validator Guide find the following example

Comparing 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>

public static boolean validateTwoFields(
Object bean,
ValidatorAction va, 
Field field,
ActionErrors errors,
HttpServletRequest request, 
ServletContext application) {

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

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

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

return true;}

I think you could be a basis to compare and validate your three fields which together form a correct date

Good Luck.

hkadejo
  • 162
  • 5