]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
Fix clearing of radio button errors
authorKevin Ball <kmball11@gmail.com>
Thu, 7 Apr 2016 20:56:16 +0000 (13:56 -0700)
committerKevin Ball <kmball11@gmail.com>
Thu, 7 Apr 2016 20:56:16 +0000 (13:56 -0700)
js/foundation.abide.js

index 4201d0b86869a5c42475a350554d2a3b861f6081..c7b874933da61568818e208a7efd8026e1ce6c9b 100644 (file)
@@ -136,6 +136,28 @@ class Abide {
     return $label;
   }
 
+  /**
+   * Get the set of labels associated with a set of radio els in this order
+   * 2. The <label> with the attribute `[for="someInputId"]`
+   * 3. The `.closest()` <label>
+   *
+   * @param {Object} $el - jQuery object to check for required attribute
+   * @returns {Boolean} Boolean value depends on whether or not attribute is checked or empty
+   */
+  findRadioLabels($els) {
+    var labels = $els.map((i, el) => {
+      var id = el.id;
+      var $label = this.$element.find(`label[for="${id}"]`);
+
+      if (!$label.length) {
+        $label = $(el).closest('label');
+      }
+      return $label[0];
+    });
+
+    return $(labels);
+  }
+
   /**
    * Adds the CSS error class as specified by the Abide settings to the label, input, and the form
    * @param {Object} $el - jQuery object to add the class to
@@ -155,11 +177,39 @@ class Abide {
     $el.addClass(this.options.inputErrorClass).attr('data-invalid', '');
   }
 
+  /**
+   * Remove CSS error classes etc from an entire radio button group
+   * @param {String} groupName - A string that specifies the name of a radio button group
+   *
+   */
+
+  removeRadioErrorClasses(groupName) {
+    var $els = this.$element.find(`:radio[name="${groupName}"]`);
+    var $labels = this.findRadioLabels($els);
+    var $formErrors = this.findFormError($els);
+
+    if ($labels.length) {
+      $labels.removeClass(this.options.labelErrorClass);
+    }
+
+    if ($formErrors.length) {
+      $formErrors.removeClass(this.options.formErrorClass);
+    }
+
+    $els.removeClass(this.options.inputErrorClass).removeAttr('data-invalid');
+
+  }
+
   /**
    * Removes CSS error class as specified by the Abide settings from the label, input, and the form
    * @param {Object} $el - jQuery object to remove the class from
    */
   removeErrorClasses($el) {
+    // radios need to clear all of the els
+    if($el[0].type == 'radio') {
+      return this.removeRadioErrorClasses($el.attr('name'));
+    }
+
     var $label = this.findLabel($el);
     var $formError = this.findFormError($el);