gpt4 book ai didi

validation - 属性 startDate 必须是 Grails 中的有效日期错误

转载 作者:行者123 更新时间:2023-12-02 15:52:23 25 4
gpt4 key购买 nike

我面临与提到的相同的问题 here ,但是当我厌倦了解决方案中的内容时,我仍然遇到同样的错误:

Property startDate must be a valid Date Property endDate must be a valid Date



这是我的域:
class EmpRef {
String workName
String title
Date startDate
Date endDate
String reasonForLeaving
String directMgrName
String directMgrTitle
String directMgrTelephone
}

这是我的保存操作:
def save(EmpRef empRefInstance) {
empRefInstance.startDate=Date.parse('dd-MM-yyyy',params.startDate)
empRefInstance.endDate=Date.parse('dd-MM-yyyy',params.endDate)
if (empRefInstance == null) {
notFound()
return
}

if (empRefInstance.hasErrors()) {
respond empRefInstance.errors, view:'create'
return
}

empRefInstance.save flush:true
}

GSP 中的 jQuery 代码:
$('.datePicker').datepicker({
changeYear: true,
changeMonth: true,
autoSize: true,
dateFormat: "dd-mm-yy",
maxDate: "0y",
showAnim: "show",
yearRange:'c-70:c+0'
});

最佳答案

如果 EmpRef是方法的参数,它会根据请求自动绑定(bind)。这会导致 startDateendDate有错误。在这些列上设置值后,您需要执行 empRefInstance.validate()而不是 empRefInstance.hasErrors()在域中获得新的验证结果。

也因为你检查了empRefInstance为空 之后 您设置了开始和结束日期,如果此对象为空,您将在前几行获得 NullPointerException,因此应在代码的较早部分检查空

关于validation - 属性 startDate 必须是 Grails 中的有效日期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37205746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com