You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jean-Marie Pitre <jm...@cofisun.com> on 2006/08/04 16:36:09 UTC

Validator lost data when go back to form

Hi,

I'am using validator to validate data on client side and server side.
It works fine, but when I validate on server side when there are errors detected I go back to the html but without user data (the form is reseted).

Have you got any idea ?

Thanks in advance,

Jean-Marie.

-----Message d'origine-----
De : fea jabi [mailto:zyxrm@hotmail.com] 
Envoyé : vendredi 4 août 2006 15:24
À : user@struts.apache.org
Objet : RE: validate integer with a comma thousands seperator using Validator

Thanks, Eric.


>From: "Givler, Eric" <eg...@state.pa.us>
>Reply-To: "Struts Users Mailing List" <us...@struts.apache.org>
>To: "Struts Users Mailing List" <us...@struts.apache.org>
>Subject: RE: validate integer with a comma thousands seperator using 
>Validator
>Date: Thu, 3 Aug 2006 11:45:28 -0400
>
>You could create another validator, and inside that perform a stripping of 
>non-numeric characters (stripCommas), then
>execute code similar to the existing "long range" validator like this:
>
>
>     /**
>     * Determines if a formatted numeric value is between a range as 
>defined by
>     * the user-supplied variables: min and max
>     * <p>
>     * Note: The only formattting performed is stripping off any commas.  
>If the
>     * resulting number does not convert to a Long, then validation will 
>fail as
>     * well.
>     * </p>
>     * <p>
>     * Validation succeeds if:
>     * <ol>
>     *    <li>The value in question is null.</li>
>     *    <li>The parsed field value is within the user-supplied 
>range.</li>
>     * </ol>
>     * </p>
>     * @return
>     * @param application
>     * @param request
>     * @param errors
>     * @param field
>     * @param va
>     * @param bean
>     */
>     public static boolean validateFormattedLongRange(
>         Object bean,
>         ValidatorAction va,
>         Field field,
>         ActionMessages errors,
>         HttpServletRequest request,
>         ServletContext application)
>     {
>         System.out.println("*** validateFormattedLongRange (" + 
>field.getProperty() + ") - START");
>         boolean isValid = true;
>
>         String value = null;
>         value = ValidationUtils.evaluateBean(bean, field);
>         if (!GenericValidator.isBlankOrNull(value))
>         {
>             try
>             {
>                 long minVal = Long.parseLong(field.getVarValue("min"));
>                 long maxVal = Long.parseLong(field.getVarValue("max"));
>                 // We don't want to do this unless we use map.put to show 
>tampered value
>                 // value = value.replaceAll("[^-0123456789.]", "");
>                 value = value.replaceAll("[,]", "");
>                 long lngValue = Long.parseLong(value);
>
>                 if ((lngValue > maxVal) || (lngValue < minVal))
>                 {
>                     errors.add(field.getKey(),
>                         Resources.getActionMessage(request, va, field));
>                     isValid = false;
>                 }
>             }
>             catch (NumberFormatException nfex)
>             {
>                 errors.add(field.getKey(),
>                     Resources.getActionMessage(request, va, field));
>                 isValid = false;
>             }
>         }
>         System.out.println("*** validateFormattedLongRange (" + 
>field.getProperty() + ") - END, returning " + isValid);
>         return isValid;
>     }
>
>Define this validator in validator-rules.xml:
>
>       <validator name="fmtLongRange"
>             classname="view.struts.validator.StrutsValidationExtensions"
>                method="validateFormattedLongRange"
>          methodParams="java.lang.Object,
>                        org.apache.commons.validator.ValidatorAction,
>                        org.apache.commons.validator.Field,
>                        org.apache.struts.action.ActionMessages,
>                        javax.servlet.http.HttpServletRequest,
>                        javax.servlet.ServletContext"
>                   msg="errors.fmtLongRange">
>       </validator>
>
>Add a rule for a field:
>
>       <field property="water_usage" page="2" depends="fmtLongRange">
>         <arg0 name="required" key="Page_2.water_usage"/>
>         <arg0 name="fmtLongRange" key="Page_2.water_usage" />
>         <arg1 name="fmtLongRange" key="${var:min}" resource="false"/>
>         <arg2 name="fmtLongRange" key="999,999,999" resource="false"/>
>         <var><var-name>min</var-name><var-value>0</var-value></var>
>         
><var><var-name>max</var-name><var-value>999999999</var-value></var>
>       </field>
>
>Define the message in the resource file (errors.fmtLongRange):
>
>errors.fmtLongRange={0} must be a valid whole number between {1} and {2}.
>
>
>-----Original Message-----
>From: fea jabi [mailto:zyxrm@hotmail.com]
>Sent: Thursday, August 03, 2006 10:01 AM
>To: user@struts.apache.org
>Subject: RE: validate integer with a comma thousands seperator using
>Validator
>
>
>can someone help me with this please?
>
>
> >From: "fea jabi" <zy...@hotmail.com>
> >Reply-To: "Struts Users Mailing List" <us...@struts.apache.org>
> >To: user@struts.apache.org
> >Subject: validate integer with a comma thousands seperator using 
>Validator
> >Date: Wed, 02 Aug 2006 10:09:19 -0400
> >
> >Using struts validator.
> >
> >have to validate the user entered value.
> >
> >The value entered should be a positive integer with a comma thousands
> >seperator. the number need not be in thousands.
> >
> >I have as below to check for positive integer without comma seperator. 
>but
> >not sure how to validate if the user entered value with a comma 
>seperator?
> >
> >i.e value like 25,349 // how to validate this?
> >
> ><field property="hrs" depends="integer,validwhen">
> >                <msg name="integer" key="errors.notvalid"/>
> >                <msg name="validwhen" key="errors.notvalid"/>
> >                <var>
> >                    <var-name>test</var-name>
> >                    <var-value>(*this* >= 0)</var-value>
> >                </var>
> >            </field>
> >
> >how to validate the user entered value with a comma seperator? Thanks.
> >
> >_________________________________________________________________
> >Don't just search. Find. Check out the new MSN Search!
> >http://search.msn.click-url.com/go/onm00200636ave/direct/01/
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >For additional commands, e-mail: user-help@struts.apache.org
> >
>
>_________________________________________________________________
>Don't just search. Find. Check out the new MSN Search!
>http://search.msn.click-url.com/go/onm00200636ave/direct/01/
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


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


