You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Thom Burnett <th...@gmail.com> on 2006/12/15 17:00:20 UTC

Validation rule writing

I'm trying to validate two fields - area code and phone number (as nnn-nnnn
no areacode in that input).

Various samples and reading suggest that I should be able to create a mask
and apply it as below but my result is that the area code can't be empty but
it can contain letters, be longer than 3. It's ignoring my regular
expression mask.

Am I missing something simple in how to setup and use the validations.xml or
am I going about this in the wrong way?


<?xml version='1.0' encoding='windows-1252'?>
<form-validation>
  <global>
     <constant>
           <constant-name>phoneFormatMask</constant-name>
           <constant-value>^[0-9]{3}-[0-9]{4}$</constant-value>
     </constant>
     <constant>
           <constant-name>areaCodeFormatMask</constant-name>
           <constant-value>^[0-9]{3}$</constant-value>
     </constant>
  </global>
  <formset>
    <!-- Start of validation rules for each bean -->
    <form name="contactBean">
      <field property="firstName" depends="required" page="1">
         <arg0 key="validation.error.firstname"/>
       </field>
       <field property="lastName" depends="required" page="1">
         <arg0 key="validation.error.last"/>
       </field>
       <field property="email" depends="required, email" page="1">
         <arg0 key="validation.error.email"/>
       </field>
       <field property="areaCode" depends="required, areaCodeMask" page="1">
         <arg0 key="validation.error.areaCode"/>
          <msg name="areaCodeMask" key="validation.error.areaCode.format"/>
           <var>
             <var-name>areaCodeMask</var-name>
             <var-value>${areaCodeFormatMask}</var-value>
           </var>
       </field>
       <field property="phoneNumber" depends="required, phoneMask" page="1">
          <arg0 key="validation.error.phone"/>
          <var>
            <var-name>phoneMask</var-name>
            <var-value>${phoneFormatMask}</var-value>
          </var>
       </field>
...
</form>
</formset>
</form-validation>

Re: Validation rule writing

Posted by Laurie Harper <la...@holoweb.net>.
What Chris said, plus this:

   <field property="areaCode" depends="required, areaCodeMask" ...

there's no validation rule named 'areaCodeMask' or 'phoneMask'; you want 
just 'mask' as the rule name. See the validator documentation for examples.

Also, is validation working at all for you? For example, is it correctly 
detecting errors for the properties you have marked with just the 
'required' rule? If not, start by figuring that out, then worry about 
getting your masks applied correctly.

L.

Thom Burnett wrote:
> I'm trying to validate two fields - area code and phone number (as nnn-nnnn
> no areacode in that input).
> 
> Various samples and reading suggest that I should be able to create a mask
> and apply it as below but my result is that the area code can't be empty 
> but
> it can contain letters, be longer than 3. It's ignoring my regular
> expression mask.
> 
> Am I missing something simple in how to setup and use the 
> validations.xml or
> am I going about this in the wrong way?
> 
> 
> <?xml version='1.0' encoding='windows-1252'?>
> <form-validation>
>  <global>
>     <constant>
>           <constant-name>phoneFormatMask</constant-name>
>           <constant-value>^[0-9]{3}-[0-9]{4}$</constant-value>
>     </constant>
>     <constant>
>           <constant-name>areaCodeFormatMask</constant-name>
>           <constant-value>^[0-9]{3}$</constant-value>
>     </constant>
>  </global>
>  <formset>
>    <!-- Start of validation rules for each bean -->
>    <form name="contactBean">
>      <field property="firstName" depends="required" page="1">
>         <arg0 key="validation.error.firstname"/>
>       </field>
>       <field property="lastName" depends="required" page="1">
>         <arg0 key="validation.error.last"/>
>       </field>
>       <field property="email" depends="required, email" page="1">
>         <arg0 key="validation.error.email"/>
>       </field>
>       <field property="areaCode" depends="required, areaCodeMask" page="1">
>         <arg0 key="validation.error.areaCode"/>
>          <msg name="areaCodeMask" key="validation.error.areaCode.format"/>
>           <var>
>             <var-name>areaCodeMask</var-name>
>             <var-value>${areaCodeFormatMask}</var-value>
>           </var>
>       </field>
>       <field property="phoneNumber" depends="required, phoneMask" page="1">
>          <arg0 key="validation.error.phone"/>
>          <var>
>            <var-name>phoneMask</var-name>
>            <var-value>${phoneFormatMask}</var-value>
>          </var>
>       </field>
> ....
> </form>
> </formset>
> </form-validation>
> 


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


Re: Validation rule writing

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thom,

Thom Burnett wrote:
> I'm trying to validate two fields - area code and phone number (as nnn-nnnn
> no areacode in that input).
>
> [snip]
>
>           <constant-name>phoneFormatMask</constant-name>
>           <constant-value>^[0-9]{3}-[0-9]{4}$</constant-value>

This regexp looks good. Have you checked the docs for the regexp engine
used by the validator (is it Jakarta-ORO?) to make sure that the syntax
you are using is correct? You may have to use [0-9][0-9][0-9] instead of
[0-9]{3}, for instance.

>          <var>
>            <var-name>phoneMask</var-name>
>            <var-value>${phoneFormatMask}</var-value>
>          </var>

Have you tried putting the mask directly into the <var-value> element?
This would help you verify that your mask is working, even if your
configuration isn't 100% correct (i.e. the use of the 'constant' fields
might not be working).

Hope that helps,
- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFgtQE9CaO5/Lv0PARAu/yAKCMR7YwcuX+BBQ+/D7DX/e04dS+0QCeLlSz
9/TG4qqBX7xgXJW8YKPKX68=
=L1kC
-----END PGP SIGNATURE-----

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