var isGood = true;
switch ($el[0].type) {
- case 'checkbox':
- case 'radio':
- isGood = $el[0].checked;
- break;
-
case 'select':
case 'select-one':
case 'select-multiple':
equalTo = this.options.validators.equalTo($el);
}
+
var goodToGo = [clearRequire, validated, customValidator, equalTo].indexOf(false) === -1;
var message = (goodToGo ? 'valid' : 'invalid') + '.zf.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;
}
/**
--- /dev/null
+<!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>