<field name="fieldName"> <field-validator type="stringlength"> <param name="param name">param value</param> <message>validation error message</message> </field-validator> </field>
<validator type="stringlength"> <param name="param name">param value</param> <message>validation error message</message> </validator>
Parameter name | Required | Description |
fieldName | Name of the field (required if using plain validator syntax). | |
minLength | No | An integer number specifies minimum length of the field value. |
maxLength | No | An integer number specifies maximum length of the field value. |
trim | No | Bolean value. If set to true, trim the field value before validating its length. Default is true. |
minLengthExpression | No | OGNL expression used to obtain the minimum length value. |
maxLengthExpression | No | OGNL expression used to obtain the maximum length value. |
trimExpression | No | OGNL expression used to obtain the trim flag. |
<field name="password">
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">12</param>
<message>Your password must have from ${minLength} to ${maxLength} characters</message>
</field-validator>
</field><field name="password">
<field-validator type="stringlength">
<param name="minLength">6</param>
<message>Your password must have at least ${minLength} characters</message>
</field-validator>
</field><field name="password">
<field-validator type="stringlength">
<param name="minLength">12</param>
<param name="maxLength">12</param>
<message>Your password must contain exactly ${maxLength} characters</message>
</field-validator>
</field><field name="password">
<field-validator type="stringlength">
<param name="minLengthExpression">${minLengthValue}</param>
<param name="maxLengthExpression">${maxLengthValue}</param>
<message>Your password must have from ${minLength} to ${maxLength} characters</message>
</field-validator>
</field>In this case, we have to implement two methods getMinLengthValue() and getMaxLengthValue() in the action or JavaBean class as follows:public int getMinLengthValue() {
return 6;
}
public int getMaxLengthValue() {
return 12;
} <validator type="stringlength">
<param name="fieldName">password</param>
<param name="minLength">6</param>
<param name="maxLength">12</param>
<message>Your password must have from ${minLength} to ${maxLength} characters</message>
</validator> @StringLengthFieldValidator(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). |
minLength | No | The minimum length to check the field value. | |
minLengthExpression | No | OGNL expression used to obtain the minimum length value. | |
maxLength | No | The maximum length to check the field value. | |
maxLengthExpression | No | OGNL expression used to obtain the maximum length value. | |
trim | No | true | Whether to evaluate trim the field value before validating. |
trimExpression | No | OGNL expression used to obtain the trim flag. |
@StringLengthFieldValidator(
minLength = "6",
maxLength = "12",
message = "Your password must have from ${minLength} to ${maxLength} characters"
)
public void setPassword(String password) {
this.password = password;
} @StringLengthFieldValidator(
minLength = "6",
maxLength = "12",
message = "Default message",
key = "form.validation.password"
)
public void setPassword(String password) {
this.password = password;
} @StringLengthFieldValidator(
type = ValidatorType.SIMPLE,
fieldName = "password",
minLength = "6",
maxLength = "12",
message = "Your password must have from ${minLength} to ${maxLength} characters"
)
public String execute() {
return SUCCESS;
}
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.