You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sh...@Biometmail.com on 2003/10/16 21:28:59 UTC

Validator

I am trying to use Struts validator and am running into some problems. Are 
there any good step-by-step instructions? I have Struts in Action and 
tried to follow the chapter concerning Validator but I didn't get the 
desired results and am not sure where to begin in tracing what I may have 
done wrong or not done that I should have.


Thank you 
Shawn Rummel

calling previous ActionForm

Posted by Fedor Smirnoff <in...@olnix.com>.
Hi,

I need to access a method within an ActionForm that was used by the
Action that send a redirect to my current action.
So Action A using ActionForm A and after done processing redirects to
Action B that uses ActionForm B, question how can Action B access
ActionForm A

Thank you
Fedor



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


Re: Validator

Posted by Barry Volpe <st...@childrencare.com>.
**Use the struts-validator.war example
in the struts 1.1 release to learn more.**

make sure you have this in your jsp:

<html:errors />

This is where the validation error gets outputted.

You should have this in your struts-config at the bottom where plug-ins go.

<!-- 
     Add multiple validator resource files by setting the pathnames property
     with a comma delimitted list of resource files to load.
  -->
 <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
                 /WEB-INF/validation.xml"/>
 </plug-in>

You should not have to change validator-rules.xml most of the generic
validation is here.

You need to add your forms to validation.xml here is an example:

<form-validation>
<global>
      <constant>
        <constant-name>phone</constant-name>






  <constant-value>^\(?(\d{3})\)?[-| ]?(\d{3})[-| ]?(\d{4})$</constant-value>
      </constant>
      <constant>
        <constant-name>zip</constant-name>
        <constant-value>^\d{5}\d*$</constant-value>
      </constant>
   </global>
 <formset>
      <constant>
        <constant-name>zip</constant-name>
        <constant-value>^\d{5}(-\d{4})?$</constant-value>
      </constant>
 <form    name="userForm">
    <field    property="email"
             depends="required,email">
              <arg0 key="userForm.email.displayname"/>
 </field>
 <field    property="firstname"
         depends="required,mask,minlength">
         <arg0 key="userForm.firstname.displayname"/>
         <arg1 name="minlength" key="${var:minlength}" resource="false"/>
         <var>
           <var-name>mask</var-name>
           <var-value>^\w+$</var-value>
         </var>
         <var>
           <var-name>minlength</var-name>
           <var-value>3</var-value>
         </var>
         </field>
   <field    property="lastname"
         depends="required,mask,maxlength">
           <arg0 key="userForm.lastname.displayname"/>
           <arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
                 <var>
                   <var-name>mask</var-name>
                   <var-value>^[a-zA-Z]*$</var-value>
                 </var>
                 <var>
                   <var-name>maxlength</var-name>
                   <var-value>10</var-value>
                 </var>
     </field>
   <field    property="address1"
             depends="required">
               <arg0 key="userForm.address1.displayname"/>
         </field>
   <field    property="city"
             depends="required,mask">
               <arg0 key="userForm.city.displayname"/>
                     <var>
                       <var-name>mask</var-name>
                       <var-value>^[a-zA-Z\s]*$</var-value>
                     </var>
         </field>
         <field    property="state"
             depends="required,mask">
               <arg0 key="userForm.state.displayname"/>
                     <var>
                       <var-name>mask</var-name>
                       <var-value>^[a-zA-Z]*$</var-value>
                     </var>
         </field>
   <field    property="zip"
             depends="required,mask">
               <arg0 key="userForm.zip.displayname"/>
                     <var>
                       <var-name>mask</var-name>
                       <var-value>${zip}</var-value>
                     </var>
         </field>
   <field    property="phone"
             depends="required,mask">
               <arg0 key="userForm.phone.displayname"/>
                     <var>
                       <var-name>mask</var-name>
                       <var-value>${phone}</var-value>
                     </var>
         </field>
   <field    property="password"
             depends="required,password">
              <arg0 key="userForm.password.displayname"/>
 </field>
</form>

<form name="userPasswordForm">
    <field    property="email"
             depends="required,email">
              <arg0 key="userForm.email.displayname"/>
 </field>

  <field property="distance"
    depends="required,integer">
        <arg0 key="teacherForm.distance.displayname"/>
       </field>
    <field property="rate"
    depends="required,float">
        <arg0 key="teacherForm.rate.displayname"/>
       </field>

</form>

</formset>
</form-validation>


(both the validator-rules.xml and validation.xml go in your Web-inf
directory

If you are using forms extend ValidatorForm

If you are using DynaValidatorForm use this in your form definitions in your
struts-config
type="org.apache.struts.validator.DynaValidatorForm"

In your validation.xml you have this:

<arg0 key="teacherForm.children_experience.displayname"/>

this key is used in your message resource.


This goes in your message resource file:
errors.header=<font color="#FFF000" face="Arial, Helvetica, sans-serif">You
must complete the following information before proceeding:<ul>
header will be displayed first.

The  <arg0 key="teacherForm.children_experience.displayname"/>
will be the {0} in the resource file.

These also goes in your message resource file (whatever you require):

errors.minlength=<li>{0} cannot be less than {1} characters.</li>
errors.maxlength=<li>{0} cannot be greater than {1} characters.</li>
errors.email=<li>{0} is an invalid e-mail address.</li>
errors.required=<li>{0} is required.</li>
errors.invalid=<li>{0} is invalid.</li>
errors.float=<li>{0} must be a decimal number.</li>
errors.integer=<li>{0} must be a number.</li>
errors.footer=</ul></footer</li>


Typically when I get an error (nothing happenns) it is because I had a typo
in the
validation.xml file.

I learned validation by looking at the struts-validator.war in the 1.1
Release


Hope this helps.

Barry



----- Original Message ----- 
From: <Sh...@Biometmail.com>
To: <st...@jakarta.apache.org>
Sent: Thursday, October 16, 2003 12:28 PM
Subject: Validator


> I am trying to use Struts validator and am running into some problems. Are
> there any good step-by-step instructions? I have Struts in Action and
> tried to follow the chapter concerning Validator but I didn't get the
> desired results and am not sure where to begin in tracing what I may have
> done wrong or not done that I should have.
>
>
> Thank you
> Shawn Rummel



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