0

I want to parse a string to date just before validate a command object, here is my command object code

class ActivitiesCommand {
    List schools
    List departments
    Date from
    Date to

    static constraints = {
        schools nullable:false
        departments nullable:false
        from blank:false
        to blank:false
    }

    def beforeValidate() {
       def from = new Date().parse("yyyy-MM-dd", from)
       def to = new Date().parse("yyyy-MM-dd", to)
    }
}

but i am getting java.lang.NullPointerException when i try def from = new Date().parse("yyyy-MM-dd", from) or def to = new Date().parse("yyyy-MM-dd", to). What can i do in order to successfully parse the date before validate command object?

I read the command object docs. I got this sample from there. I tried if removing ? beforeValidate does not work, so i understand i need to provide a null safe but i do not know how to do it in my scenario

class Person {
    String name

    static constraints = { name size: 5..45 }

    def beforeValidate() { name = name?.trim() } 
}

Thanks for your time.

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
Mario
  • 4,784
  • 3
  • 34
  • 50

1 Answers1

1

from and to is set to Date in the Command Object, so request parameter string with from and to names will be converted to a Date and then bound to these field.

If the expected date format matches then binding will be successful.

In your case, from and to in beforeValidate is treated as String instead. If they are String actually then you can make them nullable: false in constraints or do the check as below in beforeValidate:

from = from ? Date.parse("yyyy-MM-dd", from) : new Date() - 1 //for example

Note the appropriate use of Date.parse()

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • I am trying @dmahapatro recommendation here is the code def beforeValidate() { from = from ? Date.parse("yyyy-MM-dd", from) : new Date() - 1 } Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'to'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2014-05-31" – Mario Jun 06 '14 at 21:28
  • i also remove beforeValidate and its content, and removed from blank:false, and to blank:false from constraints in order to see if there is some automatic convert from strin to date, no success and same error message Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'to'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2014-05-31" – Mario Jun 06 '14 at 21:36
  • solution fix original question java.lang.NullPointerException, but i need know to parse string date to Date before validate command object that was the original intention for using a beforevalidte. – Mario Jun 06 '14 at 22:15
  • Yes I was about to suggest to look at this as well http://stackoverflow.com/questions/17241914/bind-date-to-command-object-in-grails/17243717#17243717 for older version of Grails – dmahapatro Jun 06 '14 at 22:29