diff options
author | José Valim <jose.valim@gmail.com> | 2010-01-08 08:37:58 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-01-08 21:36:04 +0100 |
commit | 7045c4c279499eb7340fb420398d613497739eef (patch) | |
tree | c62d42b74d68a738a3baa935add403e10305f70d /activemodel/lib/active_model/validations/validates.rb | |
parent | fa14d6d51ed89acde66b49e8d3a6423396c3d553 (diff) | |
download | rails-7045c4c279499eb7340fb420398d613497739eef.tar.gz rails-7045c4c279499eb7340fb420398d613497739eef.tar.bz2 rails-7045c4c279499eb7340fb420398d613497739eef.zip |
Allow validates to map some types to specific options. So now you can do:
validates :email, :presence => true, :format => /@/
validates :genre, :inclusion => %w(m f)
validates :password, :length => 6..20
Diffstat (limited to 'activemodel/lib/active_model/validations/validates.rb')
-rw-r--r-- | activemodel/lib/active_model/validations/validates.rb | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index 61caf32c06..ea7303c70b 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -7,6 +7,7 @@ module ActiveModel # custom validator classes in their place such as PresenceValidator. # # Examples of using the default rails validators: + # # validates :terms, :acceptance => true # validates :password, :confirmation => true # validates :username, :exclusion => { :in => %w(admin superuser) } @@ -19,6 +20,7 @@ module ActiveModel # # The power of the +validates+ method comes when using cusom validators # and default validators in one call for a given attribute e.g. + # # class EmailValidator < ActiveModel::EachValidator # def validate_each(record, attribute, value) # record.errors[attribute] << (options[:message] || "is not an email") unless @@ -31,29 +33,32 @@ module ActiveModel # attr_accessor :name, :email # # validates :name, :presence => true, :uniqueness => true, :length => { :maximum => 100 } - # validates :email, :presence => true, :format => { :with => /@/ } + # validates :email, :presence => true, :email => true # end # # Validator classes my also exist within the class being validated # allowing custom modules of validators to be included as needed e.g. - # - # module MyValidators + # + # class Film + # include ActiveModel::Validations + # # class TitleValidator < ActiveModel::EachValidator # def validate_each(record, attribute, value) # record.errors[attribute] << "must start with 'the'" unless =~ /^the/i # end # end - # end # - # class Film - # include ActiveModel::Validations - # include MyValidators - # # validates :name, :title => true - # end + # end + # + # The validators hash can also handle regular expressions, ranges and arrays: + # + # validates :email, :format => /@/ + # validates :genre, :inclusion => %w(mail female) + # validates :password, :length => 6..20 # - # The options :if, :unless, :on, :allow_blank and :allow_nil can be given to one specific - # validator: + # Finally, the options :if, :unless, :on, :allow_blank and :allow_nil can be given + # to one specific validator: # # validates :password, :presence => { :if => :password_required? }, :confirmation => true # @@ -78,7 +83,22 @@ module ActiveModel raise ArgumentError, "Unknown validator: '#{key}'" end - validates_with(validator, defaults.merge(options == true ? {} : options)) + validates_with(validator, defaults.merge(_parse_validates_options(options))) + end + end + + protected + + def _parse_validates_options(options) #:nodoc: + case options + when TrueClass + {} + when Hash + options + when Regexp + { :with => options } + when Range, Array + { :in => options } end end end |