This project is archived and is in readonly mode.
validates_acceptance_of deep failure ?
-
MatthewRudy
I think this is because validates_acceptance_of has a default of :allow_nil => true,
def validates_acceptance_of(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save, :allow_nil => true, :accept => "1" } configuration.update(attr_names.extract_options!) db_cols = begin column_names rescue ActiveRecord::StatementInvalid [] end names = attr_names.reject { |name| db_cols.include?(name.to_s) } attr_accessor(*names) validates_each(attr_names,configuration) do |record, attr_name, value| record.errors.add(attr_name, configuration[:message]) unless value == configuration[:accept] end endbut it does seem to default to only accept "1" as a valid value...
should probably be "1" or true?
def validates_acceptance_of(*attr_names) configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save, :allow_nil => true, :accept => ["1", true] } configuration.update(attr_names.extract_options!) db_cols = begin column_names rescue ActiveRecord::StatementInvalid [] end names = attr_names.reject { |name| db_cols.include?(name.to_s) } attr_accessor(*names) validates_each(attr_names,configuration) do |record, attr_name, value| record.errors.add(attr_name, configuration[:message]) unless safe_to_array(configuration[:accept]).include?(value) end endperhaps that works better?
-
Jérôme
but the documentation says:
:accept - Specifies value that is considered accepted. The default value is a string "1", which makes it easy to relate to an HTML checkbox. This should be set to true if you are validating a database column, since the attribute is typecast from "1" to true before validation. -
MatthewRudy
great,
so the current functionality is fine, I guess.
-
Jérôme
Errrr no, since the tests fail !
-
Jérôme
Ok so I misunderstanded the doc. The typecasting explanation was unclear for me. Setting :accept => true is a better option since it validates "1", 1 and true, while the default :accept => "1" only validates "1"...
What about changing this default value then ?
-
MatthewRudy
well, that was my suggestion,
and what my code did was...
default to "1" or true
But, need to get a core team guy on here to decide what "should" be the default.
-
Lawrence Pit
I brought this up last month on the core list. See
http://groups.google.com.au/grou...
It does work, though not as advertised. You do have to add a hidden checkbox with a default value.
-
josh
- State changed from new to stale
- Tag set to 2.1, activerecord, bug, validations