Reputation: 123
I have a simple code for custom validation in grails but don't know why its not working ???
I have a domain class Person with 2 fields nationality and other .The condition goes like if the user selects nationality as others which is an inList
, then the other field is made mandatory and if user skips this field grails errors message should be raised .
nationality(blank:false,nullable:false,inList:["Indian", "Other"])
other(blank:true,nullable:true,validator:{val,obj ->
if(obj.nationality!="Indian" && !val){println "values"+val+"nationality"+obj.nationality
return ['requiredfield']
}
I have also mapped the name of error in message.properties
file as mentioned below
person.nationality.requiredfield=Please mention your nationality.
The output on console is values null nationality Others
Upvotes: 2
Views: 849
Reputation: 2312
Have you tried to provide a fully-qualified name in messages.properties
?
So something like:
com.domain.Person.other.requiredfield = Please mention you nationality
Upvotes: 1
Reputation: 4373
You are placing error message at wrong place:
do this :
person.other.requiredfield = Please mention your nationality.
Note the difference in error code i.e replace "nationality" with "other".
Upvotes: 2