| Author |
Message |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 11/08/2008 17:26:18
|
saoj
Joined: 22/12/2007 07:20:02
Messages: 22
Offline
|
A rule validates whether an action input value is valid or not to be passed along to the action. To learn about rules let's code a simple NegativeRule class that validates only negative numbers.
When you extend BasicRule you should implement two methods: check() and getTokens().
check(String value): In this method you receive the value you are suppose to validate and just do it, however you want to, returning true or false.
getTokens(): In this method you return a map with tokens and their values to be used by error messages. The default implementation in BasicRule is to return null, so you should only implement this method if you have tokens to return.
A token is used inside error messages so that it can be substituted for values associated with the rule. For example, in our simple rule above, the negative number has no minimum boundary. Let's change it to include a possible minimum:
Note that now we have the possibility to validate the input value based on a minimum number and we are returning the token "min" with whatever value is given to that minimum number. Therefore you may have an error message like the one below:
The %min% token is substituted with the value returned in the getTokens() method. The IntegerRule that comes with Mentawai also returns tokens for its min and max values. The tokens of a rule are nothing more than its properties that you may want to display along with the error messages.
There are other types of validation that are not solely based in the value itself. For example you may want to perform validation based on the user locale and you may want to perform validation of one field based on another field.
The most common example of a validation that depends on the locale is date validation. Depending on the user locale the date 12/30/2008 may be valid or not. Below is the source code of a simple rule to validate dates. Note that we are now extending LocaleRule instead of BasicRule.
Note that now the check() method receives not just the value but also the locale of the user performing the request. You should use the locale in any way you want to perform the validation. Mentawai already comes with a complete DateRule so you should not need to code a rule for dates. This was just an example for you to understand how the LocaleRule works in case you want to write your own.
The last form of common validation is when you need to compare two fields to determine whether one field is valid or not. If you thought about password and password confirmation you were right. For that we can extend the CrossRule class. Check the source below for a simple EqualRule that validates whether two fields have the same value:
When inheriting from CrossRule you should implement two abstract methods:
getFieldsToValidate() should return an array of the field names that will be compared.
check(String[] values) receives the values of the fields indicated by the getFieldsToValidate() method, so that you can perform any comparison you want to validate them.
Note that Mentawai gets the field values for you from the action input and pass them along with the check method so that you can compare them and return the validation result. Mentawai already comes with a complete EqualRule in the org.mentawai.rule package.
Another very useful built-in rule that you can use for your advantage is the RegexRule. Check below how a SocialSecurityNumberRule could be implemented by inheriting from RegexRule:
You could have also used the RegexRule with the Social Security Number pattern without the need to create a new rule for it. See below:
As you can see, by inheriting from BasicRule, LocaleRule, CrossRule and RegexRule you can perform any kind of validation for your action input values.
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 17/08/2009 20:16:12
|
josueh
![[Avatar]](/images/avatar/19ca14e7ea6328a42e0eb13d585e4c22.jpg)
Joined: 17/08/2009 20:05:55
Messages: 1
Offline
|
olá,
gosto muito do Mentawai mas, este projeto ainda está ativo ?!?!??
já finalizaram o "Mentawai In Action" em extensão PDF ?!?
olhando os posts mais recentes, parece que o projeto foi abandonado
alguém tem informações?!?
obrigado!
|
|
|
 |
|
|
|
|