]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
In Abide, properly validate radio buttons, fixes #7196
authorGeoff Kimball <geoff@zurb.com>
Tue, 1 Mar 2016 22:14:38 +0000 (14:14 -0800)
committerGeoff Kimball <geoff@zurb.com>
Tue, 1 Mar 2016 22:14:38 +0000 (14:14 -0800)
js/foundation.abide.js
test/visual/abide/abide-radio.html [new file with mode: 0644]

index cc481b4810c5c18b114a6321cb2599267a3019cb..685045bac8ddc8de8710b4dff92648f1a3e0fee1 100644 (file)
@@ -83,11 +83,6 @@ class Abide {
     var isGood = true;
 
     switch ($el[0].type) {
-      case 'checkbox':
-      case 'radio':
-        isGood = $el[0].checked;
-        break;
-
       case 'select':
       case 'select-one':
       case 'select-multiple':
@@ -220,6 +215,7 @@ class Abide {
       equalTo = this.options.validators.equalTo($el);
     }
 
+
     var goodToGo = [clearRequire, validated, customValidator, equalTo].indexOf(false) === -1;
     var message = (goodToGo ? 'valid' : 'invalid') + '.zf.abide';
 
@@ -286,23 +282,29 @@ class Abide {
    }
 
   /**
-   * Determines whether or a not a radio input is valid based on whether or not it is required and selected
+   * Determines whether or a not a radio input is valid based on whether or not it is required and selected. Although the function targets a single `<input>`, it validates by checking the `required` and `checked` properties of all radio buttons in its group.
    * @param {String} groupName - A string that specifies the name of a radio button group
    * @returns {Boolean} Boolean value depends on whether or not at least one radio input has been selected (if it's required)
    */
   validateRadio(groupName) {
-    var $group = this.$element.find(`:radio[name="${groupName}"]`),
-        counter = [],
-        _this = this;
-
-    $group.each(function(){
-      var rdio = $(this),
-          clear = _this.requiredCheck(rdio);
-      counter.push(clear);
-      if(clear) _this.removeErrorClasses(rdio);
+    // If at least one radio in the group has the `required` attribute, the group is considered required
+    // Per W3C spec, all radio buttons in a group should have `required`, but we're being nice
+    var $group = this.$element.find(`:radio[name="${groupName}"]`);
+    var valid = false;
+
+    // .attr() returns undefined if no elements in $group have the attribute "required"
+    if ($group.attr('required') === undefined) {
+      valid = true;
+    }
+
+    // For the group to be valid, at least one radio needs to be checked
+    $group.each((i, e) => {
+      if ($(e).prop('checked')) {
+        valid = true;
+      }
     });
 
-    return counter.indexOf(false) === -1;
+    return valid;
   }
 
   /**
diff --git a/test/visual/abide/abide-radio.html b/test/visual/abide/abide-radio.html
new file mode 100644 (file)
index 0000000..04332f9
--- /dev/null
@@ -0,0 +1,57 @@
+<!doctype html>
+<!--[if IE 9]><html class="lt-ie10" lang="en" > <![endif]-->
+<html class="no-js" lang="en" dir="ltr">
+  <head>
+    <meta charset="utf-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <title>Foundation for Sites Testing</title>
+    <link href="../assets/css/foundation.css" rel="stylesheet" />
+  </head>
+  <body>
+    <div class="row column">
+      <h1>Abide Radio Buttons</h1>
+
+      <p>This form has required radio buttons.</p>
+      <form id="form" data-abide novalidate>
+        <div class="alert callout hide" data-abide-error>
+          <p>This form has errors.</p>
+        </div>
+        <fieldset>
+          <legend>Fieldset Label</legend>
+          <input required type="radio" name="example1" value="yes" id="example1Yes" />
+          <label for="example1Yes">Yes</label>
+          <input required type="radio" name="example1" value="no" id="example1No" />
+          <label for="example1No">No</label>
+        </fieldset>
+        <button class="button" type="submit">Submit</button>
+        <button class="button" type="reset">Reset</button>
+      </form>
+
+      <hr>
+
+      <p>This form has optional radio buttons.</p>
+      <form id="form" data-abide novalidate>
+        <div class="alert callout hide" data-abide-error>
+          <p>This form has errors.</p>
+        </div>
+        <fieldset>
+          <legend>Fieldset Label</legend>
+          <input type="radio" name="example2" value="yes" id="example2Yes" />
+          <label for="example2Yes">Yes</label>
+          <input type="radio" name="example2" value="no" id="example2No" />
+          <label for="example2No">No</label>
+        </fieldset>
+        <button class="button" type="submit">Submit</button>
+        <button class="button" type="reset">Reset</button>
+      </form>
+
+      <hr>
+    </div>
+
+    <script src="../assets/js/vendor.js"></script>
+    <script src="../assets/js/foundation.js"></script>
+    <script>
+      $(document).foundation();
+    </script>
+  </body>
+</html>