...
Non-Field Validator Vs Field-Validator
Anchor |
---|
| validatortypes |
---|
| validatortypes |
---|
|
Wiki Markup |
---|
{snippet:id=validatorVsFieldValidators1|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java} |
Wiki Markup |
---|
{snippet:id=nonFieldValidatorUsingValidatorSyntax|lang=xml|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java} |
Wiki Markup |
---|
{snippet:id=fieldValidatorUsingValidatorSyntax|lang=xml|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java} |
Wiki Markup |
---|
{snippet:id=validatorVsFieldValidators2|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java} |
Wiki Markup |
---|
{snippet:id=fieldValidatorUsingFieldValidatorSyntax|lang=xml|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java} |
Wiki Markup |
---|
{snippet:id=validatorVsFieldValidators3|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java} |
There are two ways you can define validators in your -validation.xml file:
- <validator>
- <field-validator>
Keep the following in mind when using either syntax:
Non-Field-Validator: The <validator> element allows you to declare both types of validators (either a plain Validator a field-specific FieldValidator).
Code Block |
---|
|
<validator type="expression>
<param name="expression">foo gt bar</param>
<message>foo must be great than bar.</message>
</validator>
|
Code Block |
---|
|
<validator type="required">
<param name="fieldName">bar</param>
<message>You must enter a value for bar.</message>
</validator>
|
field-validator: The <field-validator> elements are basically the same as the <validator> elements except that they inherit the fieldName attribute from the enclosing <field> element. FieldValidators defined within a <field-validator> element will have their fieldName automatically filled with the value of the parent <field> element's fieldName attribute. The reason for this structure is to conveniently group the validators for a particular field under one element, otherwise the fieldName attribute would have to be repeated, over and over, for each individual <validator>.
Tip |
---|
It is always better to defined field-validator inside a <field> tag instead of using a <validator> tag and supplying fieldName as its param as the xml code itself is clearer (grouping of field is clearer) |
Note |
---|
Note that you should only use FieldValidators (not plain Validators) within a block. A plain Validator inside a <field> will not be allowed and would generate error when parsing the xml, as it is not allowed in the defined dtd (xwork-validator-1.0.2.dtd) |
Declaring a FieldValidator using the <field-validator> syntax:
Code Block |
---|
|
<field name="email_address">
<field-validator type="required">
<message>You cannot leave the email address field empty.</message>
</field-validator>
<field-validator type="email">
<message>The email address you entered is not valid.</message>
</field-validator>
</field>
|
The choice is yours. It's perfectly legal to only use elements without the elements and set the fieldName attribute for each of them. The following are effectively equal:
Code Block |
---|
|
<field name="email_address">
<field-validator type="required">
<message>You cannot leave the email address field empty.</message>
</field-validator>
<field-validator type="email">
<message>The email address you entered is not valid.</message>
</field-validator>
</field>
<validator type="required">
<param name="fieldName">email_address</param>
<message>You cannot leave the email address field empty.</message>
</validator>
<validator type="email">
<param name="fieldName">email_address</param>
<message>The email address you entered is not valid.</message>
</validator>
|
Wiki Markup |
---|
{snippet:id=similarVaidatorDeclaredInDiffSyntax|lang=xml|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java} |
Short-Circuiting Validator
...