These are the standard validators that come with XWork:
All the example XML validation rule snippets below have been put together into one big example here. RequiredFieldValidatorThis validator checks that a field is non-null. Example: <validators> <field name="user"> <field-validator type="required"> <message>You must enter a value for user.</message> </field-validator> </field> </validators> RequiredStringValidatorThis validator checks that a String field is not empty (i.e. non-null with a length > 0).
Example: <validators> <field name="userName"> <field-validator type="requiredstring"> <message>You must enter an username.</message> </field-validator> </field> </validators> StringLengthFieldValidatorThis validator checks that a String field is of the right length. It assumes that the field is a String.
If neither minLength nor maxLength is set, nothing will be done. Example: <validators> <field name="userName"> <field-validator type="stringlength"> <param name="minLength">3</param> <param name="maxLength">10</param> <message>Username must be between ${minLength} and ${maxLength} characters long.</message> </field-validator> </field> </validators> StringRegexValidatorThis validator checks that a String field matches a configure Regular Expression, if it is not an empty String.
Example: <validators> <field name="coords"> <field-validator type="regex"> <param name="regex">[0-9],[0-9]</param> <message>The value of coords must be in the format "x, y", where x and y are between 0 and 9</message> </field-validator> </field> </validators> EmailValidatorThis validator checks that a field is a valid e-mail address if it contains a non-empty String. Example: <validators> <field name="email"> <field-validator type="email"> <message>You must enter a valid email address.</message> </field-validator> </field> </validators> URLValidatorThis validator checks that a field is a valid URL. Example: <validators> <field name="homepage"> <field-validator type="url"> <message>You must enter a valid URL.</message> </field-validator> </field> </validators> IntRangeFieldValidatorThis validator checks that a numeric field has a value within a specified range.
If neither min nor max is set, nothing will be done. Example: <validators> <field name="age"> <field-validator type="int"> <param name="min">0</param> <param name="max">100</param> <message>Not a valid age!</message> </field-validator> </field> </validators> DateRangeFieldValidatorThis validator checks that a date field has a value within a specified range.
If neither min nor max is set, nothing will be done. Example: <validators> <field name="startDate"> <field-validator type="date"> <param name="min">12/22/2002</param> <param name="max">12/25/2002</param> <message>The date must be between 12-22-2002 and 12-25-2002.</message> </field-validator> </field> </validators> ConversionErrorFieldValidatorThis validator checks if there are any conversion errors for a field and applies them if they exist. See Type Conversion Error Handling for details. Example: <validators> <field name="startDate"> <field-validator type="conversion"> <message>Could not convert input to a valid date.</message> </field-validator> </field> </validators> ExpressionValidatorThis validator uses an OGNL expression to perform its validation. The error message will be added to the action if the expression returns false when it is evaluated against the value stack.
Example: <validator type="expression"> <param name="expression">foo > bar</param> <message default="Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}."/> </validator> FieldExpressionValidatorThis validator uses an OGNL expression to perform its validation. The error message will be added to the field if the expression returns false when it is evaluated against the value stack.
Example: <validators> <field name="homepage"> <field-validator type="fieldexpression"> <param name="expression">homepage.indexOf('opensymphony.com') == -1</param> <message>Please provide an OpenSymphony website</message> </field-validator> </field> </validators> VisitorFieldValidatorThe validator allows you to forward validation to object properties of your action using the objects own validation files. This allows you to use the ModelDriven development pattern and manage your validations for your models in one place, where they belong, next to your model classes. The VisitorFieldValidator can handle either simple Object properties, Collections of Objects, or Arrays. The error message for the VisitorFieldValidator will be appended in front of validation messages added by the validations for the Object message.
Example: <validators> <field name="user"> <field-validator type="visitor"> <param name="context">anotherContext</param> <message>user: </message> </field-validator> </field> </validators> Here we see the context being overridden in the validator mapping, so the action alias context will not be propogated. ModelDriven example: <validators> <field name="model"> <field-validator type="visitor"> <param name="appendPrefix">false</param> <message /> </field-validator> </field> </validators> This will use the model's validation rules and any errors messages will be applied directly (nothing is prefixed because of the empty message). |