Grails 域类自定义验证器
我有限制,所以不能超过 存储的 ConfigurationHolder.config.support.reminder.web.person.max
对象。 我没有找到如何添加与特定属性无关的验证器。所以目前我是这样实现的。你们有什么想法如何让它变得更好吗?
package support.reminder.web
import org.codehaus.groovy.grails.commons.ConfigurationHolder;
class Person {
String firstName
String lastName
String email
Date lastDutyDate
static constraints = {
firstName(blank: false)
lastName(blank: false)
email(blank: false, email: true)
lastDutyDate(nullable: true)
id validator: {val ->
if (val)
Person.count() <= ConfigurationHolder.config.support.reminder.web.person.max
else
Person.count() < ConfigurationHolder.config.support.reminder.web.person.max
}
}
String toString() {
"[$firstName $lastName, $email, $lastDutyDate]"
}
}
I have a restriction so there could be no more thanConfigurationHolder.config.support.reminder.web.person.max
object stored.
I didn't find how to add a validator which doesn't relate on particular property. So for now I implemented it in this way. Do you guys have any ideas how to make it better?
package support.reminder.web
import org.codehaus.groovy.grails.commons.ConfigurationHolder;
class Person {
String firstName
String lastName
String email
Date lastDutyDate
static constraints = {
firstName(blank: false)
lastName(blank: false)
email(blank: false, email: true)
lastDutyDate(nullable: true)
id validator: {val ->
if (val)
Person.count() <= ConfigurationHolder.config.support.reminder.web.person.max
else
Person.count() < ConfigurationHolder.config.support.reminder.web.person.max
}
}
String toString() {
"[$firstName $lastName, $email, $lastDutyDate]"
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Grails 自定义约束插件 来管理您的验证实施。然后,您可以像预定义的 Grails 约束一样调用您自己的约束:
或者,如果您不想依赖第 3 方插件,您可以在 Service 方法中实现自定义验证器的逻辑,但从 Service 方法中的自定义验证器调用它领域:
You can use the Grails Custom Constraints Plugin to manage your validation implementation. Then you can call your own constraint just like the predefined Grails constraints:
Alternatively, if you don't want to rely on 3rd Party Plugins you can implement the logic of your custom validator within a Service method but call it from the custom validator in the Domain:
我没有更好的主意,但我确实建议您可能需要检查 <不<=。我认为在验证您的对象时,它尚未存储在数据库中,因此它不会包含在 Person.count() 中。我认为 <= 会导致它通过验证然后被保存,那么你就违反了规则。
I don't have a better idea, but I do suggest that maybe you need to check for < not <=. I think that when validating your object, it has not yet been stored in the DB, so it will not be included in Person.count(). I reckon that <= would cause it to pass the validation then be saved, then you would be violating the rule.
我建议您使用服务函数,例如 personService.addPerson()。然后在保存新对象之前检查约束。如果您获得更复杂的约束,例如当它与许多域对象相关时,它将受益匪浅。
如果从验证器的含义来看,使用验证器来限制对象的数量实际上并不是很好:对象是有效的,只是对象的数量太大了。
简而言之:逻辑代码进入服务。
I recommend you using a service function, like personService.addPerson(). Then check the constraint before you save a new object. It will benefit if you get more complicated constraint, such as when it related many domain objects, for example.
The use of a validator for restricting the number of object is actually not very good if concerning about the meaning of validator: the object is valid, only the number of objects is too large.
In short: the logical code goes to the service.