The conversion validator is a Struts field validator that checks if a type conversion error occurs for a field. For example, if the user inputs the value “one” for an integer field, a conversion error will occur, and an error message will be displayed next to the field. This validator can be used in either of the following forms:
<field name="fieldName"> <field-validator type="conversion"> <message>conversion error message</message> </field-validator> </field>
<validator type="conversion"> <param name="fieldName">myField</param> <message>conversion error message</message> </validator>
Parameter name | Description |
fieldName | Name of the field to validate. Required if using plain validator syntax. |
<field name="myAge"> <field-validator type="conversion"> <message>Please enter a number for your age!</message> </field-validator> </field>
<validator type="conversion"> <param name="fieldName">myAge</param> <message>Age must be a number!</message> </validator>
<field name="myAge"> <field-validator type="conversion"> <param name="repopulateField">true</param> <message>Please enter a number for your age!</message> </field-validator> </field>
Usage: Put the @ConversionErrorFieldValidatorannotation before the field’s setter/getter method or action method (in case of using plain-validator) in the following form:
@ConversionErrorFieldValidator (param1 = "param 1 value", param2 = "param 2 value", ...)
Parameter name | Required | Default value | Description |
message | Yes | validation error message. | |
key | No | i18n key for validation error message. | |
messageParams | No | Additional parameters to customize the message. | |
fieldName | No | Specifies field name in case this validator type is plain-validator. | |
shortCircuit | No | false | Whether this validator is short circuit. |
type | No | ValidatorType.FIELD | type of the validator: field-validator (FIELD) or plain-validator (SIMPLE). |
@ConversionErrorFieldValidator( message = "Age must be a number (integer wanted!)" ) public int getMyAge() { return myAge; }
@ConversionErrorFieldValidator( key = "form.validation.age", message = "Age must be a number (integer wanted!)" // default message ) public void setMyAge(int myAge) { this.myAge = myAge; }
@ConversionErrorFieldValidator( type = ValidatorType.SIMPLE, fieldName = "myAge", message = "Age must be a number (integer wanted!)" )
Related Struts Form Validation Tutorials: