提交 f6e921f9 编写于 作者: J John D. Hume 提交者: Michael Koziarski

Substitute value into validates_format_of message

Signed-off-by: NMichael Koziarski <michael@koziarski.com>
上级 ef0ea782
......@@ -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
......
......@@ -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 ) )
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册