Permalink
Browse files

Prevent setting and removing errors on hidden elements client-side

git-svn-id: http://svn.php.net/repository/pear/packages/HTML_QuickForm2/trunk@325774 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent f5adcdd commit 54c4ea0e5094b33c5913df5d947bf0d722c668c2 @sad-spirit sad-spirit committed May 22, 2012
Showing with 41 additions and 17 deletions.
  1. +5 −5 data/js/min/quickform.js
  2. +18 −6 data/js/quickform.js
  3. +18 −6 js/src/validator.js
View
10 data/js/min/quickform.js
@@ -22,10 +22,10 @@ c&&c.scrollTop||0)-(b.clientTop||0)}!a.which&&a.button&&(a.which=a.button&1?1:a.
qf.events.fixEvent(a).propertyName&&(this._checked_changed=!0)}),qf.events.addListener(a,"click",function(a){this._checked_changed&&(a=qf.events.fixEvent(a),a._type="change",this._checked_changed=!1,b(a))})),a._onchange_attached=!0)});qf.events.test.focusinBubbles?this.addListener(a,"focusout",b,!0):this.addListener(a,"blur",b,!0)}};qf.Rule=function(a,b,c,d){this.callback=a;this.owner=b;this.message=c;this.chained=d||[[]]};qf.LiveRule=function(a,b,c,d,e){qf.Rule.call(this,a,b,c,e);this.triggers=d};qf.LiveRule.prototype=new qf.Rule;qf.LiveRule.prototype.constructor=qf.LiveRule;
qf.Validator=function(a,b){this.rules=b||[];this.errors=new qf.Map;this.classes={error:"error",valid:"valid",message:"error",ancestor:"element"};a.validator=this;qf.events.addListener(a,"submit",qf.Validator.submitHandler);for(var c=0,d;d=this.rules[c];c++)if(d instanceof qf.LiveRule){qf.events.addLiveValidationHandler(a,qf.Validator.liveHandler);break}};qf.Validator.submitHandler=function(a){var a=qf.events.fixEvent(a),b=a.target;b.validator&&!b.validator.run(b)&&a.preventDefault()};
qf.Validator.liveHandler=function(a){a=qf.events.fixEvent(a);if(a.target.form&&a.target.form.validator){var b=a.target.id,c=a.target.form.validator;("change"===(a._type||a.type)||!c._lastTarget||b!==c._lastTarget)&&c.runLive(a);c._lastTarget=b}};
-qf.Validator.prototype={onStart:function(){for(var a=0,b;b=this.rules[a];a++)this.removeRelatedErrors(b)},onFieldError:function(a,b){var c=this.findAncestor(a);qf.classes.add(c,this.classes.error);var d=document.createElement("span");d.className=this.classes.message;d.appendChild(document.createTextNode(b));d.appendChild(document.createElement("br"));if("fieldset"!=c.nodeName.toLowerCase())c.insertBefore(d,c.firstChild);else{var e=c.getElementsByTagName("legend");0==e.length?c.insertBefore(d,c.firstChild):
-e[e.length-1].parentNode.insertBefore(d,e[e.length-1].nextSibling)}},onFieldValid:function(a){qf.classes.add(this.findAncestor(a),this.classes.valid)},onFormValid:function(){},onFormError:function(){},run:function(a){this.onStart(a);this.errors.clear();for(var a=0,b;b=this.rules[a];a++)this.errors.hasKey(b.owner)||this.validate(b);if(this.errors.isEmpty())return this.onFormValid(),!0;this.onFormError();return!1},runLive:function(a){for(var a=" "+a.target.id+" ",b=new qf.Map,c=-1;b.length()>c;)for(var c=
-b.length(),d=0,e;e=this.rules[d];d++)if(e instanceof qf.LiveRule&&!b.hasKey(d))for(var f=0,g;g=e.triggers[f];f++)if(-1<a.indexOf(" "+g+" ")){b.set(d,!0);this.removeRelatedErrors(e);a+=e.triggers.join(" ")+" ";break}for(d=0;e=this.rules[d];d++)b.hasKey(d)&&!this.errors.hasKey(e.owner)&&this.validate(e)},validate:function(a){for(var b=!1,c=a.callback.call(this),d=0,e;e=a.chained[d];d++){for(var f=0,g;(g=e[f])&&!(c=c&&this.validate(g),!c);f++);if(b=b||c)break;c=!0}if(!b&&a.message&&!this.errors.hasKey(a.owner))this.errors.set(a.owner,
-a.message),this.onFieldError(a.owner,a.message);else if(!this.errors.hasKey(a.owner))this.onFieldValid(a.owner);return b},findAncestor:function(a){for(a=document.getElementById(a);!qf.classes.has(a,this.classes.ancestor)&&"fieldset"!=a.nodeName.toLowerCase()&&"form"!=a.nodeName.toLowerCase();)a=a.parentNode;return a},removeRelatedErrors:function(a){var b,c,d,e;b=this.findAncestor(a.owner);this.errors.remove(a.owner);qf.classes.remove(b,[this.classes.error,this.classes.valid]);c=b.getElementsByTagName("span");
-for(b=c.length-1;0<=b;b--)qf.classes.has(c[b],this.classes.message)&&c[b].parentNode.removeChild(c[b]);for(b=0;d=a.chained[b];b++)for(c=0;e=d[c];c++)this.removeRelatedErrors(e)}};qf.rules=qf.rules||{};qf.rules.each=function(a){for(var b=0;b<a.length;b++)if(!a[b]())return!1;return!0};qf.rules.empty=function(a){switch(qf.typeOf(a)){case "array":for(var b=0;b<a.length;b++)if(!qf.rules.empty(a[b]))return!1;return!0;case "undefined":case "null":return!0;default:return""==a}};
+qf.Validator.prototype={onStart:function(){for(var a=0,b;b=this.rules[a];a++)this.removeRelatedErrors(b)},onFieldError:function(a,b){var c=this.findAncestor(a);if(c){qf.classes.add(c,this.classes.error);var d=document.createElement("span");d.className=this.classes.message;d.appendChild(document.createTextNode(b));d.appendChild(document.createElement("br"));if("fieldset"!=c.nodeName.toLowerCase())c.insertBefore(d,c.firstChild);else{var e=c.getElementsByTagName("legend");0==e.length?c.insertBefore(d,
+c.firstChild):e[e.length-1].parentNode.insertBefore(d,e[e.length-1].nextSibling)}}},onFieldValid:function(a){(a=this.findAncestor(a))&&qf.classes.add(a,this.classes.valid)},onFormValid:function(){},onFormError:function(){},run:function(a){this.onStart(a);this.errors.clear();for(var a=0,b;b=this.rules[a];a++)this.errors.hasKey(b.owner)||this.validate(b);if(this.errors.isEmpty())return this.onFormValid(),!0;this.onFormError();return!1},runLive:function(a){for(var a=" "+a.target.id+" ",b=new qf.Map,
+c=-1;b.length()>c;)for(var c=b.length(),d=0,e;e=this.rules[d];d++)if(e instanceof qf.LiveRule&&!b.hasKey(d))for(var f=0,g;g=e.triggers[f];f++)if(-1<a.indexOf(" "+g+" ")){b.set(d,!0);this.removeRelatedErrors(e);a+=e.triggers.join(" ")+" ";break}for(d=0;e=this.rules[d];d++)b.hasKey(d)&&!this.errors.hasKey(e.owner)&&this.validate(e)},validate:function(a){for(var b=!1,c=a.callback.call(this),d=0,e;e=a.chained[d];d++){for(var f=0,g;(g=e[f])&&!(c=c&&this.validate(g),!c);f++);if(b=b||c)break;c=!0}if(!b&&
+a.message&&!this.errors.hasKey(a.owner))this.errors.set(a.owner,a.message),this.onFieldError(a.owner,a.message);else if(!this.errors.hasKey(a.owner))this.onFieldValid(a.owner);return b},findAncestor:function(a){a=document.getElementById(a);if(a.type&&"hidden"===a.type)return null;for(;!qf.classes.has(a,this.classes.ancestor)&&"fieldset"!=a.nodeName.toLowerCase()&&"form"!=a.nodeName.toLowerCase();)a=a.parentNode;return a},removeRelatedErrors:function(a){var b,c,d,e;b=this.findAncestor(a.owner);this.errors.remove(a.owner);
+if(b){qf.classes.remove(b,[this.classes.error,this.classes.valid]);c=b.getElementsByTagName("span");for(b=c.length-1;0<=b;b--)qf.classes.has(c[b],this.classes.message)&&c[b].parentNode.removeChild(c[b])}for(b=0;d=a.chained[b];b++)for(c=0;e=d[c];c++)this.removeRelatedErrors(e)}};qf.rules=qf.rules||{};qf.rules.each=function(a){for(var b=0;b<a.length;b++)if(!a[b]())return!1;return!0};qf.rules.empty=function(a){switch(qf.typeOf(a)){case "array":for(var b=0;b<a.length;b++)if(!qf.rules.empty(a[b]))return!1;return!0;case "undefined":case "null":return!0;default:return""==a}};
qf.rules.nonempty=function(a,b){var c,d=0;if("array"==qf.typeOf(a)){for(c=0;c<a.length;c++)qf.rules.nonempty(a[c],1)&&d++;return d>=b}if(a instanceof qf.Map){var e=a.getValues();if(1==a.length()){c=a.getKeys()[0];var f=e[0];if("[]"==c.slice(-2)&&"array"==qf.typeOf(f))return qf.rules.nonempty(f,b)}for(c=0;c<e.length;c++)qf.rules.nonempty(e[c],1)&&d++;return d>=b}return""!=a&&"undefined"!=qf.typeOf(a)&&"null"!=qf.typeOf(a)};
qf.rules.email=function(a){if(qf.rules.empty(a))return!0;a=a.split("@");if(2!=a.length||64<a[0].length||4>a[1].length||255<a[1].length)return!1;for(var b=a[0].split("."),c=0;c<b.length;c++)if(!/^[A-Za-z0-9!#$%&'*+\/=?^_`{|}~-]+$/.test(b[c]))return!1;return/^([a-z0-9][a-z0-9\-]*[a-z0-9]|[a-z0-9])(\.([a-z0-9][a-z0-9\-]*[a-z0-9]|[a-z0-9])){0,10}\.([a-z]{2,}){1}$/i.test(a[1])};
View
24 data/js/quickform.js
@@ -936,6 +936,9 @@ qf.Validator.prototype = {
onFieldError: function(elementId, errorMessage)
{
var parent = this.findAncestor(elementId);
+ if (!parent) {
+ return
+ }
qf.classes.add(parent, this.classes.error);
var error = document.createElement('span');
@@ -962,7 +965,10 @@ qf.Validator.prototype = {
*/
onFieldValid: function(elementId)
{
- qf.classes.add(this.findAncestor(elementId), this.classes.valid);
+ var ancestor = this.findAncestor(elementId);
+ if (ancestor) {
+ qf.classes.add(ancestor, this.classes.valid);
+ }
},
/**
@@ -1083,6 +1089,10 @@ qf.Validator.prototype = {
findAncestor: function(elementId)
{
var parent = document.getElementById(elementId);
+ // prevent setting anything on hidden elements
+ if (parent.type && 'hidden' === parent.type) {
+ return null;
+ }
while (!qf.classes.has(parent, this.classes.ancestor)
&& 'fieldset' != parent.nodeName.toLowerCase()
&& 'form' != parent.nodeName.toLowerCase()
@@ -1103,12 +1113,14 @@ qf.Validator.prototype = {
parent = this.findAncestor(rule.owner);
this.errors.remove(rule.owner);
- qf.classes.remove(parent, [this.classes.error, this.classes.valid]);
+ if (parent) {
+ qf.classes.remove(parent, [this.classes.error, this.classes.valid]);
- var spans = parent.getElementsByTagName('span');
- for (i = spans.length - 1; i >= 0; i--) {
- if (qf.classes.has(spans[i], this.classes.message)) {
- spans[i].parentNode.removeChild(spans[i]);
+ var spans = parent.getElementsByTagName('span');
+ for (i = spans.length - 1; i >= 0; i--) {
+ if (qf.classes.has(spans[i], this.classes.message)) {
+ spans[i].parentNode.removeChild(spans[i]);
+ }
}
}
for (i = 0; item = rule.chained[i]; i++) {
View
24 js/src/validator.js
@@ -157,6 +157,9 @@ qf.Validator.prototype = {
onFieldError: function(elementId, errorMessage)
{
var parent = this.findAncestor(elementId);
+ if (!parent) {
+ return
+ }
qf.classes.add(parent, this.classes.error);
var error = document.createElement('span');
@@ -183,7 +186,10 @@ qf.Validator.prototype = {
*/
onFieldValid: function(elementId)
{
- qf.classes.add(this.findAncestor(elementId), this.classes.valid);
+ var ancestor = this.findAncestor(elementId);
+ if (ancestor) {
+ qf.classes.add(ancestor, this.classes.valid);
+ }
},
/**
@@ -304,6 +310,10 @@ qf.Validator.prototype = {
findAncestor: function(elementId)
{
var parent = document.getElementById(elementId);
+ // prevent setting anything on hidden elements
+ if (parent.type && 'hidden' === parent.type) {
+ return null;
+ }
while (!qf.classes.has(parent, this.classes.ancestor)
&& 'fieldset' != parent.nodeName.toLowerCase()
&& 'form' != parent.nodeName.toLowerCase()
@@ -324,12 +334,14 @@ qf.Validator.prototype = {
parent = this.findAncestor(rule.owner);
this.errors.remove(rule.owner);
- qf.classes.remove(parent, [this.classes.error, this.classes.valid]);
+ if (parent) {
+ qf.classes.remove(parent, [this.classes.error, this.classes.valid]);
- var spans = parent.getElementsByTagName('span');
- for (i = spans.length - 1; i >= 0; i--) {
- if (qf.classes.has(spans[i], this.classes.message)) {
- spans[i].parentNode.removeChild(spans[i]);
+ var spans = parent.getElementsByTagName('span');
+ for (i = spans.length - 1; i >= 0; i--) {
+ if (qf.classes.has(spans[i], this.classes.message)) {
+ spans[i].parentNode.removeChild(spans[i]);
+ }
}
}
for (i = 0; item = rule.chained[i]; i++) {

0 comments on commit 54c4ea0

Please sign in to comment.