Multiple Custom Validators

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple Custom Validators

James Daugherty
Hi All,

I have a few closures that I've reused as custom validators and I've run across an instance where it would be nice to have 2 different custom validators for a single property on a command/domain object.   These validations are specific to a package so I would rather not create a custom constraint that any one else can use in the application. I searched around for adding multiple custom validators for one property and one recommendation has been to create a wrapper closure that calls the ones you want.   i.e. something like ...

def foo = { val, obj -> /* validation here */ return null }
def bar = { val, obj -> /* validation here */ return null }


class SomeCommand {
     String someValue

     static constraints = {
           someValue( validator: { val, obj, errors ->
                 def fooValue = foo.call(val,obj)
                 if(fooValue) {
                      errors.rejectValue(fooValue)
                 }

                 def barValue = bar.call(val,obj)
                 if(barValue) {
                       errors.rejectValue(barValue)
                 }
           );
     }
}


This doesn't seem very DRY since I'd have to create a wrapper closure any where I wanted to do this.  Do people think the right path is creating a custom validators constraint, that takes a list of validators to call & otherwise functions like the built in validator constraint?  Is there a reason that the built in custom validator constraint doesn't support a list of validators instead of a single one?  Locally, I created a ValidatorsConstraint that is essentially the built in ValidatorConstraint, except it takes a list as well.  Do people think I should contribute this as a patch to Grails or create a separate plugin with this feature?  Thoughts?

Regards,
James
Reply | Threaded
Open this post in threaded view
|

Re: Multiple Custom Validators

Nathan Wells
Personally, I would recommend a plugin, if possible. I've never come across this need before, and doubt that you could add it to grails core in such a way that would not add complexity for those of us who don't need the feature.

But, I'm not an expert on validation. Maybe patching Grails is the right way to go.

Nathan Wells


On Tue, Nov 13, 2012 at 1:44 PM, James Daugherty <[hidden email]> wrote:
Hi All,

I have a few closures that I've reused as custom validators and I've run across an instance where it would be nice to have 2 different custom validators for a single property on a command/domain object.   These validations are specific to a package so I would rather not create a custom constraint that any one else can use in the application. I searched around for adding multiple custom validators for one property and one recommendation has been to create a wrapper closure that calls the ones you want.   i.e. something like ...

def foo = { val, obj -> /* validation here */ return null }
def bar = { val, obj -> /* validation here */ return null }


class SomeCommand {
     String someValue

     static constraints = {
           someValue( validator: { val, obj, errors ->
                 def fooValue = foo.call(val,obj)
                 if(fooValue) {
                      errors.rejectValue(fooValue)
                 }

                 def barValue = bar.call(val,obj)
                 if(barValue) {
                       errors.rejectValue(barValue)
                 }
           );
     }
}


This doesn't seem very DRY since I'd have to create a wrapper closure any where I wanted to do this.  Do people think the right path is creating a custom validators constraint, that takes a list of validators to call & otherwise functions like the built in validator constraint?  Is there a reason that the built in custom validator constraint doesn't support a list of validators instead of a single one?  Locally, I created a ValidatorsConstraint that is essentially the built in ValidatorConstraint, except it takes a list as well.  Do people think I should contribute this as a patch to Grails or create a separate plugin with this feature?  Thoughts?

Regards,
James