Rails displays all validation error messages associated with a given field. If I have three validates_XXXXX_of :email
, and I leave the field blank, I get three
My monkey patch of ActiveModel::Errors
class lib/core_ext/rails/active_model/errors.rb
(I use this code for Ruby on Rails 5.0 release):
module ActiveModel
class Errors
# don't add an attribute's error message to details
# if it already contains at least one message
alias_method :old_add, :add
def add(attribute, message = :invalid, options = {})
if details[attribute.to_sym].size.zero?
old_add(attribute, message, options)
end
end
end
end
Create a file config/initializers/core_ext.rb
and add a require core_ext/rails/active_model/errors.rb
to it.