This project is archived and is in readonly mode.

#1715 ✓committed
Martijn Vos

AR:Base mangles dates when user submits only part of the fields

Reported by Martijn Vos | January 9th, 2009 @ 07:05 AM

When using date_select, it's possible for the user to submit only month and day, leaving year empty. How to handle that issue is up to the developer of the site, but his job is made a lot harder by ActiveRecord::Base which throws empty values out of the hash before assigning the composite values to a field.

The offending code:

  def extract_callstack_for_multiparameter_attributes(pairs)
    attributes = { }

    for pair in pairs
      multiparameter_name, value = pair
      attribute_name = multiparameter_name.split("(").first
      attributes[attribute_name] = [] unless attributes.include?(attribute_name)

      unless value.empty?
        attributes[attribute_name] <<
          [ find_parameter_position(multiparameter_name), type_cast_attribute_value(multiparameter_name, value) ]
      end
    end

    attributes.each { |name, values| attributes[name] = values.sort_by{ |v| v.first }.collect { |v| v.last } }
  end

This means that if the user submits something like:

{"date(1i)"=>"", "date(2i)"=>"6", "date(3i)"=>"5"}

The date becomes "0005-06-01" instead of "0000-06-05".

Both are meaningless dates, but in the latter case, at least it's obvious what the problem is. The developer can easily validate this and return a meaningful error. In the former case, this is not so easy.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages