GeeCON Prague 2014*
The moose likes Struts and the fly likes Limits of Validator Framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Limits of Validator Framework" Watch "Limits of Validator Framework" New topic
Author

Limits of Validator Framework

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Suppose i want to check either name or SSN field is specified. How can i check this using validator framework?

validwhen also has the limitations..
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If all you want to do is check that either field is entered, that can be done quite easliy with the validwhen validation rule. Example:


You're right, though, about the fact that validwhen has some serious limitations. In this example, if you not only wanted to check that either name or SSN is entered, but also that the SSN if entered contains only digits, validwhen doesn't give you a way to do that. You'd have to write a custom validation rule.


Merrill
Consultant, Sima Solutions
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Actually the easiest way is to override the validate() and call super.validate() from it.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9399
    
    2

"You'd have to write a custom validation rule"

Then why would I even need a Validator framework explicitly??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Let's not lose sight of the fact that the validator framework was designed to handle only the simple validations. Example: is it non-blank? is it an integer? is it a valid email address?

Anything much more complicated than that requires you to write your own validation code. Example: Is the account number valid? Were at least three of the five checkboxes checked?

In answer to Jothi's question, there are a lot of good reasons for using the validation framework. The most obvious is that it provides a codeless way of performing simple validations. The fact that it also provides client-side validation is also a big plus.

In Sandeep's example, if this is a validation that occurs only once in your application, then I agree that it's easier just to put the logic in the validate() method. However, if the same validation is used multiple times in the application, it might be worth while to create a custom validation rule.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9399
    
    2

Awesome replies Merril...Thanks once again!
 
GeeCON Prague 2014
 
subject: Limits of Validator Framework