如何伪造验证错误?
我正在使用 Grails Webflow 插件。以下是我正在使用的域对象:
class Foo implements Serializable {
String fooProp1,
fooProp2
static constraints = {
fooProp2 nullable: false
}
}
class Bar implements Serializable {
Foo fooObject
static constraints = {
fooObject nullable: false
}
}
在 webflow 中的某个点,我需要确保 fooObject.fooProp1 不为 null。如果是,我想抛出一个错误并强制用户为其提供一个值。我尝试使用 validate() 来执行此操作(在 Bar 和 Foo 对象上),但由于 fooProp1 具有 nullable:true 属性,因此它通过了验证。有什么想法吗?
I'm using the Grails Webflow plugin. Here are the domain objects I'm working with:
class Foo implements Serializable {
String fooProp1,
fooProp2
static constraints = {
fooProp2 nullable: false
}
}
class Bar implements Serializable {
Foo fooObject
static constraints = {
fooObject nullable: false
}
}
At a point in the webflow, I need to make sure that fooObject.fooProp1 is not null. If it is, I want to throw an error and force the user to supply it with a value. I tried using validate() to do this (on both the Bar and Foo objects), but since fooProp1 has the nullable:true property, it passes validation. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过调整以下代码在 Web Flow 中执行此操作:
该方法的第二个参数
'nullable'
对于您的情况可能有所不同。您只需将其设置为消息代码(来自 message.properties)即可显示您想要的错误消息。请查看此处,了解更多使用
reject()
和rejectValue()
。You can probably do this in the Web Flow by adapting the following code:
The second argument to that method,
'nullable'
, might be different for your situation. You'll just need to set it to the message code (from message.properties) to display the error message that you want.Have a look here for more ways to use
reject()
andrejectValue()
.