Permalink
Browse files

Added Base.validates_format_of that Validates whether the value of th…

…e specified attribute is of the correct form by matching it against the regular expression provided. [Marcel]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@174 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Dec 16, 2004
1 parent 01e9b7c commit 1579f3b45db4b5717984174689c41b1e7f679d1b
View
@@ -32,7 +32,14 @@
errors.on(:name) # => "must be shorter"
errors.on("name") # => "must be shorter"
-* Added Base.validates_boundries_of that delegates to add_on_boundary_breaking #312 [Tobias Luetke]. Example:
+* Added Base.validates_format_of that Validates whether the value of the specified attribute is of the correct form by matching
+ it against the regular expression provided. [Marcel]
+
+ class Person < ActiveRecord::Base
+ validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create
+ end
+
+* Added Base.validates_boundaries_of that delegates to add_on_boundary_breaking #312 [Tobias Luetke]. Example:
class Person < ActiveRecord::Base
validates_boundries_of :password, :password_confirmation
@@ -123,19 +123,19 @@ def validates_presence_of(*attr_names)
end
end
- # Validates that the specified attributes are within the boundry defined in configuration[:within]. Happens by default on both create and update.
+ # Validates that the specified attributes are within the boundary defined in configuration[:within]. Happens by default on both create and update.
#
# class Person < ActiveRecord::Base
- # validates_boundries_of :password, :password_confirmation
- # validates_boundries_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
+ # validates_boundaries_of :password, :password_confirmation
+ # validates_boundaries_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
# end
#
# Configuration options:
# ::within: The range that constitutes the boundary (default is: 6..20)
# ::too_long: The error message if the attributes go over the boundary (default is: "is too long (max is %d characters)")
# ::too_short: The error message if the attributes go under the boundary (default is: "is too short (min is %d characters)")
# ::on: Specifies when this validation is active (default is :save, other options :create, :update)
- def validates_boundries_of(*attr_names)
+ def validates_boundaries_of(*attr_names)
configuration = { :within => 5..20, :too_long => ActiveRecord::Errors.default_error_messagess[:too_long], :too_short => ActiveRecord::Errors.default_error_messagess[:too_short], :on => :save }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
@@ -165,7 +165,29 @@ def validates_uniqueness_of(*attr_names)
end
end
-
+ # Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression
+ # provided.
+ #
+ # class Person < ActiveRecord::Base
+ # validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create
+ # end
+ #
+ # A regular expression must be provided or else an exception will be raised.
+ #
+ # Configuration options:
+ # ::message: A custom error message (default is: "is invalid")
+ # ::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)
+ def validates_format_of(*attr_names)
+ configuration = { :message => ActiveRecord::Errors.default_error_messagess[:invalid], :on => :save, :with => nil }
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
+
+ raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp)
+
+ for attr_name in attr_names
+ class_eval(%(#{validation_method(configuration[:on])} %{errors.add("#{attr_name}", "#{configuration[:message]}") unless #{attr_name} and #{attr_name}.to_s.match(/#{configuration[:with]}/)}))
+ end
+ end
private
def validation_method(on)
@@ -310,7 +332,7 @@ def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_m
end
end
- alias :add_on_boundry_breaking :add_on_boundary_breaking
+ alias :add_on_boundary_breaking :add_on_boundary_breaking
# Returns true if the specified +attribute+ has errors associated with it.
def invalid?(attribute)
@@ -24,7 +24,7 @@ class Developer < ActiveRecord::Base
protected
def validate
- errors.add_on_boundry_breaking("name", 3..20)
+ errors.add_on_boundary_breaking("name", 3..20)
end
end
@@ -123,5 +123,5 @@ CREATE TABLE mixins (
root_id integer,
created_at timestamp,
updated_at timestamp,
- PRIMARY KEY (`id`)
+ PRIMARY KEY (id)
);
@@ -3,6 +3,6 @@ class Developer < ActiveRecord::Base
protected
def validate
- errors.add_on_boundry_breaking("name", 3..20)
+ errors.add_on_boundary_breaking("name", 3..20)
end
-end
+end
@@ -191,7 +191,7 @@ def test_validate_uniqueness
end
def test_validate_boundaries
- Topic.validates_boundries_of(:title, :content, :within => 3..5)
+ Topic.validates_boundaries_of(:title, :content, :within => 3..5)
t = Topic.create("title" => "a!", "content" => "I'm ooooooooh so very long")
assert !t.save
@@ -203,4 +203,21 @@ def test_validate_boundaries
assert t.save
end
-end
+
+ def test_validate_format
+ Topic.validates_format_of(:title, :content, :with => /^Validation macros rule!$/, :message => "is bad data")
+
+ t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!")
+ assert !t.valid?, "Shouldn't be valid"
+ assert !t.save, "Shouldn't save because it's invalid"
+ assert_equal "is bad data", t.errors.on(:title)
+ assert_nil t.errors.on(:content)
+
+ t.title = "Validation macros rule!"
+
+ assert t.save
+ assert_nil t.errors.on(:title)
+
+ assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) }
+ end
+end

0 comments on commit 1579f3b

Please sign in to comment.