user3714598
user3714598

Reputation: 1773

Grails: check if a specific property has an error

Im currently working on grails constraints. I have class that looks like this

@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."); 
       } 
   }
}

Now, assuming I declared a lot of constraints and then I call student.validate()

How will I know if a specific property has an error? Example, I just want to know if "age" property has an error?

Upvotes: 1

Views: 257

Answers (1)

Michal_Szulc
Michal_Szulc

Reputation: 4177

If you're sure that your object has errors by checking student.validate(), you can use:

student.errors.getFieldError( "age" )

Remember that you can also validate only custom properties:

student.validate(["age"])

Upvotes: 3

Related Questions