Making Fields Required

 < Day Day Up > 



To round out our example, let's add two fields to our form, namely, a first name and a last name field. Let's say that all you want is text in these fields; you don't care if it matches any pattern—just that it is present.

You have to add the bean properties to your Action form for firstName and lastName, and then add labels for these fields to your resource bundle. Once you have done that, you can make the fields required by adding them to rule associations in the form we have been working on:

               <field property="firstName"                             depends="required">                      <arg0 key="inputForm.firstName" />               </field>               <field property="lastName"                             depends="required">                      <arg0 key="inputForm.lastName" />               </field> 

Notice that the required rule takes no arguments. The required rule is likely the rule you will use the most.

start sidebar

It would be a good idea to restrict the fields lastName and firstName to letters only. You can do this by using the mask rule.

end sidebar

To make the birth date field required, use this code:

                 <field property="birthDate"                             depends="required,date">                      <arg0 key="inputForm.birthDate" />                     <var>                        <var-name>datePattern</var-name>                        <var-value>MM-dd-yyyy</var-value>                     </var>                 </field> 

Simply add this code before the rules list (specified by the depends attribute of the field element). If the field is required, then you must use the required rule.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net