mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #8527 from shockone/patch-1
Add a method full_messages_for to the Errors class
This commit is contained in:
commit
b8b3df0e63
3 changed files with 39 additions and 0 deletions
|
@ -1,5 +1,10 @@
|
||||||
## Rails 4.0.0 (unreleased) ##
|
## Rails 4.0.0 (unreleased) ##
|
||||||
|
|
||||||
|
* Add ActiveModel::Errors#full_messages_for, a method that returns all the error
|
||||||
|
messages for a given attribute.
|
||||||
|
|
||||||
|
*Volodymyr Shatsky*
|
||||||
|
|
||||||
* Added a method so that validations can be easily cleared on a model.
|
* Added a method so that validations can be easily cleared on a model.
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
|
|
|
@ -352,6 +352,20 @@ module ActiveModel
|
||||||
map { |attribute, message| full_message(attribute, message) }
|
map { |attribute, message| full_message(attribute, message) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns all the full error messages for a given attribute in an array.
|
||||||
|
#
|
||||||
|
# class Person
|
||||||
|
# validates_presence_of :name, :email
|
||||||
|
# validates_length_of :name, in: 5..30
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# person = Person.create()
|
||||||
|
# person.errors.full_messages_for(:name)
|
||||||
|
# # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
|
||||||
|
def full_messages_for(attribute)
|
||||||
|
(get(attribute) || []).map { |message| full_message(attribute, message) }
|
||||||
|
end
|
||||||
|
|
||||||
# Returns a full message for a given attribute.
|
# Returns a full message for a given attribute.
|
||||||
#
|
#
|
||||||
# person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
|
# person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
|
||||||
|
|
|
@ -216,6 +216,26 @@ class ErrorsTest < ActiveModel::TestCase
|
||||||
person.errors.add(:name, "can not be nil")
|
person.errors.add(:name, "can not be nil")
|
||||||
assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages
|
assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'full_messages_for should contain all the messages for a given attribute' do
|
||||||
|
person = Person.new
|
||||||
|
person.errors.add(:name, "can not be blank")
|
||||||
|
person.errors.add(:name, "can not be nil")
|
||||||
|
assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages_for(:name)
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'full_messages_for should not contain messages for another attributes' do
|
||||||
|
person = Person.new
|
||||||
|
person.errors.add(:name, "can not be blank")
|
||||||
|
person.errors.add(:email, "can not be blank")
|
||||||
|
assert_equal ["name can not be blank"], person.errors.full_messages_for(:name)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "full_messages_for should return an empty array in case if errors hash doesn't contain a given attribute" do
|
||||||
|
person = Person.new
|
||||||
|
person.errors.add(:name, "can not be blank")
|
||||||
|
assert_equal [], person.errors.full_messages_for(:email)
|
||||||
|
end
|
||||||
|
|
||||||
test 'full_message should return the given message if attribute equals :base' do
|
test 'full_message should return the given message if attribute equals :base' do
|
||||||
person = Person.new
|
person = Person.new
|
||||||
|
|
Loading…
Reference in a new issue