Case Insensitive Constraint

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

Case Insensitive Constraint

NiJK
Hi,

I have the following constraints in one of my domain classes

static constraints = {
        name(blank:false,unique:true)
        description(nullable:true)
    }

which works fine except that I'd like the name to be case insensitive so that names like "Example" and "example" would not be allowed.

Is there a quick and easy way to do that?

Nick.
Reply | Threaded
Open this post in threaded view
|

RE: Case Insensitive Constraint

Dustin Barnes
Pretty sure you'll have to write a custom validator for that. I'm
relatively certain this won't work, but it should send you on the right
path:

static constraints = {
  name(blank: false, validator: { val, obj ->
    return ( MyDomain.getByNameIlike(val)?.id == obj.id )
  }
}

-----Original Message-----
From: NiJK [mailto:[hidden email]]
Sent: Wednesday, September 24, 2008 3:31 AM
To: [hidden email]
Subject: [grails-user] Case Insensitive Constraint


Hi,

I have the following constraints in one of my domain classes

static constraints = {
        name(blank:false,unique:true)
        description(nullable:true)
    }

which works fine except that I'd like the name to be case insensitive so
that names like "Example" and "example" would not be allowed.

Is there a quick and easy way to do that?

Nick.
--
View this message in context:
http://www.nabble.com/Case-Insensitive-Constraint-tp19646071p19646071.ht
ml
Sent from the grails - user mailing list archive at Nabble.com.


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

    http://xircles.codehaus.org/manage_email





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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Case Insensitive Constraint

NiJK
In reply to this post by NiJK

Dustin,

I understand the kind of thing I'll need to do now.

Thanks,
Nick.
Reply | Threaded
Open this post in threaded view
|

RE: Case Insensitive Constraint

NiJK
In reply to this post by Dustin Barnes
Okay, so I solved the problem of case-insensitive duplication with the following

static constraints = {
        login(blank:false, validator: { val, obj ->
                return Quota.findByLoginIlike(val) == null
            })
    }

so that each Quota object couldn't have a duplicate login field.

The problem is that on update, the validation fails since obviously the record already exists.

Can I do this with constraints or should I move all this to the controller?
Reply | Threaded
Open this post in threaded view
|

RE: Case Insensitive Constraint

rnmixon
I think you just need to make the validator a little smarter. Have it check to see if it is a new instance. I think you can just check to see if the "id" field has a value. Something like this:

static constraints = {
        login(blank:false, validator:
           { val, obj ->
                if ( !id ) {
                    return Quota.findByLoginIlike(val) == null
                } else {
                    def dbObj = Quota.findByLoginIlike(val)
                    if (dbObj ) {
                        return dbObj.id == id
                    }
                }
            }
        )
    }



NiJK wrote
Okay, so I solved the problem of case-insensitive duplication with the following

static constraints = {
        login(blank:false, validator: { val, obj ->
                return Quota.findByLoginIlike(val) == null
            })
    }

so that each Quota object couldn't have a duplicate login field.

The problem is that on update, the validation fails since obviously the record already exists.

Can I do this with constraints or should I move all this to the controller?
Reply | Threaded
Open this post in threaded view
|

RE: Case Insensitive Constraint

NiJK
Thanks, Richard, that works just fine.
I'm relatively new to Grails & Groovy and so your example helped a lot.
Reply | Threaded
Open this post in threaded view
|

RE: Case Insensitive Constraint

Dips
How can we add custom message in above validator.
Reply | Threaded
Open this post in threaded view
|

RE: Case Insensitive Constraint

Dips
Also, can you explain how above code is working.
Reply | Threaded
Open this post in threaded view
|

Re: Case Insensitive Constraint

earntodiessaz
In reply to this post by NiJK
Thank you for your post, I look for such article along time, today I find it finally. this post gives me lots of advise it is very useful for me.
fireboy and watergirl
Reply | Threaded
Open this post in threaded view
|

Re: Case Insensitive Constraint

johndude835
In reply to this post by NiJK
Is there a quick and easy way to do that?  temple run 3