Wednesday, February 15, 2012

Validate whole numbers

Simple way to do this is to add an af:validateRegExp to your af:inputText field, e.g.:
<af:validateRegExp pattern="^[1-9]+[0-9]*$"
                               messageDetailNoMatch="Value must be specified in whole number format."/>


NOTE:  when you drag n drop a number field from your view object ont a page as an af:inputText component, it adds an af:convertNumber by default.  In this case your RegExp validator will not fire as the number conversion will happen first.  This is fine to catch Alphabetic characters, but fractions will simply be rounded as per the rules specified in the af:convertNumber and no message will be raised for the user.  So if you want to validate via RegExp, delete the af:convertNumber .

A further addition to this post.  If the number field is based on a database Number column, you might want to consider specifying a precision on the column in the database and then synchronize those changes to your Entity Object in ADF.  You will then see a new Database Constraint validation added for the attribute in your Entity Object (consider adding a user friendly error message to the validation at this point).

Alternatively, if the inputText component is bound to a View Object or Entity Object, and no precision is specified on the database, you could add a RegExp validation to the attribute on the model layer.

No comments:

Post a Comment