Skip to content

Instantly share code, notes, and snippets.

@stuartc
Created March 12, 2012 12:55
Show Gist options
  • Save stuartc/2021665 to your computer and use it in GitHub Desktop.
Save stuartc/2021665 to your computer and use it in GitHub Desktop.

Revisions

  1. stuartc created this gist Mar 12, 2012.
    6 changes: 6 additions & 0 deletions _form.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    form_for(@user, :validate=>true) do |f|
    = f.label :birthdate
    = f.date_select :birthdate
    = f.text_field :birthdate, :type=>"hidden", 'data-validate'=>true
    end

    19 changes: 19 additions & 0 deletions application.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    $('select[id^=user_birthdate]').live('blur', function() {
    setTimeout(function() {
    if ($('select[id^=user_birthdate]:focus').length == 0) {
    // update the hidden date
    var curYear = $('#user_birthdate_1i').val(),
    curMonth = $('#user_birthdate_2i').val(),
    curDay = $('#user_birthdate_3i').val();

    hiddenDate = "";
    if ((curYear * curMonth * curDay) != 0) {
    hiddenDate = curMonth + "-" + curDay + "-" + curYear;
    }
    var elem = $('input[id=user_birthdate]')
    elem.val(hiddenDate);
    elem.data('changed', true); // tells validator it changed
    elem.trigger('focusout'); // manually trigger focus out
    }
    }, 20);
    });