diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-01-21 14:28:54 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-01-21 14:28:54 -0200 |
commit | 14599a575850567b28967decffbbbb270fa92676 (patch) | |
tree | 2b8e2644a7208347a3c96d649ef0e5f917ab11ca /guides | |
parent | d36a28ac59c8cedcda92ae5e9d84695caec66783 (diff) | |
parent | cb74473db68900d336844d840dda6e10dc03fde1 (diff) | |
download | rails-14599a575850567b28967decffbbbb270fa92676.tar.gz rails-14599a575850567b28967decffbbbb270fa92676.tar.bz2 rails-14599a575850567b28967decffbbbb270fa92676.zip |
Merge pull request #18322 from morgoth/add-error-codes
Add ActiveModel::Errors#codes
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_validations.md | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md index c9af70934a..84925072f2 100644 --- a/guides/source/active_record_validations.md +++ b/guides/source/active_record_validations.md @@ -227,8 +227,26 @@ end ``` We'll cover validation errors in greater depth in the [Working with Validation -Errors](#working-with-validation-errors) section. For now, let's turn to the -built-in validation helpers that Rails provides by default. +Errors](#working-with-validation-errors) section. + +### `errors.details` + +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. + +```ruby +class Person < ActiveRecord::Base + validates :name, presence: true +end + +>> person = Person.new +>> person.valid? +>> person.errors.details[:name] #=> [{error: :blank}] +``` + +Using `details` with custom validators are covered in the [Working with +Validation Errors](#working-with-validation-errors) section. Validation Helpers ------------------ @@ -1074,6 +1092,42 @@ Another way to do this is using `[]=` setter # => ["Name cannot contain the characters !@#%*()_-+="] ``` +### `errors.details` + +You can add validator type to details hash when using `errors.add` method. + +```ruby + class Person < ActiveRecord::Base + def a_method_used_for_validation_purposes + errors.add(:name, :invalid_characters) + end + end + + person = Person.create(name: "!@#") + + person.errors.details[:name] + # => [{error: :invalid_characters}] +``` + +To improve error details to contain not allowed characters set, you can +pass additional options to `errors.add` method. + +```ruby + class Person < ActiveRecord::Base + def a_method_used_for_validation_purposes + errors.add(:name, :invalid_characters, not_allowed: "!@#%*()_-+=") + end + end + + person = Person.create(name: "!@#") + + person.errors.details[:name] + # => [{error: :invalid_characters, not_allowed: "!@#%*()_-+="}] +``` + +All built in Rails validators populate details hash with corresponding +validator types. + ### `errors[:base]` You can add error messages that are related to the object's state as a whole, instead of being related to a specific attribute. You can use this method when you want to say that the object is invalid, no matter the values of its attributes. Since `errors[:base]` is an array, you can simply add a string to it and it will be used as an error message. |