From a73b2eba0d2d428c821abc15c59a791599e45deb Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Fri, 26 May 2017 13:14:47 +0200 Subject: [PATCH] Errores solucionados --- .eslintrc.yml | 1 + client/core/src/lib/validator.js | 15 ++++++++++----- client/core/src/textfield/index.js | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 762ccaad1d..8ea0fe0fe4 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -11,3 +11,4 @@ rules: guard-for-in: 0 camelcase: 0 default-case: 0 + no-eq-null: 0 diff --git a/client/core/src/lib/validator.js b/client/core/src/lib/validator.js index b27fdf8d85..3da350a3ec 100644 --- a/client/core/src/lib/validator.js +++ b/client/core/src/lib/validator.js @@ -14,12 +14,15 @@ export const validators = { min: conf.min || conf.is, max: conf.max || conf.is }; - if (!validator.isLength(value, options)) { - if (conf.is) + if (conf.is || conf.min === conf.max) throw new Error(`Value should be ${conf.is} characters long`); - else + else if (conf.min !== conf.max) throw new Error(`Value should have a length between ${conf.min} and ${conf.max}`); + else if (conf.min) + throw new Error(`Value should have at least ${conf.min} characters`); + else + throw new Error(`Value should have at most ${conf.min} characters`); } }, numericality: function(value, conf) { @@ -79,7 +82,9 @@ export function validate(value, conf) { let validator = validators[conf.validation]; try { checkNull(value, conf); - if (validator) validator(value, conf); + + if (validator && value != null) + validator(value, conf); } catch (e) { let message = conf.message ? conf.message : e.message; throw new Error(message); @@ -93,7 +98,7 @@ export function validate(value, conf) { * @param {Object} conf The validation configuration */ export function checkNull(value, conf) { - if (typeof value === 'undefined') { + if (typeof value === 'undefined' || value === '') { if (!conf.allowBlank) throw new Error(`Value can't be blank`); } else if (value === null && !conf.allowNull) diff --git a/client/core/src/textfield/index.js b/client/core/src/textfield/index.js index d556779067..af4d7c4ab4 100644 --- a/client/core/src/textfield/index.js +++ b/client/core/src/textfield/index.js @@ -45,7 +45,7 @@ export default class Textfield extends Component { this.showClear(this.input.value); } showClear(show) { - show = document.activeElement === this.input; + show = show && document.activeElement === this.input; let clearButton = this.element.querySelector('button'); clearButton.style.visibility = show ? 'visible' : 'hidden'; }