This project is archived and is in readonly mode.

#3895 ✓stale
trevor

default user input handling for dates could be improved

Reported by trevor | February 8th, 2010 @ 04:09 AM

i've been moving away from the date helpers (date_select) towards html5 form support. one of the side effects of this is needing more flexible handling of the free text format of dates put in by users. it'd be nice if this had some more intelligence to it by default – for example, "01/02/03" is interpreted as the year 0001.

Loading development environment (Rails 3.0.0.beta)
irb(main):001:0> p = Post.new
=> #<Post id: nil, my_date: nil, created_at: nil, updated_at: nil>
irb(main):002:0> p.my_date = '01/02/2003'
=> "01/02/2003"
irb(main):003:0> p
=> #<Post id: nil, my_date: "2003-02-01", created_at: nil, updated_at: nil>
irb(main):004:0> p.my_date = '01/02/03'
=> "01/02/03"
irb(main):005:0> p
=> #<Post id: nil, my_date: "0001-02-03", created_at: nil, updated_at: nil>
<div class="field">
  <%= f.label :my_date %><br />
  <%= f.text_field :my_date, :type => "date" %>
</div>

Comments and changes to this ticket

  • trevor

    trevor February 8th, 2010 @ 05:26 AM

    this is what i'm currently doing to get the dates to save and display correctly without modifying generated scaffolding -

    layout

    <!DOCTYPE html>
    <html>
    <head>
      <title>Posts: <%= controller.action_name %></title>
      <%= stylesheet_link_tag 'scaffold' %>
      <%= stylesheet_link_tag 'jquery/css/smoothness/jquery-ui-1.7.2.custom.css' %>
      <%= javascript_include_tag :defaults %>
      <%= javascript_include_tag 'jquery/js/jquery-1.3.2.min.js', 'jquery/js/jquery-ui-1.7.2.custom.min.js' %>
      <script>
        var $j = jQuery.noConflict();
        $j.datepicker.setDefaults({ dateFormat: 'yy-mm-dd', constrainInput: true, showOn: 'both', buttonImage: '<%= image_path('calendar.gif') %>', buttonImageOnly: true });
      </script>
    
      <%= csrf_meta_tag %>
    </head>
    <body>
    
    <p class="notice"><%= notice %></p>
    
    <%= yield %>
    
    </body>
    </html>
    

    form

    <% form_for(@post) do |f| %>
      <%= f.error_messages %>
    
      <script>
        // test for HTML5 input type="date" support
        var i = document.createElement("input");
        i.setAttribute("type", "date");
        if (i.type == "text") {
          // type="date" unsupported
          $j(function() {
            $j("#post_my_date").datepicker();
          });
        }
      </script>
      
      <div class="field">
        <%= f.label :my_date %><br />
        <%= f.text_field :my_date, :type => "date", :placeholder => "My Date", :value => (f.object.my_date.strftime("%Y-%m-%d") if f.object.my_date) %>
      </div>
      
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
    
  • Rohit Arondekar

    Rohit Arondekar February 2nd, 2011 @ 10:25 AM

    • State changed from “new” to “stale”
    • Importance changed from “” to “Low”

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

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>

Pages