diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index e9674d51432510267cc1e307def930e60a237648..cf97f45dba94ab1127891dc55ebbefaa17dfc3fb 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -285,6 +285,8 @@ def errors
# Runs all the specified validations and returns +true+ if no errors were
# added otherwise +false+.
#
+ # Aliased as validate.
+ #
# class Person
# include ActiveModel::Validations
#
@@ -319,6 +321,8 @@ def valid?(context = nil)
self.validation_context = current_context
end
+ alias_method :validate, :valid?
+
# Performs the opposite of valid?. Returns +true+ if errors were
# added, +false+ otherwise.
#
diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb
index 4ea5670d32c466e23bcf71975b0b3738bd9bd6a7..6a74ee353de752d9459d8d81b1d624f5e783dc31 100644
--- a/activemodel/test/cases/validations_test.rb
+++ b/activemodel/test/cases/validations_test.rb
@@ -295,6 +295,15 @@ def test_validations_on_the_instance_level
assert auto.valid?
end
+ def test_validate
+ auto = Automobile.new
+
+ assert_empty auto.errors
+
+ auto.validate
+ assert_not_empty auto.errors
+ end
+
def test_strict_validation_in_validates
Topic.validates :title, strict: true, presence: true
assert_raises ActiveModel::StrictValidationFailed do
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 42b7618fa7812f0620605b6145c06e1518cae7fe..916d24aa03a7be6de7637cc8c4e3d299ed9ca66f 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Introduce `validate` as an alias for `valid?`.
+
+ This is more intuitive when you want to run validations but don't care about the return value.
+
+ *Henrik Nyh*
+
* Create indexes inline in CREATE TABLE for MySQL.
This is important, because adding an index on a temporary table after it has been created
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 26dca415ffe81ee30a685bfcfaec950c1de887a8..9999624fcf83b474e9dea9ef1d847353429ef715 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -60,6 +60,8 @@ def save!(options={})
# Runs all the validations within the specified context. Returns +true+ if
# no errors are found, +false+ otherwise.
#
+ # Aliased as validate.
+ #
# If the argument is +false+ (default is +nil+), the context is set to :create if
# new_record? is +true+, and to :update if it is not.
#
@@ -71,6 +73,8 @@ def valid?(context = nil)
errors.empty? && output
end
+ alias_method :validate, :valid?
+
protected
def perform_validations(options={}) # :nodoc:
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index de618902aa39c91b972709b28b622e53e9e253c0..d80da06e274cd912c8732581f7d6641cc3a28008 100644
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -52,6 +52,21 @@ def test_error_on_given_context
assert r.save(:context => :special_case)
end
+ def test_validate
+ r = WrongReply.new
+
+ r.validate
+ assert_empty r.errors[:author_name]
+
+ r.validate(:special_case)
+ assert_not_empty r.errors[:author_name]
+
+ r.author_name = "secret"
+
+ r.validate(:special_case)
+ assert_empty r.errors[:author_name]
+ end
+
def test_invalid_record_exception
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.create! }
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.new.save! }