You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Anderson, James H [IT]" <ja...@citigroup.com> on 2004/02/26 15:54:26 UTC

custom validator question

I'm creating a custom validator to enforce a <= relationship between 2 date fields and am having a problem.

A validator method gets a Field arg for the field with which it's associated in validation.xml, but I need to also get the Field object for the other date field. I need this so that I can get its property name for display in an error message. (I'd also like to be able to get the field's datePattern.) I can't figure out how to do this.

Any help would be much appreciated!

jim

Here's the relevant part of my validation.xml file:

      <form name="ActivityViewForm">
        <field
           property="starting"
           depends="date">
          <arg0 key="ActivityViewForm.starting.displayname"/>
          <var>
            <var-name>datePatternStrict</var-name>
            <var-value>MM/dd/yyyy</var-value>
          </var>
        </field>
        <field
           property="ending"
           depends="date,date1LEdate2">
          <arg0 key="ActivityViewForm.ending.displayname"/>
          <var>
            <var-name>datePatternStrict</var-name>
            <var-value>MM/dd/yyyy</var-value>
          </var>
          <var>
            <var-name>date1</var-name>
            <var-value>starting</var-value>
          </var>
        </field>
      </form>

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


Re: custom validator question

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Specify the second field as <var>

<field property="myFirstDate" depends="date,dateCompare">
     <var>
         <var-name>datePattern</var-name>
         <var-value>dd/MM/yyyy</var-value>
     </var>
     <var>
         <var-name>compareDate</var-name>
         <var-value>mySecondDate</var-value>
     </var>
</field>

Then in your custom validation method:

    String datePattern = field.getVarValue("datePattern");
    String compareDateProperty = field.getVarValue("compareDate");

    String compareDate = ValidatorUtils.getValueAsString(bean,
compareDateProperty);


Niall

----- Original Message ----- 
From: "Anderson, James H [IT]" <ja...@citigroup.com>
To: <st...@jakarta.apache.org>
Sent: Thursday, February 26, 2004 2:54 PM
Subject: custom validator question


I'm creating a custom validator to enforce a <= relationship between 2 date
fields and am having a problem.

A validator method gets a Field arg for the field with which it's associated
in validation.xml, but I need to also get the Field object for the other
date field. I need this so that I can get its property name for display in
an error message. (I'd also like to be able to get the field's datePattern.)
I can't figure out how to do this.

Any help would be much appreciated!

jim

Here's the relevant part of my validation.xml file:

      <form name="ActivityViewForm">
        <field
           property="starting"
           depends="date">
          <arg0 key="ActivityViewForm.starting.displayname"/>
          <var>
            <var-name>datePatternStrict</var-name>
            <var-value>MM/dd/yyyy</var-value>
          </var>
        </field>
        <field
           property="ending"
           depends="date,date1LEdate2">
          <arg0 key="ActivityViewForm.ending.displayname"/>
          <var>
            <var-name>datePatternStrict</var-name>
            <var-value>MM/dd/yyyy</var-value>
          </var>
          <var>
            <var-name>date1</var-name>
            <var-value>starting</var-value>
          </var>
        </field>
      </form>

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




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


RE: custom validator question

Posted by claire knowles <cl...@digitalbridges.com>.
I had a custom validator 'futureDate' for which I needed to pass in a
date and time which were 2 different form fields, to check that the date
was in the future.

I passed 2 different values into a custom parameter using two args and
vars:

<form name="form">
<field property="emailDate" depends="required, mask, futureDate">
	<msg name="mask" key="errors.invalid"/>
      <arg0 key="form.emailDate"/>
      <arg1 key="form.emailTime"/>
      <var>
      	<var-name>mask</var-name>
      	<var-value>${dateMask}</var-value>
      </var>
      <var>
            <var-name>time</var-name>
            <var-value>emailTime</var-value>
      </var>
</field>
</form>

In the custom validator I retrieved the two values using:

public static boolean validateDate(Object bean, ValidatorAction va,
Field field, ActionErrors errors, HttpServletRequest request)
    {
        String value;

        if (isString(bean))
        {
            value = (String) bean;
        }
        else
        {
            value = ValidatorUtil.getValueAsString(bean,
field.getProperty());
        }

        String propertyTime = field.getVarValue("time");
        String valueTime = ValidatorUtil.getValueAsString(bean,
propertyTime);

        . . . . .

Hope this helps

Claire

-----Original Message-----
From: Anderson, James H [IT] [mailto:james.h.anderson@citigroup.com] 
Sent: 26 February 2004 14:54
To: struts-user@jakarta.apache.org
Subject: custom validator question


I'm creating a custom validator to enforce a <= relationship between 2
date fields and am having a problem.

A validator method gets a Field arg for the field with which it's
associated in validation.xml, but I need to also get the Field object
for the other date field. I need this so that I can get its property
name for display in an error message. (I'd also like to be able to get
the field's datePattern.) I can't figure out how to do this.

Any help would be much appreciated!

jim

Here's the relevant part of my validation.xml file:

      <form name="ActivityViewForm">
        <field
           property="starting"
           depends="date">
          <arg0 key="ActivityViewForm.starting.displayname"/>
          <var>
            <var-name>datePatternStrict</var-name>
            <var-value>MM/dd/yyyy</var-value>
          </var>
        </field>
        <field
           property="ending"
           depends="date,date1LEdate2">
          <arg0 key="ActivityViewForm.ending.displayname"/>
          <var>
            <var-name>datePatternStrict</var-name>
            <var-value>MM/dd/yyyy</var-value>
          </var>
          <var>
            <var-name>date1</var-name>
            <var-value>starting</var-value>
          </var>
        </field>
      </form>


________________________________________________________________________

E-mail is an informal method of communication and may be subject to data corruption, interception and unauthorised amendment for which Digital Bridges Ltd will accept no liability. Therefore, it will normally be inappropriate to rely on information contained on e-mail without obtaining written confirmation.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

________________________________________________________________________


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