Grails 域类自定义验证器

发布于 2024-10-12 17:48:32 字数 882 浏览 10 评论 0原文

我有限制,所以不能超过 存储的 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 than
ConfigurationHolder.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

a√萤火虫的光℡ 2024-10-19 17:48:32

您可以使用 Grails 自定义约束插件 来管理您的验证实施。然后,您可以像预定义的 Grails 约束一样调用您自己的约束:

package support.reminder.web

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

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(maxRows: CH.config.support.reminder.web.person.max)
    }

}

或者,如果您不想依赖第 3 方插件,您可以在 Service 方法中实现自定义验证器的逻辑,但从 Service 方法中的自定义验证器调用它领域:

package support.reminder.web

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

class Person {

    def validationService
    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 ->
           validationService.validateMaxRows(val, CH.config.support.reminder.web.person.max)
        }
    }

}

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:

package support.reminder.web

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

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(maxRows: CH.config.support.reminder.web.person.max)
    }

}

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:

package support.reminder.web

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

class Person {

    def validationService
    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 ->
           validationService.validateMaxRows(val, CH.config.support.reminder.web.person.max)
        }
    }

}
相权↑美人 2024-10-19 17:48:32

我没有更好的主意,但我确实建议您可能需要检查 <不<=。我认为在验证您的对象时,它尚未存储在数据库中,因此它不会包含在 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.

谈下烟灰 2024-10-19 17:48:32

我建议您使用服务函数,例如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文