Giter Site home page Giter Site logo

iq-scm / active_model-errors_details Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cowbell/active_model-errors_details

0.0 0.0 0.0 18 KB

Adds ActiveModel::Errors#details to return type of used validator - Backport from Rails 5.0

License: MIT License

Ruby 100.00%

active_model-errors_details's Introduction

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.

active_model-errors_details's People

Contributors

morgoth avatar tfausak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.