Posted By:
Marian_Skalsky
Posted On:
Monday, July 8, 2002 01:57 AM
Hi there. I'm looking for
the same answer as you (probably). If you wanna use client side JavaScript validation,
this may help you.
Validation
Validation is already
a part of Struts.. it used to have a seat at when it was not: http://home.earthlink.net/~dwinterfeldt/.
Now it is also at: http://cvs.apache.org/viewcvs/jakarta-commons/validator/.
Setup
- files necessary
to setup the validator:
- validator-rules.xml
(if you don't want to define own validator you don't need to toch it)
- defines
validation rules (+their Javascript) defined in org.apache.struts.validator.util.StrutsValidator
such as:
· field is required
· specified length
· matches a regular expression
· email
· credit card
· server side type checking
· date validation
· other.. plus
you may define your own validation rules as validation plugins
- validation.xml
(here you have to define validations for your specific formbeans)
- defines
global constants used by validation plugins
- different
validation rules can be defined for different locales by seting a
formset
- each
formset defines group of properties specific for the form and their
"limitations" with specific values e.g. :
- form
bean name
- field
name
- required
(true/false)
-
mask (regular expresion)
- minlenght
(size)
- field
name
- required
(true/false)
- email
- etc.
- web.xml
- locates
validator taglibs (probably)
- struts-config.xml
- this has to be added
- Message
Resources Definition - "
- Plug
Ins Configuration -
- ApplicationResources.properties
(+language versions) - define error messages such as errors.required,
errors.minlength etc. (errors.maxlength={0} can not be greater than
{1} characters) + header and footer
- custom validation
routines can be created and added to the framework
- different validation
rules can be defined for different locales
- support for
user defined constants which can be used in some field attributes
- JavascriptValidatorTag
- used to generate javascript validations on client side, based on the validation
rules loaded by the Validator
- Usage
- use as:
- extend
org.apache.struts.validator.action.ValidatorForm instead of org.apache.struts.action.ActionForm
- form
element's name attribute in the validation.xml should match action
element's name attribute.
- or as:
- extend
the ValidatorActionForm
- action
element's 'path' attribute from the struts-config.xml which should match
the form element's name attribute in the validation.xml
Internationalization
- Validation rules
for forms can be grouped under a FormSet in the validation.xml file.
- FormSet has
language, country, and variant attributes that correspond with the java.util.Locale
class
Pluggable Validators
There are predefined validators as shown above, but you may define your own
validator. Then you define it as:
Example Validator Configuration from validation.xml.
classname="org.apache.struts.validator.util.StrutsValidatorUtil"
method="validateRequired"
msg="errors.required"/>
Have a Submit
Button in a Form not Perform Validation
If you want to turn off the validation for a specific submit button like the
html:cancel tag, you can set the bCancel variable to true for the JavaScript
validation not to execute and bCancel to false for it to execute.
onclick="bCancel=false;">
Client
side validation
I'm still not
sure about how to get JS validation work at client side.. if it is only in presence
of "onsubmit="return validateRegistrationForm(this);" in the
form tag of jsp... will see. I have downloaded older Validator from http://home.earthlink.net/~dwinterfeldt/archive/
and there is working example but with no extra comment and without source codes..
But I have already seen validation window appear at client, telling me what
should be and is not valid :)) So, it works :))
Now
I'm waiting for these questions to be answered from David Winterfeldt:
- Where can I
find some working example (+source code of Actions etc!) to see how it (the
validation) works?
- Is there any
serious tutorial about "validation in struts" ?
- Where can I
find more obout validation at jakarta home page ? (I found only: http://cvs.apache.org/viewcvs/jakarta-commons/validator/)
When answered,
I'll publish.
Hope helped a bit.
Digging around :))
$kala.