Suppose I have a bean which has lets say 10 properties. Also I have a error onject which has three propertes viz. errorFlag, errCd, and errMsg And I want to validate that bean properties sequentialy and I want to break on first occcurence of error and populate the error object. How can I do that, I want to have minimum If. else if statements.
ErrorObj Bean
public class ErrObj{
private String errorMessage;
private String errorCode;
private boolean errorFlag;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public boolean isErrorFlag() {
return errorFlag;
}
public void setErrorFlag(boolean errorFlag) {
this.errorFlag = errorFlag;
}
}
Below is the class where I am validating the Bean Assume, im using some static methods of Custom util class which return boolean depending upon if a field is valid or not.
public class ValidateMyBean{
ErrObj errObj = new ErrObj();
if(!CustomUtils.isValid(myBean.getProp1))
{
// set some error conditions
errObj.errorCode("123");
errobj.setErrorMessage("validation 1 Failed");
}
else if(!CustomUtils.isValid(myBean.getProp2))
{
}
..........so on
}
So, if my bean has 10 properties and I have to validate each property against lets say 3 validator methods, I don't want to write multiple "if - elseif" statements or write mutliple "if" statements with return statement at the end of each if statement, in case of any error. Is there any cleanser way or there are some frameworks which can help me reach the desired result? PS:This Java application is using Spring Core framework and is a webservice application and not a MVC application.