paul
paul

Reputation: 303

grails is reformatting my date

I have the following domain for a groovy grails project

class Title {

    // Automatic timestamping
    DateTime dateCreated
    DateTime lastUpdated

    String name
    LocalDate startDate
    LocalDate endDate

    static constraints = {
        name blank: false, maxSize: 100
        startDate nullable: true
        endDate nullable: true
        // TODO: Validation check to make sure endDate is after startDate
    }
}

the start date uses jodatime's LocalDate.

My problem is, when I go to update the startDate field, I have to use the following format mm/dd/yyyy, but it stores the date in the database as yyyy-mm-dd. I want to make my input field, for the start date, accept yyyy-mm-dd because that is how it's stored in the database. I get the following error if I try to submit my form using any other format than the slashes

Failed to convert property value of type java.lang.String to required type org.joda.time.LocalDate for property startDate; nested exception is java.lang.IllegalArgumentException: Invalid format: "2014-01-01" is malformed at "14-01-01"

Does anyone know how to solve this issue? Should I just reformat the string?

Upvotes: 0

Views: 371

Answers (1)

Ignacio Ocampo
Ignacio Ocampo

Reputation: 2713

Are you using Joda Time plugin? If yes, you should map your field to

static mapping = {
    startDate type: PersistentLocalDate
    endDate type: PersistentLocalDate
}

In your model.

Upvotes: 1

Related Questions