diff options
author | Jeroen van Dijk <jeroen@jeevidee.nl> | 2010-05-19 15:25:46 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-06-21 11:55:21 +0200 |
commit | 26392c4ac57e27c63984d47c6326c13f502d5786 (patch) | |
tree | d3ee41f6eb5f8a394d059097629d84365936a995 /activemodel/lib/active_model/validations/format.rb | |
parent | ead72b319f781ae3767a8695e3e29e9249388f7f (diff) | |
download | rails-26392c4ac57e27c63984d47c6326c13f502d5786.tar.gz rails-26392c4ac57e27c63984d47c6326c13f502d5786.tar.bz2 rails-26392c4ac57e27c63984d47c6326c13f502d5786.zip |
Make ActiveModel::Errors#add_on_blank and #add_on_empty accept an options hash and make various Validators pass their (filtered) options.
This makes it possible to pass additional options through Validators to message generation. E.g. plugin authors want to add validates_presence_of :foo, :format => "some format".
Also, cleanup the :default vs :message options confusion in ActiveModel validation message generation.
Also, deprecate ActiveModel::Errors#add_on_blank(attributes, custom_message) in favor of ActiveModel::Errors#add_on_blank(attributes, options).
Also, refactoring of ActiveModel and ActiveRecord Validation tests. Test are a lot more DRY now. Better test coverage as well now.
The first four points were reapplied from an older patch of Sven Fuchs which didn't apply cleanly anymore and was not complete yet.
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activemodel/lib/active_model/validations/format.rb')
-rw-r--r-- | activemodel/lib/active_model/validations/format.rb | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index 3b57d4fd77..6f6933205f 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -5,12 +5,12 @@ module ActiveModel class FormatValidator < EachValidator def validate_each(record, attribute, value) if options[:with] && value.to_s !~ options[:with] - record.errors.add(attribute, :invalid, :default => options[:message], :value => value) + record.errors.add(attribute, :invalid, options.except(:with).merge(:value => value)) elsif options[:without] && value.to_s =~ options[:without] - record.errors.add(attribute, :invalid, :default => options[:message], :value => value) + record.errors.add(attribute, :invalid, options.except(:with).merge(:value => value)) end end - + def check_validity! unless options.include?(:with) ^ options.include?(:without) # ^ == xor, or "exclusive or" raise ArgumentError, "Either :with or :without must be supplied (but not both)" |