Grails输入错误:属性必须是有效日期 [英] Grails input error: property must be a valid date

查看:71
本文介绍了Grails输入错误:属性必须是有效日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用自定义日期和时间选择器脚本的文本输入.

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>

它给了我这个错误 Property org.springframework.context.support.DefaultMessageSourceResolvable: codes [rms.User.dateOfBirth,dateOfBirth]; arguments []; default message [dateOfBirth] must be a valid Date

我不知道如何解决这个问题.使用grails dateTime picker标签似乎有效,但是很丑

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

推荐答案

唯一的方法是使用SimpleDateFormat.我经常将其用于内部带有JS日历的表单:

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
  }

更新:

您必须使用正确的日期格式.我在示例dd.MM.yyyy中给出的那个对应于08.02.2014.对于像February 08 2014这样的字符串,您将需要像MMM dd yyyy这样的字符串.

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.

请参见 http://docs.oracle.com /javase/7/docs/api/java/text/SimpleDateFormat.html 了解更多信息

这篇关于Grails输入错误:属性必须是有效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