Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // client_side_validations rails gem doesn't handle radio buttons, so manually process those with this code
- // this code assumes that your related radio buttons are wrapped in a div with class .radio
- function validate_radio(validations, $radio) {
- var input_wrap = window.ClientSideValidations.forms.new_user.input_tag.split('<span id="input_tag" />');
- var label_wrap = window.ClientSideValidations.forms.new_user.label_tag.split('<label id="label_tag" />');
- if ($radio.find('input[type="radio"]:checked').length) {
- // buttons were checked, so if a previous error message was displayed then get rid of it.
- $radio.find('.field_with_errors > *').unwrap();
- $radio.find('.message').remove();
- } else {
- // no radio buttons in this group were checked so see if we have a presence validator
- var name = ($radio.find('input[type="radio"]').attr('name'));
- if (validations.validators[name] && validations.validators[name].presence.length) {
- var message = '<label class="message">' + validations.validators[name].presence[0].message + '</label>';
- // if we don't already have an error message, then display it
- if (!$radio.find('field_with_errors').length) {
- $radio.find('label').first().wrap(label_wrap[0]);
- $radio.find('> label, > input').wrapAll(input_wrap[0]).parent().append(message);
- }
- }
- }
- }
- $('form').each(function() {
- var validations = window.ClientSideValidations.forms[$(this).attr('id')];
- if (validations) {
- $(this).submit(function() {
- // check all radio buttons on submit
- $(this).find('.radio').each(function() {
- validate_radio(validations, $(this));
- });
- });
- $(this).find('.radio input[type="radio"]').blur(function() {
- validate_radio(validations, $(this).parents('.radio'));
- });
- }
- });
Add Comment
Please, Sign In to add comment