From 21674e425d649eae1eafdf878dbbcef1ba758785 Mon Sep 17 00:00:00 2001 From: Ben Boyle Date: Wed, 9 Apr 2014 01:05:04 +1000 Subject: [PATCH] refactor conditional logic for duplicate name #40 - only check for radio buttons (skips more elements) - bump version to 1.1.1 --- bower.json | 2 +- dist/form-validation.js | 11 ++++++----- dist/form-validation.min.js | 4 ++-- package.json | 2 +- src/form-validation.js | 9 +++++---- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/bower.json b/bower.json index c0d2a86..b72b1ed 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "form-validation", - "version": "1.1.0", + "version": "1.1.1", "homepage": "https://github.com/bboyle/form-validation", "authors": [ "Ben Boyle " diff --git a/dist/form-validation.js b/dist/form-validation.js index 744889a..bcc75d1 100644 --- a/dist/form-validation.js +++ b/dist/form-validation.js @@ -1,4 +1,4 @@ -/*! Form validation - v1.1.0 - 2014-04-09 +/*! Form validation - v1.1.1 - 2014-04-09 * https://github.com/bboyle/form-validation * Copyright (c) 2014 Ben Boyle; Licensed MIT */ (function( $ ) { @@ -143,11 +143,12 @@ return false; } - if ( ! invalidFields.cache ) { - invalidFields.cache = {}; + // only check radio button groups once (skip individual radio button) + if ( this.type === 'radio' ) { + if ( ! invalidFields.cache ) { + invalidFields.cache = {}; - } else if ( this.type === 'radio' ) { - if ( invalidFields.cache[ this.name ] === true ) { + } else if ( invalidFields.cache[ this.name ] === true ) { return false; } invalidFields.cache[ this.name ] = true; diff --git a/dist/form-validation.min.js b/dist/form-validation.min.js index 48ae5a2..9e5378e 100644 --- a/dist/form-validation.min.js +++ b/dist/form-validation.min.js @@ -1,4 +1,4 @@ -/*! Form validation - v1.1.0 - 2014-04-09 +/*! Form validation - v1.1.1 - 2014-04-09 * https://github.com/bboyle/form-validation * Copyright (c) 2014 Ben Boyle; Licensed MIT */ -!function(a){"use strict";var b=1e4,c='

