The date range field validator checks if a Date field falls within a specific range. By default, the input format for the date in a text field is of DateFormat.SHORT and depends on user’s current locale, for example:
Using this validator in XML or annotation as follows:
NOTES: This validator does not perform date range check if the field is empty, so it’s recommended to use this validator in conjunction with the required string validator.
<field name="fieldName"> <field-validator type="date"> <param name="param name">param value</param> <message>validation error message</message> </field-validator> </field>
<validator type="date"> <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). | |
min | No | The minimum date range to check. It won’t be checked if omitted. |
max | No | The maximum date range to check. It won’t be checked if omitted. |
minExpression | No | OGNL expression used to obtain the minimum date value. |
maxExpression | No | OGNL expression used to obtain the maximum date value. |
parse | No | Boolean value. If set to true, the minExpressionand maxExpression will be evaluated to obtain the minimum/maximum date. Default is true. |
<field name="birthday"> <field-validator type="date"> <param name="min">1/1/1960</param> <param name="max">12/31/1995</param> <message>Please enter your birthday between ${min} and ${max}</message> </field-validator> </field>
<field name="birthday"> <field-validator type="date"> <param name="min">1/1/1990</param> <message>Your birth date must be after ${min}</message> </field-validator> </field>
<field name="birthday"> <field-validator type="date"> <param name="minExpression">${minDate}</param> <param name="maxExpression">${maxDate}</param> <message>Please enter your birthday between ${min} and ${max}</message> </field-validator> </field>
In this case, we it requires implementing the following methods in the action or JavaBean class as follows:
public Date getMinDate() throws ParseException { DateFormat dateFormat = new SimpleDateFormat("M/d/yy"); return dateFormat.parse("1/18/70"); } public Date getMaxDate() throws ParseException { DateFormat dateFormat = new SimpleDateFormat("M/d/yy"); return dateFormat.parse("11/22/90"); }
<validator type="date"> <param name="fieldName">birthday</param> <param name="min">1/1/1960</param> <param name="max">12/31/1995</param> <message>Please enter your birthday between ${min} and ${max}</message> </validator>
Usage: Annotate the field’s setter method or action method by the @DateRangeFieldValidatoras follows:
@DateRangeFieldValidator(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). |
min | No | The minimum date to check the field value. | |
minExpression | No | OGNL expression used to obtain the minimum date value. | |
max | No | The maximum date to check the field value. | |
maxExpression | No | OGNL expression used to obtain the maximum date value. | |
dateFormat | No | Specify the format used to parse the dates specified by the min/max parameters. |
@DateRangeFieldValidator( min = "1/1/60", max = "12/31/95", message = "Please enter your birthday between ${min} and ${max}" ) public void setBirthday(Date birthday) { this.birthday = birthday; }
@DateRangeFieldValidator( min = "1/1/60", max = "12/31/95", message = "Default message", key = "form.validation.birthday" ) public void setBirthday(Date birthday) { this.birthday = birthday; }
@DateRangeFieldValidator( min = "01/01/1980", max = "31/12/1997", dateFormat = "dd/MM/yyyy", message = "Please enter your birthday between ${min} and ${max}" ) public void setBirthday(Date birthday) { this.birthday = birthday; }
@DateRangeFieldValidator( type = ValidatorType.SIMPLE, fieldName = "birthday", min = "1/1/60", max = "12/31/95", message = "Please enter your birthday between ${min} and ${max} LOL" ) public String execute() { return SUCCESS; }