Grails 域类属性设置验证失败
我对 grails 中的 setter 有问题。我有两个属性 beforeTax 和 afterTax。我只想在Tax之前的数据库中存储一个属性。在用户界面中,我希望用户在税前或税后输入。因此,我将 afterTax 设置为这样的瞬态属性:
double getAfterTax(){
return beforeTax * tax
}
void setAfterTax(double value){
beforeTax = value / tax
}
当我现在输入税后值并想要保存对象时,验证失败(税前不能为空值)
我做错了什么?
I have problem with a setter in grails. I have two properties beforeTax and afterTax. I only want to store one property in the db beforeTax. In the ui I want the user to enter either before or after tax. So I made the afterTax a transient property like this:
double getAfterTax(){
return beforeTax * tax
}
void setAfterTax(double value){
beforeTax = value / tax
}
When I now enter the after tax value and want to save the object the validation fails (before tax can not be an empty value)
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,你想根据 afterTax 的值计算 beforeTax 吗?
您可以使用事件处理程序方法 beforeXXX()(其中 XXX 是 Validate、Insert 和 Update)来计算 beforeTax。那么beforeTax的约束可以为nullable:false。
If I understand your question correctly, you want to compute beforeTax based on the value of afterTax?
You could use the event handler methods beforeXXX() where XXX is Validate, Insert, and Update to compute beforeTax. Then beforeTax's constraint can be nullable:false.
您必须将一个属性标记为瞬态,以防止 GORM 尝试将此变量保存到数据库中。尝试将此行添加到您的域类中,其中包含
afterTax
。You have to flag one property as transient, in order to prevent GORM from trying to save this variable into DB. Try to add this line into your domain class, which contains
afterTax
.