From cf87544ce0604020987d70937782618ab53372ba Mon Sep 17 00:00:00 2001 From: Guillaume Potier Date: Mon, 11 Mar 2013 10:02:06 +0100 Subject: [PATCH] - specific behavior when field have required constraint. Do not show required error along another errors, and reciprocally (Closes #142) --- CHANGELOG.md | 3 +++ TODO.md | 1 + dist/parsley-standalone.min.js | 26 +++++++++++++------------- dist/parsley.extend.min.js | 2 +- dist/parsley.min.js | 26 +++++++++++++------------- parsley.js | 9 +++++++++ tests/index.html | 1 + tests/tests.js | 20 ++++++++++++++++++-- 8 files changed, 59 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac85e2ef4..8eab52fd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ **1.1.15 (next version)** + - specific behavior when field have required constraint. Do not show required error + along another errors, and reciprocally (#142) + **1.1.14 (current stable)** - added luhn validator in parsley.extra (#150) diff --git a/TODO.md b/TODO.md index be27a916d..28ccb924d 100644 --- a/TODO.md +++ b/TODO.md @@ -17,6 +17,7 @@ # refactos +* erroManagement bullshit function * [DONE] refacto ugly manageErrors() function * [DONE] stop binding `keypress` and `change` events by default. Must be passed as data arguments. By default, onSubmit validation only diff --git a/dist/parsley-standalone.min.js b/dist/parsley-standalone.min.js index 9850208ab..eecace962 100644 --- a/dist/parsley-standalone.min.js +++ b/dist/parsley-standalone.min.js @@ -1,4 +1,4 @@ -/* Parsley dist/parsley-standalone.min.js build version 1.1.14 http://parsleyjs.org */ +/* Parsley dist/parsley-standalone.min.js build version 1.1.15-dev http://parsleyjs.org */ (function(b){String.prototype.trim===b&&(String.prototype.trim=function(){return this.replace(/^\s+/,"").replace(/\s+$/,"")});Array.prototype.reduce===b&&(Array.prototype.reduce=function(k){if(void 0===this||null===this)throw new TypeError;var q=Object(this),p=q.length>>>0,m=0,a;if("function"!=typeof k)throw new TypeError;if(0==p&&1==arguments.length)throw new TypeError;if(2<=arguments.length)a=arguments[1];else{do{if(m in q){a=q[m++];break}if(++m>=p)throw new TypeError;}while(1)}for(;m",errorElem:"
  • "},listeners:{onFieldValidate:function(){return!1},onFormSubmit:function(){},onFieldError:function(){},onFieldSuccess:function(){}}};b(window).on("load",function(){b('[data-validate="parsley"]').each(function(){b(this).parsley()})})}(window.jQuery|| -window.Zepto); +function(){b(this).remove()}):b(this.ulError).remove()},reset:function(){this.isValid=null;this.removeErrors();this.validatedOnce=!1;this.errorClassHandler.removeClass(this.options.successClass).removeClass(this.options.errorClass);return this},manageError:function(a){b(this.ulError).length||this.manageErrorContainer();if(!("required"===a.name&&null!==this.getVal()&&0",errorElem:"
  • "},listeners:{onFieldValidate:function(){return!1},onFormSubmit:function(){},onFieldError:function(){},onFieldSuccess:function(){}}};b(window).on("load",function(){b('[data-validate="parsley"]').each(function(){b(this).parsley()})})}(window.jQuery||window.Zepto); diff --git a/dist/parsley.extend.min.js b/dist/parsley.extend.min.js index a92c6518c..eae07a296 100644 --- a/dist/parsley.extend.min.js +++ b/dist/parsley.extend.min.js @@ -1,4 +1,4 @@ -/* Parsley dist/parsley.extend.min.js build version 1.1.14 http://parsleyjs.org */ +/* Parsley dist/parsley.extend.min.js build version 1.1.15-dev http://parsleyjs.org */ window.ParsleyConfig=window.ParsleyConfig||{}; (function(d){window.ParsleyConfig=d.extend(!0,{},window.ParsleyConfig,{validators:{minwords:function(a,b){a=a.replace(/(^\s*)|(\s*$)/gi,"");a=a.replace(/[ ]{2,}/gi," ");a=a.replace(/\n /,"\n");a=a.split(" ").length;return a>=b},maxwords:function(a,b){a=a.replace(/(^\s*)|(\s*$)/gi,"");a=a.replace(/[ ]{2,}/gi," ");a=a.replace(/\n /,"\n");a=a.split(" ").length;return a<=b},rangewords:function(a,b){a=a.replace(/(^\s*)|(\s*$)/gi,"");a=a.replace(/[ ]{2,}/gi," ");a=a.replace(/\n /,"\n");a=a.split(" ").length; return a>=b[0]&&a<=b[1]},greaterthan:function(a,b,c){c.options.validateIfUnchanged=!0;return new Number(a)>new Number(d(b).val())},lessthan:function(a,b,c){c.options.validateIfUnchanged=!0;return new Number(a)",errorElem:"
  • "},listeners:{onFieldValidate:function(){return!1},onFormSubmit:function(){},onFieldError:function(){},onFieldSuccess:function(){}}};d(window).on("load",function(){d('[data-validate="parsley"]').each(function(){d(this).parsley()})})}(window.jQuery|| -window.Zepto); +function(){d(this).remove()}):d(this.ulError).remove()},reset:function(){this.isValid=null;this.removeErrors();this.validatedOnce=!1;this.errorClassHandler.removeClass(this.options.successClass).removeClass(this.options.errorClass);return this},manageError:function(a){d(this.ulError).length||this.manageErrorContainer();if(!("required"===a.name&&null!==this.getVal()&&0",errorElem:"
  • "},listeners:{onFieldValidate:function(){return!1},onFormSubmit:function(){},onFieldError:function(){},onFieldSuccess:function(){}}};d(window).on("load",function(){d('[data-validate="parsley"]').each(function(){d(this).parsley()})})}(window.jQuery||window.Zepto); diff --git a/parsley.js b/parsley.js index 107a72851..4db6fd614 100644 --- a/parsley.js +++ b/parsley.js @@ -847,6 +847,15 @@ this.manageErrorContainer(); } + // TODO: refacto properly + // if required constraint but field is not null, do not display + if ( 'required' === constraint.name && null !== this.getVal() && this.getVal().length > 0 ) { + return; + // if empty required field and non required constraint fails, do not display + } else if ( this.isRequired && 'required' !== constraint.name && ( null === this.getVal() || 0 === this.getVal().length ) ) { + return; + } + // TODO: refacto error name w/ proper & readable function var constraintName = constraint.name , liClass = false !== this.options.errorMessage ? 'custom-error-message' : constraintName diff --git a/tests/index.html b/tests/index.html index cf7e1e6e1..f69793152 100644 --- a/tests/index.html +++ b/tests/index.html @@ -149,6 +149,7 @@
    + diff --git a/tests/tests.js b/tests/tests.js index 48c0c3188..d0079ff16 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -234,7 +234,6 @@ var testSuite = function () { } ) it ( 'required - select multiple', function () { expect( $( '#required-selectmultiple' ).parsley( 'validate' ) ).to.be( false ); - $( '#required-selectmultiple' ) } ) it ( 'minlength', function () { triggerSubmitValidation( '#minlength', '12345' ); @@ -600,8 +599,10 @@ var testSuite = function () { } ) it ( 'Change error messages with data-api', function () { triggerSubmitValidation( '#requiredchanged3', '' ); - expect( getErrorMessage( '#requiredchanged3', 'type') ).to.be( 'custom email' ); expect( getErrorMessage( '#requiredchanged3', 'required') ).to.be( 'custom required' ); + + triggerSubmitValidation( '#requiredchanged3', 'foo' ); + expect( getErrorMessage( '#requiredchanged3', 'type') ).to.be( 'custom email' ); } ) it ( 'Change error handler', function () { $( '#errorsmanagement-form' ).parsley( { @@ -631,6 +632,21 @@ var testSuite = function () { test field validation scenarios ***************************************/ describe ( 'Test field validation scenarios', function () { + it ( 'Test multiple constraints/errors required field scenario', function () { + var fieldHash = $( '#scenario-multiple-errors-and-required' ).parsley( 'getHash' ); + expect( $( '#scenario-multiple-errors-and-required' ).parsley( 'validate' ) ).to.be( false ); + expect( $( 'ul#' + fieldHash + ' li' ).length ).to.be( 1 ); + expect( $( 'ul#' + fieldHash + ' li' ).eq( 0 ).hasClass( 'required' ) ).to.be( true ); + + $( '#scenario-multiple-errors-and-required' ).val( 'foo@bar.com' ); + expect( $( '#scenario-multiple-errors-and-required' ).parsley( 'validate' ) ).to.be( false ); + expect( $( 'ul#' + fieldHash + ' li' ).length ).to.be( 1 ); + expect( $( 'ul#' + fieldHash + ' li' ).eq( 0 ).hasClass( 'rangelength' ) ).to.be( true ); + + $( '#scenario-multiple-errors-and-required' ).val( 'foo' ); + expect( $( '#scenario-multiple-errors-and-required' ).parsley( 'validate' ) ).to.be( false ); + expect( $( 'ul#' + fieldHash + ' li' ).length ).to.be( 2 ); + } ) it ( 'Test keyup scenario for non-required field', function () { // do not pass the 3 chars min trigger $( '#scenario-not-required' ).val( 'fo' );