Sagarmichael
Sagarmichael

Reputation: 1624

is there a date contraint in grails?

I am using.

Date dateOfBirth
    static constraints = {
        dateOfBirth blank: false, date: true
    }

Is this allowed I am after a date constraint and I also want to specify the format how can I do this?

Thanks.

Upvotes: 3

Views: 1870

Answers (2)

Łukasz Dziedziul
Łukasz Dziedziul

Reputation: 144

Read about validators: http://grails.org/doc/latest/guide/validation.html

Date dateOfBirth
static constraints = {
    dateOfBirth blank: false, date: true, validator: { val -> validateDate(val) }
}

Upvotes: 4

Universitas
Universitas

Reputation: 493

The Date object in grails will be a date picker in the format dd/mm/yyyy/hh:mm:ss. You can format it to just day month and year by specifying precision="day" - for example:

<g:datePicker name="fromDate" value="${params?.fromDate?: new Date().clearTime()}" precision="day"
                       noSelection="['':'-Choose-']"/>

where clearTime() sets hours minutes and seconds to 00:00:00 for use if you don't need that much precision and are only concerned with the date.

If you want to format the output of a date: g:formatDate . for validation you might want to use a closure. However, I'm not sure what you are trying to validate. Is it just that dateOfBirth is not null? Or is it that dOB is on or after a certain date? In the latter case, use a closure to define the constraint.

Upvotes: 2

Related Questions