Recently I came across the following link:
and tried to test it myself, there were some issue but it worked out eventually.
I modified the requirements to achieve the following:
a. add an expression validator at runtime with custom message.
b. remove the added validator at runtime.
here are the detailed steps to do it:
- create a new ADF application.
- in the model project create new business components from tables - will use the employees table from the HR schema.
- open the entity and generate :EntityObjectImpl and EntityDefImpl
- in the entityDefImpl add the following code:
String addExpressionValidator(String attributeName, String groovyExpression,
String errorMessage) {
AttributeDef attributeDef = findAttributeDef(attributeName);
JboExpressionValidator jboExpressionValidator =
new JboExpressionValidator(false, groovyExpression);
jboExpressionValidator.setErrorMsgId("dynamic_Message");
Map map = new HashMap();
map.put("0", errorMessage);
jboExpressionValidator.setErrorMsgExpressions(map);
((AttributeDefImpl)attributeDef).addValidator(jboExpressionValidator);
jboExpressionValidator.setName(jboExpressionValidator.getName()+System.currentTimeMillis());
return jboExpressionValidator.getName();
}
void removeExpressionValidator(String attributeName,
String validatorName) {
AttributeDefImpl attributeDef =
(AttributeDefImpl)findAttributeDef(attributeName);
List validators = attributeDef.getValidators();
JboAbstractValidator targetValidator = null;
for (Object _validator : validators) {
targetValidator = (JboAbstractValidator)_validator;
if (targetValidator.getName().equalsIgnoreCase(validatorName)) {
break;
}
}
attributeDef.removeValidator(targetValidator);
}
String errorMessage) {
AttributeDef attributeDef = findAttributeDef(attributeName);
JboExpressionValidator jboExpressionValidator =
new JboExpressionValidator(false, groovyExpression);
jboExpressionValidator.setErrorMsgId("dynamic_Message");
Map map = new HashMap();
map.put("0", errorMessage);
jboExpressionValidator.setErrorMsgExpressions(map);
((AttributeDefImpl)attributeDef).addValidator(jboExpressionValidator);
jboExpressionValidator.setName(jboExpressionValidator.getName()+System.currentTimeMillis());
return jboExpressionValidator.getName();
}
void removeExpressionValidator(String attributeName,
String validatorName) {
AttributeDefImpl attributeDef =
(AttributeDefImpl)findAttributeDef(attributeName);
List validators = attributeDef.getValidators();
JboAbstractValidator targetValidator = null;
for (Object _validator : validators) {
targetValidator = (JboAbstractValidator)_validator;
if (targetValidator.getName().equalsIgnoreCase(validatorName)) {
break;
}
}
attributeDef.removeValidator(targetValidator);
}
5.in the bundle used for the messages of the model add the following:
dynamic_Message={0}
6. expose the above method as a viewobject method.
7, design the apge as you see fit
now at runtime
as you can see I added the folloing:
attribute name:Salary
Expression:newValue<=oldValue*1.5
message: "new salary value("+newValue+") should be less than or equal to old salary value("+oldValue+"*1.5)"
please note the message, the adf processes this message as a groovy expression.
No comments:
Post a Comment