doelleri
doelleri

Reputation: 19682

What's the correct way to constrain a Date property?

The following constraint is unreliable because new Date() will only be evaluated once, leaving you with a stale max date.

class Foo {
    Date date

    static constraints = {
        date max: new Date()
    }
}

So how do you reliably constrain a Date?

Upvotes: 0

Views: 167

Answers (1)

James Kleeh
James Kleeh

Reputation: 12228

Assuming the date cannot be greater than the current date of validation:

static constraints = {
    date(validator: { val, obj -> val <= new Date() })
}

Grails validator

Upvotes: 4

Related Questions