No commit activity in last 3 years
No release in over 3 years
Backported from Rails 5.0 to use with 4.x versions
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

< 5.0.0, >= 3.2.13
~> 1.7
>= 5.6
>= 10.0

Runtime

< 5.0.0, >= 3.2.13
 Project Readme

ActiveModel::Errors#details

Build Status

Feature backported from Rails 5.0 to use with Rails 3.2.x and 4.x apps.

Background: rails/rails#18322

Installation

gem install "active_model-errors_details"

Usage

To check what validator type was used on invalid attribute, you can use errors.details[:attribute]. It returns array of hashes where under :error key you will find symbol of used validator.

class Person < ActiveRecord::Base
  validates :name, presence: true
end

person = Person.new
person.valid?
person.errors.details[:name]
# => [{error: :blank}]

You can add validator type to details hash when using ActiveModel::Errors.add method.

class User < ActiveRecord::Base
  validate :adulthood

  def adulthood
    errors.add(:age, :too_young) if age < 18
  end
end

user = User.new(age: 15)
user.valid?
user.errors.details
# => {age: [{error: :too_young}]}

To improve error details to contain additional options, you can pass them to ActiveModel::Errors.add method.

class User < ActiveRecord::Base
  validate :adulthood

  def adulthood
    errors.add(:age, :too_young, years_limit: 18) if age < 18
  end
end

user = User.new(age: 15)
user.valid?
user.errors.details
# => {age: [{error: :too_young, years_limit: 18}]}

All built in Rails validators populate details hash with corresponding validator types.