Quantcast

multiple custom validators for one property

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

multiple custom validators for one property

Matti van Aert
Hi All,

I'm trying to add multiple custom validators for one property. What is the best way to do this?

Two different messages should be returned, this solution will override message1.

startDate  (validator: { val, obj -> return obj.publishDate.before(val) && obj.closeDate.before(val) }) // message 1
startDate  (validator: { val, obj -> return obj.startDate == val && !obj.hasOrders() }) //message 2

Message1=StartDate must be after publishDate
Message2=StartDate cannot be changed because of existing orders

Thanks for any help.

--
Met vriendelijke groet,
 
Matti van Aert

Achter de Molen 45
4873 GW Etten-Leur
M: <a href="tel:%2B31%20%280%296%205499%204867" value="+31654994867" target="_blank">+31 (0)6 5499 4867

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: multiple custom validators for one property

Ian Roberts
On 11/05/2012 09:32, Matti van Aert wrote:
> Hi All,
>
> I'm trying to add multiple custom validators for one property. What is
> the best way to do this?
>
> Two different messages should be returned, this solution will override
> message1.

A validator closure can return a String which is used as part of the
message code (see the last example at
http://grails.org/doc/latest/ref/Constraints/validator.html), so put the
two tests into one closure and have them return different codes.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: multiple custom validators for one property

bksaville
In reply to this post by Matti van Aert
Combine it into one validator:

validator:{ val, obj ->
  if (obj.publishDate.before(val) && obj.closeDate.before(val))
    return "message1" // rendered as domainClassName.startDate.message1 from messages.properties
  if (obj.startDate==val && !obj.hasOrders())
    return "message2" // rendered as domainClassName.startDate.message2 from messages.properties
}

The ability to return a string and have it appended onto the messages.properties resolution makes validators able to be used for multiple validation errors.  See the documentation on validator for more info.

Hope that helps,
Brian

On Fri, May 11, 2012 at 2:32 AM, Matti van Aert <[hidden email]> wrote:
Hi All,

I'm trying to add multiple custom validators for one property. What is the best way to do this?

Two different messages should be returned, this solution will override message1.

startDate  (validator: { val, obj -> return obj.publishDate.before(val) && obj.closeDate.before(val) }) // message 1
startDate  (validator: { val, obj -> return obj.startDate == val && !obj.hasOrders() }) //message 2

Message1=StartDate must be after publishDate
Message2=StartDate cannot be changed because of existing orders

Thanks for any help.

--
Met vriendelijke groet,
 
Matti van Aert

Achter de Molen 45
4873 GW Etten-Leur
M: <a href="tel:%2B31%20%280%296%205499%204867" value="+31654994867" target="_blank">+31 (0)6 5499 4867


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: multiple custom validators for one property

Matti van Aert
Hi,

Thanks for your reply.

But if both validations are true only the first message will be returned. What is the solution to return both messages?

Regards,
Matti

2012/5/11 Brian Saville <[hidden email]>
Combine it into one validator:

validator:{ val, obj ->
  if (obj.publishDate.before(val) && obj.closeDate.before(val))
    return "message1" // rendered as domainClassName.startDate.message1 from messages.properties
  if (obj.startDate==val && !obj.hasOrders())
    return "message2" // rendered as domainClassName.startDate.message2 from messages.properties
}

The ability to return a string and have it appended onto the messages.properties resolution makes validators able to be used for multiple validation errors.  See the documentation on validator for more info.

Hope that helps,
Brian


On Fri, May 11, 2012 at 2:32 AM, Matti van Aert <[hidden email]> wrote:
Hi All,

I'm trying to add multiple custom validators for one property. What is the best way to do this?

Two different messages should be returned, this solution will override message1.

startDate  (validator: { val, obj -> return obj.publishDate.before(val) && obj.closeDate.before(val) }) // message 1
startDate  (validator: { val, obj -> return obj.startDate == val && !obj.hasOrders() }) //message 2

Message1=StartDate must be after publishDate
Message2=StartDate cannot be changed because of existing orders

Thanks for any help.

--
Met vriendelijke groet,
 
Matti van Aert

Achter de Molen 45
4873 GW Etten-Leur
M: <a href="tel:%2B31%20%280%296%205499%204867" value="+31654994867" target="_blank">+31 (0)6 5499 4867




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: multiple custom validators for one property

bksaville
Well, to keep it simple but violate DRY a little, check for the "both" case and return another message with has both in there.  Just an idea.  Not sure of the recommended way to do this actually.

On Sat, May 12, 2012 at 3:52 AM, Matti van Aert <[hidden email]> wrote:
Hi,

Thanks for your reply.

But if both validations are true only the first message will be returned. What is the solution to return both messages?

Regards,
Matti

2012/5/11 Brian Saville <[hidden email]>
Combine it into one validator:

validator:{ val, obj ->
  if (obj.publishDate.before(val) && obj.closeDate.before(val))
    return "message1" // rendered as domainClassName.startDate.message1 from messages.properties
  if (obj.startDate==val && !obj.hasOrders())
    return "message2" // rendered as domainClassName.startDate.message2 from messages.properties
}

The ability to return a string and have it appended onto the messages.properties resolution makes validators able to be used for multiple validation errors.  See the documentation on validator for more info.

Hope that helps,
Brian


On Fri, May 11, 2012 at 2:32 AM, Matti van Aert <[hidden email]> wrote:
Hi All,

I'm trying to add multiple custom validators for one property. What is the best way to do this?

Two different messages should be returned, this solution will override message1.

startDate  (validator: { val, obj -> return obj.publishDate.before(val) && obj.closeDate.before(val) }) // message 1
startDate  (validator: { val, obj -> return obj.startDate == val && !obj.hasOrders() }) //message 2

Message1=StartDate must be after publishDate
Message2=StartDate cannot be changed because of existing orders

Thanks for any help.

--
Met vriendelijke groet,
 
Matti van Aert

Achter de Molen 45
4873 GW Etten-Leur
M: <a href="tel:%2B31%20%280%296%205499%204867" value="+31654994867" target="_blank">+31 (0)6 5499 4867





Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: multiple custom validators for one property

ideasculptor


On Sat, May 12, 2012 at 10:08 AM, Brian Saville <[hidden email]> wrote:
Well, to keep it simple but violate DRY a little, check for the "both" case and return another message with has both in there.  Just an idea.  Not sure of the recommended way to do this actually.

I'm just guessing, but in a normal springMVC context, you'd pass an errors object into a validator, which could then reject a field as many times as it wanted.  Is there perhaps a variant of the validator closure which receives an errors object as an input param that you could then call reject(messageId) on rather than returning a message id?  If such a mechanism isn't currently possible, that would at least be a way to support it in future, assuming that grails is using the Errors interface under the hood.  It actually fits quite nicely, since the Errors implementation can be set up with the context of which field is currently being validated prior to calling the closure.  I'd look to see if there is already a mechanism for that in place, but not documented.
 
Ha! Score one for programming by convention! I just looked at the docs and, sure enough, not only does it do exactly as I suggested but it is even documented.  Just add a third param to the closure to receive the Errors instance.

From the docs:

"A custom validator is implemented by a Closure that takes up to three parameters. If the Closure accepts zero or one parameter, the parameter value will be the one being validated ("it" in the case of a zero-parameter Closure). If it accepts two parameters the first is the value and the second is the domain class instance being validated. This is useful when your validation needs access to other fields, for example when checking that two entered passwords are the same. If it accepts three parameters the first is the value, the second is the instance, and the third is the Spring Errors object."

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: multiple custom validators for one property

bond_
Anyone have any example of how this is done?

Relation: Domain hasMany Elements

elements(minSize: 1, validator: {val,obj,errors->
            if (obj.elements.countBy {it.val1}.find{it.value>1}){
                errors.rejectValue('specimens',"domain.elements.val1.unique.error")
            }
            if (obj.elements.countBy {it.val2}.find{it.value>1}){
                errors.rejectValue('specimens',"domain.elements.val2.unique.error")
            }
        })

1. I'm not getting the two errors
2. The message code is not getting resolved
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: multiple custom validators for one property

bond_
Never mind the message.properties didn't get reloaded :). The previous example stated in my previous reply works


On Thu, Dec 27, 2012 at 1:00 PM, bond_ <[hidden email]> wrote:
Anyone have any example of how this is done?

Relation: Domain hasMany Elements

elements(minSize: 1, validator: {val,obj,errors->
            if (obj.elements.countBy {it.val1}.find{it.value>1}){

errors.rejectValue('specimens',"domain.elements.val1.unique.error")
            }
            if (obj.elements.countBy {it.val2}.find{it.value>1}){

errors.rejectValue('specimens',"domain.elements.val2.unique.error")
            }
        })

1. I'm not getting the two errors
2. The message code is not getting resolved



--
View this message in context: http://grails.1312388.n4.nabble.com/multiple-custom-validators-for-one-property-tp4626746p4639459.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Ravi Teja Lokineni | Software Engineer
SemanticBits India Pvt. Ltd.


Loading...