Grails 日期验证的最小约束
我是 grails 和 groovy 的新手。我有一个带有开始和结束日期的项目域类。我想添加一个约束,指定结束日期需要大于开始日期(然后项目的另一个子对象需要使其开始日期和结束日期与父项目的日期进行验证)。这是否可以通过 min 约束实现,或者我是否必须将其放在其他地方?唯一约束确实允许两个属性以这种方式链接,希望最小/最大约束允许这样做。 我尝试过
startDate(blank:false)
endDate(blank:false, min:'startDate')
它抛出一个错误,指出属性 startDate 在项目上不可用
I am a newbie in grails and groovy. I have a Project domain-class with start and end date. I want to put in a constraint specifying that the end date needs to be greater than the start date(and then further another child object of project needs to have its startdate and enddate validate with the parent Project's dates). Is this possible with the min constraint or do I have to put it elsewhere? Unique constraint does allow two properties to be linked that way, hoping min/max constraints allow that.
I have tried
startDate(blank:false)
endDate(blank:false, min:'startDate')
It throws an error saying the property startDate is not available on Project
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用自定义验证器:
val
是字段的值,obj
是对正在验证的对象的引用。闭包可以包含您需要的任何逻辑,因此您可以按照您在问题中描述的方式扩展验证(通过使用 obj 访问您引用的子对象)。自定义验证器非常灵活。请查看文档。理想情况下,您需要返回一条自定义消息;如何做到这一点也可以在上面链接的文档中找到。
Try using a custom validator:
val
is the value of the field andobj
is a reference to the object being validated. The closure can contain whatever logic you need, so you can extend your validation in the way you're describing in your question (by accessing the child objects you refer to usingobj
).The custom validator is pretty flexible. Have a look at the documentation. Ideally you'll want to return a custom message; how to do that can also be found in the docs linked above.