【问题标题】:Domain class auto validation in grailsgrails中的域类自动验证
【发布时间】:2013-06-10 07:27:34
【问题描述】:

我正在从数据库中获取结果列表,但列表不会验证约束,直到我为每个对象调用“validate()”方法。

    def temp = ConfigInfo.where { projectId == project }.findAll()

    //at this stage domain class is not validated
    temp.each {         
        println "validate" + it.validate()
        println "has error" +  it.hasErrors()
    } 
          //in the above code block validate() is called, I don't want to do this. 

// 是否可以自动配置它并且我得到验证的对象。

我不想调用 validate 方法。 grails中是否有任何方法或配置可以自动验证域类。

请帮帮我。

【问题讨论】:

  • 如果您从表中获取数据并且不更改值,为什么需要再次验证它?

标签: grails-orm grails-2.0 grails-domain-class


【解决方案1】:

Grails 域类对象在保存前得到验证。

在您的域模型中使用以下内容来设置验证规则:

static constraints = {
   // whatever you want to validate
}

每当您保存一个对象时,它将通过您设置的约束进行验证。当对象在数据库中持久存在时,它始终是经过验证的。

【讨论】:

    【解决方案2】:

    您需要做的就是在约束闭包中定义所有验证。 例如

        static constraints = {
                    id maxLength: 5
                    //etc...
    }
    

    在保存对象时,您只需要检查对象是否有效!,它将返回真/假。 看这个: click here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      相关资源
      最近更新 更多