If you need to add validation rule to programmatically created Entity object or you just have to add some rules to the entity's attribute "on the fly", you should use implementing classes of JboValidatorInterface. ADF Model's Java API has appropriate implementing class for each validation type. If you need something "special", you can create your own implementation of JboValidationInterface.
In this post I'm going to show a couple of examples of using JboValidator classes.
The following method creates validator to control the maximum length of some entity's attribute:
In this post I'm going to show a couple of examples of using JboValidator classes.
The following method creates validator to control the maximum length of some entity's attribute:
private JboLengthValidator getLengthValidator(Integer maxLength) { //Create validator JboLengthValidator jlv = new JboLengthValidator(); //Set operation type jlv.setOperType(jlv.LESSTHANEQUALTO); //Set the value to compare with jlv.setRhsValue(maxLength); //Refer to error message in resource bundle jlv.setErrorMsgId(MAX_LENGTH_ERROR); //Populate parameter value for error message HashMap errvaluesMap = new HashMap(); errvaluesMap.put("0", maxLength.toString()); jlv.setErrorMsgExpressions(errvaluesMap); return jlv; } //The key in your resource bundle properties file. // It contains error message like this: // maxlength_err=The maximum length is {0} characters private static final String MAX_LENGTH_ERROR="maxlength_err";
The next validator is responsible for checking minimum value of some attribute:
private JboCompareValidator getMinValueValidator(Integer minValue) { JboCompareValidator jcv = new JboCompareValidator(false, JboCompareValidator.GREATERTHANEQUALTO, minValue); jcv.setErrorMsgId(MIN_VALUE_ERROR); HashMap errvaluesMap = new HashMap(); errvaluesMap.put("0", minValue.toString()); jcv.setErrorMsgExpressions(errvaluesMap); return jcv; } //minvalue_err=The minimum value is {0} private static final String MIN_VALUE_ERROR="minvalue_err";
And at last the piece of code to add the validators to some attribute:
private void addValidators(AttributeDefImpl at, Integer maxLength, Integer minValue) { if (maxLength!=0) at.addValidator(getLengthValidator(maxLength)); if (minValue!=0) at.addValidator(getMinValueValidator(minValue)); }
That's it!
No comments:
Post a Comment
Post Comment