gpt4 book ai didi

forms - Grails 自定义验证场景

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

我刚开始研究 grails,以前我主要使用 PHP,所以我的问题是向表单添加自定义验证场景的最佳方法是什么。因此,假设我们在域中拥有所有约束,但是在某个 Controller 操作下,表单绑定(bind)到域,我们想要添加某种额外的自定义验证(为了参数,假设我们有一个日期,并且仅在这种情况应该在 future 2 周以上)。这样做的最佳方法是什么?

谢谢!

最佳答案

对于您的要求,您可以使用 Command Object有约束:

@grails.validation.Validateable
class CommandObject {
Date date

static constraints = {
date(/*constraint here*/)

}
}

编辑:

关于第二个问题,是的,您可以使用 importFrom [EntityName] 将约束从域对象共享到命令对象。 ,请参阅文档中的示例:

域对象:
class User {
String firstName
String lastName
String passwordHash
static constraints = {
firstName blank: false, nullable: false
lastName blank: false, nullable: false
passwordHash blank: false, nullable: false
}
}

命令对象:
class UserCommand {
String firstName
String lastName
String password
String confirmPassword
static constraints = {
importFrom User
password blank: false, nullable: false
confirmPassword blank: false, nullable: false
}
}

Sharing Constraints

关于forms - Grails 自定义验证场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665279/

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