Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Substitute value into validates_format_of message

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit 61036a60eb4677d0b1bc0f9c5ca47ad856301431 1 parent a6e7908
@duelinmarkers duelinmarkers authored NZKoz committed
View
2  activerecord/lib/active_record/validations.rb
@@ -692,7 +692,7 @@ def validates_format_of(*attr_names)
raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp)
validates_each(attr_names, configuration) do |record, attr_name, value|
- record.errors.add(attr_name, configuration[:message]) unless value.to_s =~ configuration[:with]
+ record.errors.add(attr_name, configuration[:message] % value) unless value.to_s =~ configuration[:with]
end
end
View
6 activerecord/test/cases/validations_test.rb
@@ -583,6 +583,12 @@ def test_validate_format_numeric
assert_nil t.errors.on(:title)
end
+ def test_validate_format_with_formatted_message
+ Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => "can't be %s")
+ t = Topic.create(:title => 'Invalid title')
+ assert_equal "can't be Invalid title", t.errors.on(:title)
+ end
+
def test_validates_inclusion_of
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) )
Please sign in to comment.
Something went wrong with that request. Please try again.