Quantcast

Validating domain object as a whole?

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

Validating domain object as a whole?

John Moore
I've Googled this and haven't found a definitive answer, so I'm wondering whether I've missed something.

The Grails validation mechanism, with constraints allowing custom validators, is pretty neat. But it works at a field level, not an object level. I find I need a way of validating the domain object AS A WHOLE, not tied to any specific field. For example, in an application I'm working on, there are six related checkboxes, a maximum of 2 of which may be selected. This clearly does not lend itself to field-level validation, even with custom validators, because there is nothing wrong with the field values themselves. I need a way of applying a validation mechanism which can assess the relationship between multiple fields. Grails 1.3.6 added a prevalidate method for domain classes, and it seems this could be used to do what I want. But I get the impression that that's not what it's really for, which is more a kind of cleanup before validation. Is there some other neat way of doing what I want?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Validating domain object as a whole?

olivertynes
validator: { val, obj ->
 def count = //logic that looks if the obj.checbox1 obj.checkbox2 are
checked or not etc and counts the checked ones up
 if(count > 2) {
     return 'some.validation.error'
 }
}

Put the validator on one of the fields you are checking.

One possible solution, quick and easy one at least.

-Oliver


On Thu, Feb 9, 2012 at 2:41 PM, John Moore <[hidden email]> wrote:

> I've Googled this and haven't found a definitive answer, so I'm wondering
> whether I've missed something.
>
> The Grails validation mechanism, with constraints allowing custom
> validators, is pretty neat. But it works at a field level, not an object
> level. I find I need a way of validating the domain object AS A WHOLE, not
> tied to any specific field. For example, in an application I'm working on,
> there are six related checkboxes, a maximum of 2 of which may be selected.
> This clearly does not lend itself to field-level validation, even with
> custom validators, because there is nothing wrong with the field values
> themselves. I need a way of applying a validation mechanism which can assess
> the relationship between multiple fields. Grails 1.3.6 added a prevalidate
> method for domain classes, and it seems this could be used to do what I
> want. But I get the impression that that's not what it's really for, which
> is more a kind of cleanup before validation. Is there some other neat way of
> doing what I want?
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Validating-domain-object-as-a-whole-tp4372861p4372861.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
>
>



--
*****
Oliver Tynes
Developer
Uni CIPR -- www.cipr.uni.no
55588266
*****

---------------------------------------------------------------------
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
star

Re: Validating domain object as a whole?

John Moore
olivertynes wrote
Put the validator on one of the fields you are checking.
Thanks for the suggestion, and yes, this would work, as would the prevalidate technique. But my picky problem with it is that it's effectively a kludge - I'm actually tricking field-level validation to function as object-level validation. I'm just surprised that there's not some kind of standard way to do validation which doesn't apply at a field level.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Validating domain object as a whole?

Roberto Guerra
Hmmm, since these domain objects will all be comprised of fields, I don't see how you can do validation without fields. If one wants to validate that only a certain number of fields are validated, it is still in effect field validation. The object would need to inspect these fields and run some logic on them. Which is what the custom validators do anyway.

On Thu, Feb 9, 2012 at 8:48 AM, John Moore <[hidden email]> wrote:

olivertynes wrote
>
> Put the validator on one of the fields you are checking.
>

Thanks for the suggestion, and yes, this would work, as would the
prevalidate technique. But my picky problem with it is that it's effectively
a kludge - I'm actually tricking field-level validation to function as
object-level validation. I'm just surprised that there's not some kind of
standard way to do validation which doesn't apply at a field level.


--
View this message in context: http://grails.1312388.n4.nabble.com/Validating-domain-object-as-a-whole-tp4372861p4373091.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





--
The Journey Is The Reward.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Validating domain object as a whole?

houbie
In reply to this post by John Moore
On 09 Feb 2012, at 15:48, John Moore wrote:

>  But my picky problem with it is that it's effectively
> a kludge - I'm actually tricking field-level validation to function as
> object-level validation. I'm just surprised that there's not some kind of
> standard way to do validation which doesn't apply at a field level.

Instance level validation is indeed something that is still missing in grails.
We've implemented it in the rich domain plugin:

maxInvoiceQuantity(validator: {order ->
    if (order.paymentMethod == PaymentMethod.INVOICE && order.orderLines*.quantity.sum() > 10) {
        return "maxInvoiceQuantity"
    }
})


In this example maxInvoiceQuantity is not a field, but a logical name for the validation rule. Putting this validation on a field is not only semantically incorrect, but also has the problem that the error is shown next to that field.

Unfortunately rich domain validation is only applicable for POGO's, not for grails domain classes :(


Grtz,
Ivo


---------------------------------------------------------------------
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
star

Re: Validating domain object as a whole?

John Moore
I've settled on using beforeValidate for the moment for this, as suggested by this article:

http://dmitrijs.artjomenko.com/2011/07/whole-object-validation-with-grails.html

I test the object for validity on the basis of a number of fields, and if there's a problem, I create an ObjectError and add it to the domain object's errors object. I'm still having difficult displaying the error using message.properties (I end up with the placeholders still showing), but I'm sure it's something I'm doing wrong with ObjectError.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Validating domain object as a whole?

Alexandre Jacques
Hi,

John, have you revisited this problem? I mean, have you found another approach than the one you described here?

I've been struggling myself to find a less ugly way to do that. I would be nice if we could override/customise the validate() method.

Regards

Alexandre Jacques



On Thu, Feb 9, 2012 at 6:46 PM, John Moore <[hidden email]> wrote:
I've settled on using beforeValidate for the moment for this, as suggested by
this article:

http://dmitrijs.artjomenko.com/2011/07/whole-object-validation-with-grails.html

I test the object for validity on the basis of a number of fields, and if
there's a problem, I create an ObjectError and add it to the domain object's
errors object. I'm still having difficult displaying the error using
message.properties (I end up with the placeholders still showing), but I'm
sure it's something I'm doing wrong with ObjectError.

--
View this message in context: http://grails.1312388.n4.nabble.com/Validating-domain-object-as-a-whole-tp4372861p4374314.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





--
Alexandre Jacques
Loading...