-------------------
Email Disclaimer
http://www.cofidis.be/emaildisclaimer.php


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


RE: Validator lost data when go back to form

Posted by Martin Gainty <mg...@hotmail.com>.
It *appears* you are redirecting the user in which case you are losing all 
of the users session Data
You may want to define the ActionMapping to forward the request and ensure 
redirect=false

Here is an explanation of the use of redirect vs forward
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg21820.html

Here is doc on implementation details for the use of redirect for 
ActionForward
http://www.ingrid.org/jajakarta/struts/struts1.0/en/target/documentation/api/org/apache/struts/action/ActionForward.html#ActionForward(java.lang.String,%20boolean)

Anyone else?
Martin--
______________________________________________
Disclaimer and confidentiality note
Everything in this e-mail and any attachments relates to the official 
business of Sender. This transmission is of a confidential nature and Sender 
does not endorse distribution to any party other than intended recipient. 
Sender does not necessarily endorse content contained within this 
transmission.




>
>
>Hi,
>
>I'am using validator to validate data on client side and server side.
>It works fine, but when I validate on server side when there are errors 
>detected I go back to the html but without user data (the form is reseted).
>
>Have you got any idea ?
>
>Thanks in advance,
>
>Jean-Marie.
>
>-----Message d'origine-----
>Deа: fea jabi [mailto:zyxrm@hotmail.com]
>Envoyжа: vendredi 4 aoчt 2006 15:24
>└а: user@struts.apache.org
>Objetа: RE: validate integer with a comma thousands seperator using 
>Validator
>
>Thanks, Eric.
>
>
> >From: "Givler, Eric" <eg...@state.pa.us>
> >Reply-To: "Struts Users Mailing List" <us...@struts.apache.org>
> >To: "Struts Users Mailing List" <us...@struts.apache.org>
> >Subject: RE: validate integer with a comma thousands seperator using
> >Validator
> >Date: Thu, 3 Aug 2006 11:45:28 -0400
> >
> >You could create another validator, and inside that perform a stripping 
>of
> >non-numeric characters (stripCommas), then
> >execute code similar to the existing "long range" validator like this:
> >
> >
> >     /**
> >     * Determines if a formatted numeric value is between a range as
> >defined by
> >     * the user-supplied variables: min and max
> >     * <p>
> >     * Note: The only formattting performed is stripping off any commas.
> >If the
> >     * resulting number does not convert to a Long, then validation will
> >fail as
> >     * well.
> >     * </p>
> >     * <p>
> >     * Validation succeeds if:
> >     * <ol>
> >     *    <li>The value in question is null.</li>
> >     *    <li>The parsed field value is within the user-supplied
> >range.</li>
> >     * </ol>
> >     * </p>
> >     * @return
> >     * @param application
> >     * @param request
> >     * @param errors
> >     * @param field
> >     * @param va
> >     * @param bean
> >     */
> >     public static boolean validateFormattedLongRange(
> >         Object bean,
> >         ValidatorAction va,
> >         Field field,
> >         ActionMessages errors,
> >         HttpServletRequest request,
> >         ServletContext application)
> >     {
> >         System.out.println("*** validateFormattedLongRange (" +
> >field.getProperty() + ") - START");
> >         boolean isValid = true;
> >
> >         String value = null;
> >         value = ValidationUtils.evaluateBean(bean, field);
> >         if (!GenericValidator.isBlankOrNull(value))
> >         {
> >             try
> >             {
> >                 long minVal = Long.parseLong(field.getVarValue("min"));
> >                 long maxVal = Long.parseLong(field.getVarValue("max"));
> >                 // We don't want to do this unless we use map.put to 
>show
> >tampered value
> >                 // value = value.replaceAll("[^-0123456789.]", "");
> >                 value = value.replaceAll("[,]", "");
> >                 long lngValue = Long.parseLong(value);
> >
> >                 if ((lngValue > maxVal) || (lngValue < minVal))
> >                 {
> >                     errors.add(field.getKey(),
> >                         Resources.getActionMessage(request, va, field));
> >                     isValid = false;
> >                 }
> >             }
> >             catch (NumberFormatException nfex)
> >             {
> >                 errors.add(field.getKey(),
> >                     Resources.getActionMessage(request, va, field));
> >                 isValid = false;
> >             }
> >         }
> >         System.out.println("*** validateFormattedLongRange (" +
> >field.getProperty() + ") - END, returning " + isValid);
> >         return isValid;
> >     }
> >
> >Define this validator in validator-rules.xml:
> >
> >       <validator name="fmtLongRange"
> >             classname="view.struts.validator.StrutsValidationExtensions"
> >                method="validateFormattedLongRange"
> >          methodParams="java.lang.Object,
> >                        org.apache.commons.validator.ValidatorAction,
> >                        org.apache.commons.validator.Field,
> >                        org.apache.struts.action.ActionMessages,
> >                        javax.servlet.http.HttpServletRequest,
> >                        javax.servlet.ServletContext"
> >                   msg="errors.fmtLongRange">
> >       </validator>
> >
> >Add a rule for a field:
> >
> >       <field property="water_usage" page="2" depends="fmtLongRange">
> >         <arg0 name="required" key="Page_2.water_usage"/>
> >         <arg0 name="fmtLongRange" key="Page_2.water_usage" />
> >         <arg1 name="fmtLongRange" key="${var:min}" resource="false"/>
> >         <arg2 name="fmtLongRange" key="999,999,999" resource="false"/>
> >         <var><var-name>min</var-name><var-value>0</var-value></var>
> >
> ><var><var-name>max</var-name><var-value>999999999</var-value></var>
> >       </field>
> >
> >Define the message in the resource file (errors.fmtLongRange):
> >
> >errors.fmtLongRange={0} must be a valid whole number between {1} and {2}.
> >
> >
> >-----Original Message-----
> >From: fea jabi [mailto:zyxrm@hotmail.com]
> >Sent: Thursday, August 03, 2006 10:01 AM
> >To: user@struts.apache.org
> >Subject: RE: validate integer with a comma thousands seperator using
> >Validator
> >
> >
> >can someone help me with this please?
> >
> >
> > >From: "fea jabi" <zy...@hotmail.com>
> > >Reply-To: "Struts Users Mailing List" <us...@struts.apache.org>
> > >To: user@struts.apache.org
> > >Subject: validate integer with a comma thousands seperator using
> >Validator
> > >Date: Wed, 02 Aug 2006 10:09:19 -0400
> > >
> > >Using struts validator.
> > >
> > >have to validate the user entered value.
> > >
> > >The value entered should be a positive integer with a comma thousands
> > >seperator. the number need not be in thousands.
> > >
> > >I have as below to check for positive integer without comma seperator.
> >but
> > >not sure how to validate if the user entered value with a comma
> >seperator?
> > >
> > >i.e value like 25,349 // how to validate this?
> > >
> > ><field property="hrs" depends="integer,validwhen">
> > >                <msg name="integer" key="errors.notvalid"/>
> > >                <msg name="validwhen" key="errors.notvalid"/>
> > >                <var>
> > >                    <var-name>test</var-name>
> > >                    <var-value>(*this* >= 0)</var-value>
> > >                </var>
> > >            </field>
> > >
> > >how to validate the user entered value with a comma seperator? Thanks.
> > >
> > >_________________________________________________________________
> > >Don't just search. Find. Check out the new MSN Search!
> > >http://search.msn.click-url.com/go/onm00200636ave/direct/01/
> > >
> > >
> > >---------------------------------------------------------------------
> > >To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > >For additional commands, e-mail: user-help@struts.apache.org
> > >
> >
> >_________________________________________________________________
> >Don't just search. Find. Check out the new MSN Search!
> >http://search.msn.click-url.com/go/onm00200636ave/direct/01/
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >For additional commands, e-mail: user-help@struts.apache.org
> >
>
>_________________________________________________________________
>Express yourself instantly with MSN Messenger! Download today - it's FREE!
>http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>
>
>-------------------
>Email Disclaimer
>http://www.cofidis.be/emaildisclaimer.php
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>



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