Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Correct for multiple forms on a page that may have identical field names

  • Loading branch information...
commit 47a34ee8229fea0c7e8f470bc8e053ca82fcc673 1 parent 2bed685
@theSteveMitchell authored
Showing with 19 additions and 14 deletions.
  1. +19 −14 vendor/assets/javascripts/rails.validations.js
View
33 vendor/assets/javascripts/rails.validations.js
@@ -325,28 +325,33 @@ var ClientSideValidations = {
}
}
},
- formBuilders: {
+ fformBuilders: {
'ActionView::Helpers::FormBuilder': {
- add: function (element, settings, message) {
- if (element.data('valid') !== false && jQuery('label.message[for="' + element.attr('id') + '"]')[0] === undefined) {
- var inputErrorField = jQuery(settings.input_tag),
+ add: function(element, settings, message) {
+ if (element.data('valid') !== false && jQuery('label.validations_message[for="' + element.attr('id') + '"]')[0] == undefined) {
+
+ var formBeingValidated = element.closest("form")
+ var inputErrorField = jQuery(settings.input_tag),
labelErrorField = jQuery(settings.label_tag),
- label = jQuery('label[for="' + element.attr('id') + '"]:not(.message)');
+ label = jQuery('label[for="' + element.attr('id') + '"]:not(.validations_message)', formBeingValidated);
- if (element.attr('autofocus')) { element.attr('autofocus', false); }
- element.before(inputErrorField);
+ if (element.attr('autofocus')) { element.attr('autofocus', false) };
+ label.after(inputErrorField);
inputErrorField.find('span#input_tag').replaceWith(element);
- inputErrorField.find('label.message').attr('for', element.attr('id'));
- labelErrorField.find('label.message').attr('for', element.attr('id'));
+ inputErrorField.find('label.validations_message').attr('for', element.attr('id'));
+ labelErrorField.find('label.validations_message').attr('for', element.attr('id'));
label.replaceWith(labelErrorField);
labelErrorField.find('label#label_tag').replaceWith(label);
+ //label.toggle();
}
- jQuery('label.message[for="' + element.attr('id') + '"]').text(message);
+ jQuery('label.validations_message[for="' + element.attr('id') + '"]').text(message);
+
},
- remove: function (element, settings) {
- var errorFieldClass = jQuery(settings.input_tag).attr('class'),
+ remove: function(element, settings) {
+ var formBeingValidated = element.closest("form")
+ var errorFieldClass = jQuery(settings.input_tag).attr('class'),
inputErrorField = element.closest('.' + errorFieldClass),
- label = jQuery('label[for="' + element.attr('id') + '"]:not(.message)'),
+ label = jQuery('label[for="' + element.attr('id') + '"]:not(.validations_message)', formBeingValidated),
labelErrorField = label.closest('.' + errorFieldClass);
if (inputErrorField[0]) {
@@ -354,10 +359,10 @@ var ClientSideValidations = {
inputErrorField.replaceWith(element);
label.detach();
labelErrorField.replaceWith(label);
+ //label.toggle();
}
}
},
- },
callbacks: {
element: {
after: function (element, eventData) { },

1 comment on commit 47a34ee

@theSteveMitchell

In our web application, we use devise for authentication, and we have a shared login/registration page, as well as a modal popup for login from the navigation menu. Client_side_validations was getting confused because all three forms use the same model and field names. When adding/removing the validation messages, the js doesn't know which form we're validating, and so the messages were being added to the first form on the page (which was often not visible).

This update restricts the add and remove methods to select only in the current form when replacing labels. Also made some tweaks to prevent the error messages from showing in a separate div, so we can be more flexible on where the validation messages appear.

Please sign in to comment.
Something went wrong with that request. Please try again.