Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

default user input handling for dates could be improved

#3895

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>

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

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. trevor
    trevor

    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 %>
    

    February 8th, 2010 @ 05:26 AM

  2. Rohit Arondekar
    Rohit Arondekar
    • 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.

    February 2nd, 2011 @ 10:25 AM