aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorWojciech Wnętrzak <w.wnetrzak@gmail.com>2015-01-04 09:18:03 +0100
committerWojciech Wnętrzak <w.wnetrzak@gmail.com>2015-01-20 22:33:42 +0100
commitcb74473db68900d336844d840dda6e10dc03fde1 (patch)
treebd1a32817e79f25cb7ced1506e302a0642fbb7fb /guides
parent08fe700e2fd57a63d1ee899b63e0e818bc0f4e69 (diff)
downloadrails-cb74473db68900d336844d840dda6e10dc03fde1.tar.gz
rails-cb74473db68900d336844d840dda6e10dc03fde1.tar.bz2
rails-cb74473db68900d336844d840dda6e10dc03fde1.zip
Add ActiveModel::Errors#details
To be able to return type of validator, one can now call `details` on Errors instance: ```ruby class User < ActiveRecord::Base validates :name, presence: true end ``` ```ruby user = User.new; user.valid?; user.errors.details => {name: [{error: :blank}]} ```
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_record_validations.md58
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.