You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Axel Stahlhut <as...@neusta.de> on 2002/09/03 23:45:57 UTC

[VALIDATOR] Validating Date with DynaValidator

Sorry for posting this one again, but its a bit urgent and maybe i have a chance marking it as a post concerning [Validator].

Validating a Date with the Struts-Validator Framework works fine, but if the field i want to validate is not a required field and may be empty, the validator always reports an error, even if if i remove the depends="required" in the validation-rules.xml. 
Do i have to implement the Validator-class in a way that it doesnt validate empty dates or is there any declarative way to solve this?
Thanks for any help.

Axel

This is my validation-rules.xml:

      <validator name="date"
            classname="org.apache.struts.util.StrutsValidator"
               method="validateDate"
         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.date"
                       jsFunctionName="DateValidations">

and the validation.xml:

<field property="beginEmployment" depends="date">
<arg0 key="personal.data.beginEmployment" />
<var>
<var-name>datePattern</var-name>
<var-value>dd.MM.yyyy</var-value>
</var>
</field>



Re: [VALIDATOR] Validating Date with DynaValidator

Posted by Rick Reumann <ma...@reumann.net>.
On Tuesday, September 3, 2002, 5:45:57 PM, Axel Stahlhut wrote:


AS> This is my validation-rules.xml:

AS>       <validator name="date"
AS>             classname="org.apache.struts.util.StrutsValidator"
AS>                method="validateDate"
AS>          methodParams="java.lang.Object,
AS>                        org.apache.commons.validator.ValidatorAction,
AS>                        org.apache.commons.validator.Field,
AS>                        org.apache.struts.action.ActionErrors,
AS>                        javax.servlet.http.HttpServletRequest"
AS>                        msg="errors.date"
AS>                        jsFunctionName="DateValidations">

AS> and the validation.xml:

AS> <field property="beginEmployment" depends="date">
AS> <arg0 key="personal.data.beginEmployment" />
AS> <var>
AS> <var-name>datePattern</var-name>
AS> <var-value>dd.MM.yyyy</var-value>
AS> </var>
AS> </field>

    I think you need the depends="required" in the validation-rules
    for date. You don't want it in the validation.xml but in the rule
    I think you might need it to get it to work correctly. I have the
    depends="required" in the validation-rules.xml for date and my
    dates are getting validated if I leave the field blank, so give it
    a try. Not positive that's the problem though.

--

Rick

mailto:maillist@reumann.net


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [VALIDATOR] Validating Date with DynaValidator

Posted by Marco Maier <Ma...@ics-software.de>.
Axel Stahlhut wrote:
> Sorry for posting this one again, but its a bit urgent and maybe i have a chance marking it as a post concerning [Validator].
> 
> Validating a Date with the Struts-Validator Framework works fine, but if the field i want to validate is not a required field and may be empty, the validator always reports an error, even if if i remove the depends="required" in the validation-rules.xml. 
> Do i have to implement the Validator-class in a way that it doesnt validate empty dates or is there any declarative way to solve this?
> Thanks for any help.
> 
> Axel
> 
> This is my validation-rules.xml:
> 
>       <validator name="date"
>             classname="org.apache.struts.util.StrutsValidator"
>                method="validateDate"
>          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.date"
>                        jsFunctionName="DateValidations">
> 
> and the validation.xml:
> 
> <field property="beginEmployment" depends="date">
> <arg0 key="personal.data.beginEmployment" />
> <var>
> <var-name>datePattern</var-name>
> <var-value>dd.MM.yyyy</var-value>
> </var>
> </field>
> 
> 
> 

Hi Axel,

you can subclass the DynaValidatorForm to override the validator method.
There you can check if the date field is empty, something like that

public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request)
{
   String date = null;

   if (((date = (String) this.get("yourDateField")) == null)
        || (date.length == 0))
   {
     // do no validation
     return null;
   }
   return super.validate(mapping, request);
}

Hope this helps.

Marco


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>