Custom Validators

19 views
Skip to first unread message

Dan Vega

unread,
Mar 16, 2011, 9:22:55 AM3/16/11
to hyrule-v...@googlegroups.com
I just wanted to ask a quick question about custom validators. At some point sooner or later  the built in validators are not going to do the job. In the 1st iteration of this project I found myself needing to write custom constraints for unique values. I think we may have a way to solve that particular problem in the new version. Outside of unique I would like to get an idea 
of type of custom validators people are writing? 

The idea going forward is you can pass a validator property which will basically be a method in the model like below but I haven't figured out if this always going to work yet or not. That is why I could really use some feedback on the things you are trying to validate that the built in rules can not accomplish.

// model constraints
this.constraints = [
{property="firstName",blank=false},
{property="lastName",blank=false},
{property="email",blank=false,email=true,unique=true,context="create,foo,bar"},
{property="age",blank=false,size="18..30"},
{property="passwordConfirm",blank=false,validator="passwordsMatch"},
{property="type",blank=false,inList="employee,manager"}
];
// custom validators
public boolean function passwordsMatch(){
if( getPassword() == getPasswordConfirm() ){
return true;
}
return false;
}

Tony Nelson

unread,
Mar 16, 2011, 2:34:49 PM3/16/11
to Hyrule Validation Framework
If the validator is just the name of a function on the model being
validated, then it might get tricky to use the same validator on
multiple classes. Granted you could have the validator delegate its
responsibility to a shared component to perform the validation, but
that seems a little odd.

In other frameworks I've seen the validator point to a different class
that implements a common "isValid" method, which is what I believe
Hyrule used to do. I like this approach since then you might be able
to override some of Hyrule's internal validators through config if you
wanted to. For example, you want to change how email addresses are
validated to prevent people from using dummy email addresses like
'b...@example.com'.

Pseudo-code...

application.hyrule = new Hyrule();

application.hyrule.setValidators({
'email' = 'my.custom.EmailValidator'
});

Just my thoughts.

-Tony

Dan Vega

unread,
Mar 16, 2011, 2:39:02 PM3/16/11
to hyrule-v...@googlegroups.com, Tony Nelson
Those are some great points Tony! I like your idea of defining your custom validators from the start. We could allow them to define an actual type or add a whole directory to scan. 

Tony Nelson

unread,
Mar 16, 2011, 2:40:46 PM3/16/11
to Hyrule Validation Framework
Yeah and if you're using ColdSpring or something similar it would be
pretty trivial to wire in any custom validators without the rest of
your application being aware or even caring.

-Tony
Reply all
Reply to author
Forward
0 new messages