使用jQuery UI插件时,属性日期必须是有效的Date错误 [英] Property date must be a valid Date error, when using jquery UI plugin

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

问题描述

我有一个任务类,它有一个日期字段。在我的视图文件中,我有这行代码:

I have a tasks class, which has a date field. In my view file, I have this line of code :

<g:textField name="date" value="${tasksInstance?.date}" id="datePicker" />

使用jquery ui插件,我将这段代码添加到我的<头> 标签:

And using the jquery ui plugin, I added this code to my <head> tag :

<g:javascript>
$(document).ready(function()
{
$("#datePicker").datepicker({dateFormat: 'mm/dd/yy'});
})
</g:javascript>

但是,当我保存日期字段时,我得到以下错误:

But when I save the date field, I get the following error:

Property date must be a valid Date error



编辑:



Tasks.groovy

package mnm.schedule

class Tasks {
    static belongsTo = [ user : User, project: Project ]
        String reason
        boolean completed
        String title
        String description 
                Date date
        static constraints = {
            user(nullable:false)
            completed(nullable:true)
            title(nullable:false)
            description(size:1..500,nullable:false)
                reason(nullable:true)
        }
        String toString(){
                this.title
        }
}

以及控制器的操作代码是:

And controller action code is :

def save() {
        def adminProject = params.managersProject
        def foundProject = Project.findByNameLike("$adminProject")
        def tasksInstance = new Tasks(params)
        foundProject.addToTasks(tasksInstance)
        if (!tasksInstance.save(flush: true)) {
            render(view: "create", model: [tasksInstance: tasksInstance])
            return
        }
        redirect(action: "show", id: tasksInstance.id)
    }

如何从此恢复?

推荐答案

我假设你得到一个像 '02 / 16/2012'?你可以打电话:

I assume you're getting a String like '02/16/2012'? You could just call:

params.date = Date.parse( 'MM/dd/yyyy', params.date )

在创建任务对象之前...

Before creating the Task object...

使用 PropertyEditorSupport

另外,在grails 2.0中,您可以这样做:

Also, in grails 2.0, you can do:

def date = params.date( 'myVar', 'MM/dd/yyyy' )

解析params对象中的日期参数

To parse the date param out of the params object

这篇关于使用jQuery UI插件时,属性日期必须是有效的Date错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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