简体   繁体   English

Grails命令对象根据其他字段的值验证字段

[英]Grails command objects validating fields based on the value of other fields

I have a command object 我有一个命令对象

@Validateable
class PropertyDetailsCommand implements Serializable {
   Boolean ownerOccupied
   String personalDescription

    static constraints = {

        personalDescription validator:{value, obj, errs->
             if(obj.ownerOccupied==true)
             {
               if(!value)
                 errs.rejectValue('personalDescription','propertyDetailsCommand.personalDescription.blank')

             }

         }

    }

 }

I found that when the statement 我发现当声明

 if(obj.ownerOccupied==true)

is present, the validation does not work. 如果存在,则验证无效。

I tries by adding 我尝试通过添加

static mapping={
    ownerOccupied lazy:false
} 

But it does not work. 但这行不通。

Something like this should work... 这样的事情应该起作用...

@Validateable
class PropertyDetailsCommand implements Serializable {
    Boolean ownerOccupied
    String personalDescription

    static constraints = {
        personalDescription validator:{value, obj ->
            if(obj.ownerOccupied && !value) {
                return 'blank'
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM