Grails input error: property must be a valid date

2019-08-28 05:25发布

I have a text input that uses custom date and time picker script.

<div class="input-group date datetime" data-start-view="4" data-date-format="dd MM yyyy" data-min-view="2" data-link-field="dtp_input1">
    <input name="dateOfBirth" class="form-control" size="16" type="text" value="${userInstance?.dateOfBirth}"/>
    <span class="input-group-addon btn btn-primary"><span class="glyphicon glyphicon-th"></span></span>
</div>

It gives me this error Property org.springframework.context.support.DefaultMessageSourceResolvable: codes [rms.User.dateOfBirth,dateOfBirth]; arguments []; default message [dateOfBirth] must be a valid Date

I dont how to solve this. Using the grails dateTime picker tag seems to work but its ugly

标签: grails
1条回答
欢心
2楼-- · 2019-08-28 06:13

The only way is to use SimpleDateFormat. I often use it for forms with a JS-calendar inside:

class YourController {

  static SimpleDateFormat sdf = new SimpleDateFormat( 'dd.MM.yyyy' )

  def index(){ 
    Date dateOfBirth
    try{ 
      if( params.dateOfBirth ) dateOfBirth = sdf.parse( params.dateOfBirth )
    }catch( Exception ignoreMe ){}
    // do something useful
  }

UPDATE:

You must use the propper date format of course. The one I gave in my example dd.MM.yyyy corresponds to 08.02.2014. For strings like February 08 2014 you are gonna need something like MMM dd yyyy.

See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for further info

查看更多
登录 后发表回答