diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 87f128e9a3..7770d805b1 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -684,6 +684,8 @@ module ActiveRecord # # Configuration options: # * message - A custom error message (default is: "is invalid") + # * allow_nil - If set to true, skips this validation if the attribute is null (default is: false) + # * allow_blank - If set to true, skips this validation if the attribute is blank (default is: false) # * with - The regular expression used to validate the format with (note: must be supplied!) # * on Specifies when this validation is active (default is :save, other options :create, :update) # * if - Specifies a method, proc or string to call to determine if the validation should diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 34caf0e67e..bf79521cca 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -489,6 +489,14 @@ class ValidationsTest < ActiveRecord::TestCase assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) } end + def test_validate_format_with_allow_blank + Topic.validates_format_of(:title, :with => /^Validation\smacros \w+!$/, :allow_blank=>true) + assert !Topic.create("title" => "Shouldn't be valid").valid? + assert Topic.create("title" => "").valid? + assert Topic.create("title" => nil).valid? + assert Topic.create("title" => "Validation macros rule!").valid? + end + # testing ticket #3142 def test_validate_format_numeric Topic.validates_format_of(:title, :content, :with => /^[1-9][0-9]*$/, :message => "is bad data")