2016-02-26 55 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"])