Writing Style of Rule files
In general, XWork has released two types of validators, including our custom ones.
Plain Validators/Non-Field validators
FieldValidators
The smooth validators (such as expression validators) are not bound to a specific attribute. When we define a smooth validators, we do not need to specify the attribute name.
Attribute validators (such as email validators) are based on a specific attribute and need to be in *-validation. xml specifies a fieldname attribute. there are two equivalent methods to define the property validators in XML syntax.
Non-Field Validator Vs Field-Validator
In the-validation. xml file, we can define the validators in two ways:
1 <validator>
2 <field-validator>
Remember the following syntax:
Non-property validators: Two validators can be defined in the <validator> element, whether smooth or attribute validators.
<Validator type = "expression>
<Param name = "expression"> foo gt bar </param>
<Message> foo must be great than bar. </message>
</Validator>
<Validator type = "required">
<Param name = "fieldName"> bar </param>
<Message> You must enter a value for bar. </message>
</Validator>
Property validators: The <field-validator> element inherits the fieldName attribute of the <field> element, basically the same as the <validator> element. the property validators defined in <field-validator> automatically obtain the fieldName attribute value from the fieldName attribute of the parent element <field>. this is because it is convenient to group some validators, that is, you can define multiple <field-validator> elements in a <field> element, all these property validators verify this property. if this is not done, you must specify the fieldName attribute in each <validator> element.
Note:
Generally, defining attribute validators in <field> elements is better than defining and specifying attribute names in <validator>, because this method is clear and clear.
Use field-validator to define an attribute validator:
[Html]
<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 following two methods are equivalent:
<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>
You can choose one by yourself.