Have a highly specific, yet custom validation for a particular field on one of your Rails model objects? Don’t want to create a ActiveModel::Validator
type? Not a problem!
You can just as easily create a method that can be invoked as part of the validation process. For example, imagine a field dubbed uri
in some model object; this field must begin with a protocol (i.e. http or https). You can create a validation method like so:
1 2 3 4 5 |
|
This method resides in your model. You can then register this method as a validation for your model like so:
1
|
|
Now if the uri
field doesn’t contain http or https, model.save
will return false
. Done!