You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jefferson Wilson <je...@yahoo.com> on 2008/10/08 19:24:51 UTC

validwhen calculation is true

I have a form where the user will peform a simple calculation in order to prevent spam (1 + 10 = 11).  As a result, I would like to use the 'validwhen' validation step to handle this.  I am overlooking something I believe.

I have created the following:

1)  Field Definition In validation.xml

<field    
     property="answer"
    depends="validwhen">
      
    <msg name="validwhen" key="errors.easy.math"/>
    <arg position = "0" key="display.answer"/>
      
    <var>
        <var-name>test</var-name>
        <var-value>(*this* = (firstArgument + secondArgument))</var-value>
    </var>
</field>

2)  Configure 'validwhen' validator in validation-rules.xml

<validator name="validwhen"         
                 classname="org.apache.struts.validator.validwhen.ValidWhen"
                 method="validateValidWhen"
                 methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
          msg="errors.required">
</validator>

3)  Setup DynaValidatorActionForm

<form-bean name="contactInfoForm" type="org.apache.struts.validator.DynaValidatorActionForm">
..
..
..
<form-property name="firstArgument" type="java.lang.String"/>
<form-property name="secondArgument" type="java.lang.String"/>
<form-property name="answer" type="java.lang.String"/>
..
..
..
</form>

I am not sure why this is not working.  The DynaValidatorActionForm is setup with all fields as strings - this is a bean so this is fine.  I know that the 'validwhen' validator is setup properly because I am using it to validate two email addresses.  Since I am relatively new to Struts, is there a problem since the types are not Integers?  Where is this conversion taking place?  I am having a hard time with this.  Please advise.  

Thanks,
J.W.



      

Re: validwhen calculation is true

Posted by Laurie Harper <la...@holoweb.net>.
I don't think the validWhen validator supports either type cooercion or 
arithmetic. You'll probably need to handle this validation 
programatically in your validate() method.

L.

Jefferson Wilson wrote:
> I have a form where the user will peform a simple calculation in order to prevent spam (1 + 10 = 11).  As a result, I would like to use the 'validwhen' validation step to handle this.  I am overlooking something I believe.
> 
> I have created the following:
> 
> 1)  Field Definition In validation.xml
> 
> <field    
>      property="answer"
>     depends="validwhen">
>       
>     <msg name="validwhen" key="errors.easy.math"/>
>     <arg position = "0" key="display.answer"/>
>       
>     <var>
>         <var-name>test</var-name>
>         <var-value>(*this* = (firstArgument + secondArgument))</var-value>
>     </var>
> </field>
> 
> 2)  Configure 'validwhen' validator in validation-rules.xml
> 
> <validator name="validwhen"         
>                  classname="org.apache.struts.validator.validwhen.ValidWhen"
>                  method="validateValidWhen"
>                  methodParams="java.lang.Object,
>                        org.apache.commons.validator.ValidatorAction,
>                        org.apache.commons.validator.Field,
>                        org.apache.struts.action.ActionMessages,
>                        org.apache.commons.validator.Validator,
>                        javax.servlet.http.HttpServletRequest"
>           msg="errors.required">
> </validator>
> 
> 3)  Setup DynaValidatorActionForm
> 
> <form-bean name="contactInfoForm" type="org.apache.struts.validator.DynaValidatorActionForm">
> ...
> ...
> ...
> <form-property name="firstArgument" type="java.lang.String"/>
> <form-property name="secondArgument" type="java.lang.String"/>
> <form-property name="answer" type="java.lang.String"/>
> ...
> ...
> ...
> </form>
> 
> I am not sure why this is not working.  The DynaValidatorActionForm is setup with all fields as strings - this is a bean so this is fine.  I know that the 'validwhen' validator is setup properly because I am using it to validate two email addresses.  Since I am relatively new to Struts, is there a problem since the types are not Integers?  Where is this conversion taking place?  I am having a hard time with this.  Please advise.  
> 
> Thanks,
> J.W.
> 
> 
> 
>       


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