2016-02-26 59 views
0

我目前正在使用grails约束。我有课,看起来像这样Grails:检查一个特定的属性是否有错误

@Validateable 
class StudentBean{ 
    def String name; 
    def String age; 
    def String address; 

    def List<ErrorBean> errors = []; 
    static constraints = { 
     age nullable : false, validator : { val, obj, errors-> 
      if(val<10) 
       errors.rejectValue("age", "student.age.notQualified.message", [val] as Object[], "Student not qualified."); 
     } 
    } 
} 

现在,假设我宣布一个很大的制约,然后我打电话student.validate()

我如何知道一个特定的属性有错误?例如,我只想知道“年龄”属性是否有错误?

回答

2

如果你确定你的目的是通过检查student.validate()有错误,你可以使用:

student.errors.getFieldError("age") 

请记住,您还可以validate only custom properties

student.validate(["age"]) 
相关问题