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.
Datetime fields with client_side_validations gem
form_for(@user, :validate=>true) do |f|
= f.label :birthdate
= f.date_select :birthdate
= f.text_field :birthdate, :type=>"hidden", 'data-validate'=>true
end
$('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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment