aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/validations.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-03-19 23:28:59 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-03-19 23:28:59 +0000
commit8828b2ca674acfa028a3c1e086a1795d3bb893e1 (patch)
tree17a0b5793d99e0c0a05f31c70adea642946a87a3 /activemodel/lib/active_model/validations.rb
parent6ed42ebdff05f9d28a60e91093d8f9afad03a958 (diff)
downloadrails-8828b2ca674acfa028a3c1e086a1795d3bb893e1.tar.gz
rails-8828b2ca674acfa028a3c1e086a1795d3bb893e1.tar.bz2
rails-8828b2ca674acfa028a3c1e086a1795d3bb893e1.zip
Move all the Active Record validations to Active Model
Diffstat (limited to 'activemodel/lib/active_model/validations.rb')
-rw-r--r--activemodel/lib/active_model/validations.rb41
1 files changed, 29 insertions, 12 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index 460d2d82e5..3ea17381c9 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -38,7 +38,7 @@ module ActiveModel
# end
#
# This usage applies to +validate_on_create+ and +validate_on_update as well+.
- #
+
# Validates each attribute against a block.
#
# class Person < ActiveRecord::Base
@@ -48,7 +48,7 @@ module ActiveModel
# end
#
# Options:
- # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>)
+ # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
# * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
@@ -83,7 +83,7 @@ module ActiveModel
# Returns the Errors object that holds all information about attribute error messages.
def errors
- @errors ||= Errors.new
+ @errors ||= Errors.new(self)
end
# Runs all the specified validations and returns true if no errors were added otherwise false.
@@ -92,35 +92,52 @@ module ActiveModel
run_callbacks(:validate)
- if responds_to?(:validate)
- ActiveSupport::Deprecations.warn "Base#validate has been deprecated, please use Base.validate :method instead"
+ if respond_to?(:validate)
+ # ActiveSupport::Deprecation.warn "Base#validate has been deprecated, please use Base.validate :method instead"
validate
end
if new_record?
run_callbacks(:validate_on_create)
- if responds_to?(:validate_on_create)
- ActiveSupport::Deprecations.warn(
- "Base#validate_on_create has been deprecated, please use Base.validate_on_create :method instead")
+ if respond_to?(:validate_on_create)
+ # ActiveSupport::Deprecation.warn "Base#validate_on_create has been deprecated, please use Base.validate_on_create :method instead"
validate_on_create
end
else
run_callbacks(:validate_on_update)
- if responds_to?(:validate_on_update)
- ActiveSupport::Deprecations.warn(
- "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead")
+ if respond_to?(:validate_on_update)
+ # ActiveSupport::Deprecation.warn "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead"
validate_on_update
end
end
errors.empty?
end
+
+ # Performs the opposite of <tt>valid?</tt>. Returns true if errors were added, false otherwise.
+ def invalid?
+ !valid?
+ end
+
+ protected
+
+ # Overwrite this method for validation checks on all saves and use <tt>Errors.add(field, msg)</tt> for invalid attributes.
+ def validate
+ end
+
+ # Overwrite this method for validation checks used only on creation.
+ def validate_on_create
+ end
+
+ # Overwrite this method for validation checks used only on updates.
+ def validate_on_update
+ end
end
end
Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path|
filename = File.basename(path)
require "active_model/validations/#{filename}"
-end \ No newline at end of file
+end