Please check your answers

    ',d="input, select, textarea",e=function(){return!(this.disabled||this.validity.valid)},f="formValidation",g=function(a,b){var c=this.data(f)||this.data(f,{}).data(f);return"undefined"!=typeof a?"undefined"!=typeof b?(c[a]=b,b):"undefined"!=typeof c[a]?c[a]:null:c},h=function(b,c){return this.map(function(d,e){var f=a(e),g=null,h=null;return"object"==typeof c&&"group"===c.level?h=f.formValidation("group").find(b)[0]:f.is(":radio, :checkbox")?h=f.closest("fieldset").find(b)[0]:(g=f.closest("form").find('label[for="'+e.id+'"]'),h=g.children(b)[0],h||".hint"===b&&(g.append(''),h=g.children(b)[0])),h})},i=function(){var b,c,f=a(this),g=f.formValidation("alert");this.validity.valid?(f.formValidation("question").find(".alert").filter(g).length>0?g.remove():(c=f.formValidation("group").find(d).filter(e),c.length>0?g.text(c.formValidation("getValidationMessage")):g.remove()),f.parentsUntil("form",".invalid").filter(function(){return 0===a(this).find(d).filter(e).length}).removeClass("invalid").find(".alert").remove()):(0===g.length&&(g=a('')),g.text(f.formValidation("getValidationMessage")),f.formValidation("group").hasClass("atomic")&&(b={level:"group"}),f.formValidation("label",b).parent().find('.label, abbr[title="(required)"]').eq(-1).after(g))},j=function(){var b=a(this).closest("form"),e=b.find(d).filter(function j(){if(this.disabled)return!1;if(j.cache){if("radio"===this.type){if(j.cache[this.name]===!0)return!1;j.cache[this.name]=!0}}else j.cache={};return this.validity&&!this.validity.valid}),f=g.call(b,"summaryElement")||g.call(b,"summaryElement",a(c)),h=f.find("ol"),i=!0;return e.length>0&&(h.find("li").remove(),e.each(function(){var b,c,d=a(this),e=d.formValidation("group"),f=d.formValidation("label",{level:e.length>0?"group":null});b=f.length>0?f[0].id||f.generateId("label-"+this.id)[0].id:this.name,c=g.call(d,"summaryElement")||g.call(d,"summaryElement",a('
  1. ')),0===e.length||e[0]!==i?(i=e[0],c.find("a").text(f.text().replace(/\?$/,"")+": "+d.formValidation("getValidationMessage")).end().appendTo(h)):c.remove()})),e.length},k=function(b){var c=j.call(this),f=a(this);return f.find(".invalid").filter(function(){return 0===a(this).find(d).filter(e).length}).removeClass("invalid").find(".alert").remove(),c>0?(b.stopImmediatePropagation(),b.preventDefault(),function(b){var c=g.call(b,"summaryElement");b.prev(".status").not(c).remove(),b.before(c.fadeIn()),a(window).scrollTop(c.offset().top)}(f),f.find(d).filter(e).each(function(){i.call(this)}).parentsUntil("form",".questions > li").not(".section, .compact").addClass("invalid"),f.trigger("x-invalid"),!1):void 0},l=function(c){var d,e=c.timeStamp||a.now(),f=a(this),h=g.call(f,"summaryElement");return h&&h.remove(),d=g.call(f,"lastSubmitTimeStamp"),d&&b>e-d?(c.stopImmediatePropagation(),c.preventDefault(),!1):void g.call(f,"lastSubmitTimeStamp",e)},m={alert:function(){return this.map(function(b,c){var d,e=a(c);return e.is(":radio, :checkbox")===!0?e.closest("fieldset").find("legend > .alert")[0]:(d=e.formValidation("group").filter(".atomic"),d.length>0?d.find("legend > .alert")[0]:a('label[for="'+c.id+'"] > .alert')[0])})},label:function(a){return h.call(this,".label",a)},hint:function(a){return h.call(this,".hint",a)},question:function(b){return"object"==typeof b&&"group"===b.level?this.formValidation("group"):this.map(function(b,c){return a(c).parentsUntil("form",".questions > li")[0]})},group:function(){return this.map(function(b,c){return a(c).parentsUntil("form",".group").filter(function(){return a(this).children("fieldset").length>0})[0]})},validate:function(){return this.each(function(){a(this).closest("form").attr("novalidate",!0).unbind("submit",l).unbind("submit",k).bind("submit",k).bind("submit",l).find(d).unbind("change",i).bind("change",i)})},getValidationMessage:function(){var a=this[0].validity;return"undefined"==typeof a||a.valid===!0?"":a.valueMissing?"Must be completed":a.customError?this[0].validationMessage:a.typeMismatch?"Must be an email address":a.patternMismatch?"Must use the format shown":"Must be a valid answer"}};a.fn.formValidation=function(b){return m[b]?m[b].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof b&&b?void a.error("Method "+b+" does not exist on jQuery.formValidation"):m.init.apply(this,arguments)},a.fn.forcesForms=a.fn.formValidation}(jQuery); \ No newline at end of file +!function(a){"use strict";var b=1e4,c='

    Please check your answers

      ',d="input, select, textarea",e=function(){return!(this.disabled||this.validity.valid)},f="formValidation",g=function(a,b){var c=this.data(f)||this.data(f,{}).data(f);return"undefined"!=typeof a?"undefined"!=typeof b?(c[a]=b,b):"undefined"!=typeof c[a]?c[a]:null:c},h=function(b,c){return this.map(function(d,e){var f=a(e),g=null,h=null;return"object"==typeof c&&"group"===c.level?h=f.formValidation("group").find(b)[0]:f.is(":radio, :checkbox")?h=f.closest("fieldset").find(b)[0]:(g=f.closest("form").find('label[for="'+e.id+'"]'),h=g.children(b)[0],h||".hint"===b&&(g.append(''),h=g.children(b)[0])),h})},i=function(){var b,c,f=a(this),g=f.formValidation("alert");this.validity.valid?(f.formValidation("question").find(".alert").filter(g).length>0?g.remove():(c=f.formValidation("group").find(d).filter(e),c.length>0?g.text(c.formValidation("getValidationMessage")):g.remove()),f.parentsUntil("form",".invalid").filter(function(){return 0===a(this).find(d).filter(e).length}).removeClass("invalid").find(".alert").remove()):(0===g.length&&(g=a('')),g.text(f.formValidation("getValidationMessage")),f.formValidation("group").hasClass("atomic")&&(b={level:"group"}),f.formValidation("label",b).parent().find('.label, abbr[title="(required)"]').eq(-1).after(g))},j=function(){var b=a(this).closest("form"),e=b.find(d).filter(function j(){if(this.disabled)return!1;if("radio"===this.type){if(j.cache){if(j.cache[this.name]===!0)return!1}else j.cache={};j.cache[this.name]=!0}return this.validity&&!this.validity.valid}),f=g.call(b,"summaryElement")||g.call(b,"summaryElement",a(c)),h=f.find("ol"),i=!0;return e.length>0&&(h.find("li").remove(),e.each(function(){var b,c,d=a(this),e=d.formValidation("group"),f=d.formValidation("label",{level:e.length>0?"group":null});b=f.length>0?f[0].id||f.generateId("label-"+this.id)[0].id:this.name,c=g.call(d,"summaryElement")||g.call(d,"summaryElement",a('
    1. ')),0===e.length||e[0]!==i?(i=e[0],c.find("a").text(f.text().replace(/\?$/,"")+": "+d.formValidation("getValidationMessage")).end().appendTo(h)):c.remove()})),e.length},k=function(b){var c=j.call(this),f=a(this);return f.find(".invalid").filter(function(){return 0===a(this).find(d).filter(e).length}).removeClass("invalid").find(".alert").remove(),c>0?(b.stopImmediatePropagation(),b.preventDefault(),function(b){var c=g.call(b,"summaryElement");b.prev(".status").not(c).remove(),b.before(c.fadeIn()),a(window).scrollTop(c.offset().top)}(f),f.find(d).filter(e).each(function(){i.call(this)}).parentsUntil("form",".questions > li").not(".section, .compact").addClass("invalid"),f.trigger("x-invalid"),!1):void 0},l=function(c){var d,e=c.timeStamp||a.now(),f=a(this),h=g.call(f,"summaryElement");return h&&h.remove(),d=g.call(f,"lastSubmitTimeStamp"),d&&b>e-d?(c.stopImmediatePropagation(),c.preventDefault(),!1):void g.call(f,"lastSubmitTimeStamp",e)},m={alert:function(){return this.map(function(b,c){var d,e=a(c);return e.is(":radio, :checkbox")===!0?e.closest("fieldset").find("legend > .alert")[0]:(d=e.formValidation("group").filter(".atomic"),d.length>0?d.find("legend > .alert")[0]:a('label[for="'+c.id+'"] > .alert')[0])})},label:function(a){return h.call(this,".label",a)},hint:function(a){return h.call(this,".hint",a)},question:function(b){return"object"==typeof b&&"group"===b.level?this.formValidation("group"):this.map(function(b,c){return a(c).parentsUntil("form",".questions > li")[0]})},group:function(){return this.map(function(b,c){return a(c).parentsUntil("form",".group").filter(function(){return a(this).children("fieldset").length>0})[0]})},validate:function(){return this.each(function(){a(this).closest("form").attr("novalidate",!0).unbind("submit",l).unbind("submit",k).bind("submit",k).bind("submit",l).find(d).unbind("change",i).bind("change",i)})},getValidationMessage:function(){var a=this[0].validity;return"undefined"==typeof a||a.valid===!0?"":a.valueMissing?"Must be completed":a.customError?this[0].validationMessage:a.typeMismatch?"Must be an email address":a.patternMismatch?"Must use the format shown":"Must be a valid answer"}};a.fn.formValidation=function(b){return m[b]?m[b].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof b&&b?void a.error("Method "+b+" does not exist on jQuery.formValidation"):m.init.apply(this,arguments)},a.fn.forcesForms=a.fn.formValidation}(jQuery); \ No newline at end of file diff --git a/package.json b/package.json index 983db43..dec7fe5 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "form-validation", "title": "Form validation", "description": "Validation UI for web forms using the HTML5 constraint validation API", - "version": "1.1.0", + "version": "1.1.1", "homepage": "https://github.com/bboyle/form-validation", "author": { "name": "Ben Boyle", diff --git a/src/form-validation.js b/src/form-validation.js index 0c14d1d..0d487c0 100644 --- a/src/form-validation.js +++ b/src/form-validation.js @@ -150,11 +150,12 @@ return false; } - if ( ! invalidFields.cache ) { - invalidFields.cache = {}; + // only check radio button groups once (skip individual radio button) + if ( this.type === 'radio' ) { + if ( ! invalidFields.cache ) { + invalidFields.cache = {}; - } else if ( this.type === 'radio' ) { - if ( invalidFields.cache[ this.name ] === true ) { + } else if ( invalidFields.cache[ this.name ] === true ) { return false; } invalidFields.cache[ this.name ] = true;