From b315090a1708b0a9287b6f8fc560c1aebe9e07cf Mon Sep 17 00:00:00 2001 From: Pierre Besson Date: Thu, 13 Aug 2015 14:13:47 +0200 Subject: [PATCH 1/2] [validation] When the field is empty the validatore are not applied. Only the requirement is tested. --- .../field/mixin/validation-behaviour.js | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/common/field/mixin/validation-behaviour.js b/src/common/field/mixin/validation-behaviour.js index 47c2b86b1..af315f32f 100644 --- a/src/common/field/mixin/validation-behaviour.js +++ b/src/common/field/mixin/validation-behaviour.js @@ -11,6 +11,11 @@ let validationMixin ={ validator: undefined }; }, + /** + * Compute the validation status and merge all errors into one. + * @param {object} validationStatus - The result from the validation. + * @return {true | string} - true if the validation is ok and a message if it is not the case. + */ _computeValidationStatus(validationStatus){ if(validationStatus.isValid){ return true; @@ -22,35 +27,37 @@ let validationMixin ={ * @return {object} */ validateInput: function validateInputText() { - var value = this.getValue(); - if (this.props.isRequired && (value === undefined || value === '')) { - return this.i18n('field.required', {name: this.i18n(this.props.label)}); - } - if (this.props.validator) { - var validStat = this._computeValidationStatus(validate({ - value: value, - name: this.i18n(this.props.label) - }, - this.props.validator - )); - if(validStat !== true){ - validStat = this.i18n(validStat); + let value = this.getValue(); + let {isRequired, validator, label} = this.props; + if (isRequired && (undefined === value || '' === value)) { + return this.i18n('field.required', {name: this.i18n(label)}); } - return validStat; - } - return true; + console.log('validation', label, 'value', value, 'validator', validator); + //The validation is performed only when the field has a value, otherwise, only the required validation is performed. + if (validator && undefined !== value) { + let validStat = this._computeValidationStatus(validate({ + value: value, + name: this.i18n(label) + }, + validator + )); + if(true !== validStat){ + validStat = this.i18n(validStat); + } + return validStat; + } + return true; }, /** * Validate the field. * @return {object} - undefined if valid, {name: "errors"} if not valid. */ validate: function validateField() { - var validationStatus = this.validateInput(); - if(validationStatus !== true){ - this.setError(validationStatus); - return validationStatus; - } - return; + let validationStatus = this.validateInput(); + if(true !== validationStatus){ + this.setError(validationStatus); + return validationStatus; + } }, /** * Set the error on the field. From bfb3c2beb4b86cc9d66a6ae43d5832c13dc6ead7 Mon Sep 17 00:00:00 2001 From: Pierre Besson Date: Thu, 13 Aug 2015 14:15:01 +0200 Subject: [PATCH 2/2] Update example on validation. --- example/js/initFocus.js | 17 ++++++++++------- src/common/form/example/index.html | 1 + 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/example/js/initFocus.js b/example/js/initFocus.js index 981f68a37..075146516 100644 --- a/example/js/initFocus.js +++ b/example/js/initFocus.js @@ -3,10 +3,13 @@ var domain = { style: "do_text", type: "text", component: "PapaSinge", - validation: [{ + validator: [{ type: "function", - value: function () { - return false; + options:{ + translationKey: 'domain.doTEXT.test' + }, + value: function (d) { + return _.isString(d); } }] }, @@ -14,7 +17,7 @@ var domain = { style: "do_email", type: "email", component: "PapaMail", - validation: [{ + validator: [{ type: "function", value: function () { return true; @@ -46,9 +49,9 @@ var entities = { "firstName": { "domain": "DO_TEXT", "required": false, - "validator": function (data) { - return data.length <= 3 ? "le champ doit dépasser la taille de 3" : true; - } + "validator": [{options:{translationKey: 'entityContactValidation.test'}, type:'function', value: function (data) { + return data.length <= 3 ? false : true; + }}] }, "lastName": { "domain": "DO_TEXT", diff --git a/src/common/form/example/index.html b/src/common/form/example/index.html index d24754869..5637135b7 100644 --- a/src/common/form/example/index.html +++ b/src/common/form/example/index.html @@ -17,6 +17,7 @@ +