Grails - 使域类中的嵌入字段可为空
如何将嵌入字段指定为可为空?在下面的简单示例中,如果没有与该商品关联的价格,我希望字段价格可为空。但是,如果有价格,则货币中的两个字段都是必需的。下面的代码不起作用。当我尝试保存项目时,它抱怨货币字段为空值。
class Item {
static constraints = {
price(nullable:true)
}
static embedded = ['price']
Currency price
}
class Currency {
Integer quantity
String currencyType
}
How can you specify an embedded field as nullable? In the simple example below I want the field price to be nullable, if there is no price associated with the item. However, if there is a price, both fields in the Currency are required. The following code doesn't work. When I try to save the Item, it complains about null values for the currency fields.
class Item {
static constraints = {
price(nullable:true)
}
static embedded = ['price']
Currency price
}
class Currency {
Integer quantity
String currencyType
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在嵌入对象中定义一个
静态约束
即可。然后,只需将
'Currency.both.fields.required'
添加到messages.properties
即可显示相应的错误。Just define a
static constraints
in your embedded object.Then, just add
'Currency.both.fields.required'
to yourmessages.properties
to display the appropriate error.