Ramandeep Singh Nanda
Published

Wed 22 January 2014

←Home

Doing entity validations at runtime

I was recently given a requirement to drive the entity attribute validations at runtime rather than at design time, with the following criteria.

  1. Ability to change fields to required at runtime.
  2. To be able to define validation message for field at runtime
  3. Define validations such as field must be numeric, or contain only alphabets (Used Regular expression for this)
  4. Localization of validation messages

To accomplish this we can use database dictionary views along with custom database tables to store the validations and perform validations at runtime. The data model diagram is shown below. DEMO_RUNTIME_VALThe key thing then is to use database dictionary views along with these database tables to validate attributes of the entity for a pattern (regular expression). The class that contains the method to perform validations is CustomEntityImpl and each entity that one wants to enable for runtime validation must extend this class and call its validateCustomEntity method in its entity method validator.

In the sample application i have enabled a required and pattern constraint as shown in the following screenshots for dummy entity object’s attribute. runtimevalidation_defineruntimevalidation_define_messagesruntimevalidation_define_1 runtimevalidationerror The application source and sql script can be downloaded from the below mentioned link. Source Code Note: If you want to test the application you must create tables under user “raman”. Edit: To skip validation for existing attribute that has not been modified you can use the following snippet.

1
2
3
4
Object attribValue=getAttribute(attrName);
int attribIndex=getAttributeIndexOf(attrName);
Object oldAttribValue=getPostedAttribute(attribIndex);
if(attribValue==null || !attribValue.equals(oldAttribValue)){
1
2
3
4
ArrayList<JboException> list=new ArrayList<JboException>(2);
if(attribRequired.equalsIgnoreCase("Y"))
{
....................

Go Top
comments powered by Disqus