I see custom validators being used in models (good) but I also see the validators themselves adding to the record errors object.
e.g. I see
(changed example)
class Title < ActiveRecord::Base
# implement the method called during validation
def validate_each(record, attribute, value)
record.errors[attribute] << 'must be Mr / Mrs / Dr.' unless ['Mr.', 'Mrs.', 'Dr.'].include?(value)
end
end
This feels a bit like a state thing that should be being done in the controller and the validation should just return true/false but maybe not.
I tend towards this pattern:
Where the error set by the model is a symbol (acting more like an error code), which is then handled by the view layer,
.ymlfiles of whatever. This avoids putting too much presentation layer stuff in the model.