how to use several shared/reusable constraints on a field

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

how to use several shared/reusable constraints on a field

Zoran F.
Hi grails folks!

This is my first posting. I'm doing my first grails project in my company, I want to establish it as a standard web framework and I'm very excited using it! :-)

Here is  my very first question. It is about shared constraints. For example
supportedAmount(shared:'check1')
works just fine.

But is there a possibility to use more than one shared constraint on a field in a domain class ?
I searched the forum and internet, but coudn't find anything about that.

In my ignorance, I tried things like
supportedAmount(shared:'check1', shared:'check2')
supportedAmount(shared:['check1', 'check2'])
supportedAmount(shared:'check1', 'check2')
but none of that works.

Of course I defined constraints 'check1' and 'check2' in Config.groovy

I also tried this approach:
http://www.jworks.nl/2011/07/19/testing-resusable-validation/
but again could not make it work with two constraints on the same field, like:
        supportedAmount(SharedConstraints.checkA, SharedConstraints.checkB)

During writing this posting I also stumbled accros this plugin
http://grails.org/plugin/constraints
but I haven't tried it yet. From its documentation I cannot figure out if it supports such thing.

Maybe solution is trivial, but I'm new to grails. I also read about Command Objects and validation, but somehow I still don't really uderstand them. I have to research/read more about using them.

Can someone please give me a short advice on which concept to use?
We are implementing an online wizard where user submits a lot of fields and the validation is actually the core-thing of the project.  We are implementing an online wizard where user submits many fields. On some fields we will have like 2 or three phase validation, so I need more than one custom validator on a field, but validators should be reusable for other fields and classes.

We have very tight schedule for the project, therefore I wanted to post this as soon as possible as I'am kind of stuck right now and need some competent advice.

Thank you very much in advance!

Best Regards, Zoran F.
Reply | Threaded
Open this post in threaded view
|

Re: how to use several shared/reusable constraints on a field

marcin.erdmann
On 09/15/2011 10:52 AM, Zoran F. wrote:
> I also tried this approach:
> http://www.jworks.nl/2011/07/19/testing-resusable-validation/
> but again could not make it work with two constraints on the same field,
> like:
>          supportedAmount(SharedConstraints.checkA, SharedConstraints.checkB)
Zoran,

For this to work you need to write:

     supportedAmount(SharedConstraints.checkA + SharedConstraints.checkB)

If you followed the aforementioned blog post then checkA and checkB
should be maps of constraints (just as in the blog post). The
constraints DSL in Grails works in a way where you call methods inside
of the static constraints closure. The method name is the field name for
which you define the constraints and the sole parameter is a map of
constraints. This is why you should add checkA and checkB.

Hope that it helps,
Marcin Erdmann



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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: how to use several shared/reusable constraints on a field

Zoran F.
In reply to this post by Zoran F.
Wow, thank you!