- Return true/false/null
- Return an error code String
- Return a List consisting of the error code and args
- Attach the errors yourself (only available in the 3 argument validator)
class Lolcat {will attach the following error codes to an instance's fields when validate is called (the bolded value is the default returned by obj.error.code):
String cheezburger
String bukkit
String flavr
static constraints = {
cheezburger(validator: {
return false
})
bukkit(validator: {
return 'epic.fail'
})
flavr(validator: { value, target, errors ->
errors.rejectValue('flavr', 'epic.fail')
})
}
}
- cheezburger
- lolcat.cheezburger.validator.error.Lolcat.cheezburger
- lolcat.cheezburger.validator.error.cheezburger
- lolcat.cheezburger.validator.error.java.lang.String
- lolcat.cheezburger.validator.error
- lolcat.cheezburger.validator.invalid.Lolcat.cheezburger
- lolcat.cheezburger.validator.invalid.cheezburger
- lolcat.cheezburger.validator.invalid.java.lang.String
- lolcat.cheezburger.validator.invalid
- validator.invalid.Lolcat.cheezburger
- validator.invalid.cheezburger
- validator.invalid.java.lang.String
- validator.invalid
- bukkit
- lolcat.bukkit.validator.error.Lolcat.bukkit
- lolcat.bukkit.validator.error.bukkit
- lolcat.bukkit.validator.error.java.lang.String
- lolcat.bukkit.validator.error
- lolcat.bukkit.epic.fail.Lolcat.bukkit
- lolcat.bukkit.epic.fail.bukkit
- lolcat.bukkit.epic.fail.java.lang.String
- lolcat.bukkit.epic.fail
- epic.fail.Lolcat.bukkit
- epic.fail.bukkit
- epic.fail.java.lang.String
- epic.fail
- flavr
- epic.fail.Lolcat.flavr
- epic.fail.flavr
- epic.fail.java.lang.String
- epic.fail
In addition, on the 'cheezburger' and 'bukkit' validators Grails will automatically attach three arguments:
- field name
- class name
- field value
I've created a helper method ValidationUtils.rejectValue(target, errors, fieldName, code, args) that will do all this for you which enables error codes and message arguments to be consistently standardised no matter which type of validator you're writing.
No comments:
Post a Comment