Home  >  Struts 2

@ConversionErrorFieldValidator and @DateRangeFieldValidator in Struts 2

By Arvind Rai, June 15, 2014
@ConversionErrorFieldValidator and @DateRangeFieldValidator are the struts 2 validation annotation. Both @ConversionErrorFieldValidator and @DateRangeFieldValidator works at method level. @ConversionErrorFieldValidator validates a field for type conversion and @DateRangeFieldValidator validates for date range from and to.

@ConversionErrorFieldValidator Annotation in Struts 2

Suppose we have a field to enter integer but we are entering string. @ConversionErrorFieldValidator will check for this validation and show the error message.

message : This attribute is required. This displays the error message.
key : This is I18 key which is obtained from property file.
messageParams : This is a additional param which is used to customize the message.
fieldname : This denotes the field name.
shortCircuit : Enables this validator to be used as short circuit.
type : The value of type can be like ValidatorType.FIELD.

Syntax will be as below.
@ConversionErrorFieldValidator(message = "message", key = "i18n.key.value", shortCircuit = true) 
Find the code how to use @ConversionErrorFieldValidator annotation.
public class UserAction extends ActionSupport{
	private int age;
	public String execute() {
	   return SUCCESS; 
	}
	@ConversionErrorFieldValidator(message = "Enter Inetger only.")
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
} 

@DateRangeFieldValidator Annotation in Struts 2

@DateRangeFieldValidator checks that the given date is within a given range otherwise it will display an error message.

message : This attribute is required. This displays the error message.
key : This is I18 key which is obtained from property file.
messageParams : This is a additional param which is used to customize the message.
fieldname : This denotes the field name.
shortCircuit : Enables this validator to be used as short circuit.
type : The value of type can be like ValidatorType.FIELD.
min: Minimum date that can a user enter.
max: Maximum date that a user can enter.
dateFormat: This is date format according to which min and max date will be parsed.

Syntax will be as given.
@DateRangeFieldValidator(message = "message", key = "i18n.key.value", shortCircuit = true, min = "2013/01/01", max = "2015/12/31")  
Find the code below how to use @DateRangeFieldValidator .
public class UserAction extends ActionSupport{
	private Date date;
	public String execute() {
	   return SUCCESS; 
	}
	@DateRangeFieldValidator(message = "Enter valid date.", min = "2013/01/01", max = "2015/12/31") 
	public Date getDate() {
		return date;
	}
	
	public void setDate(Date date) {
		this.date = date;
	}
	
} 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us