Errores solucionados

This commit is contained in:
Juan Ferrer Toribio 2017-05-26 13:14:47 +02:00
parent f6888af74a
commit a73b2eba0d
3 changed files with 12 additions and 6 deletions

View File

@ -11,3 +11,4 @@ rules:
guard-for-in: 0 guard-for-in: 0
camelcase: 0 camelcase: 0
default-case: 0 default-case: 0
no-eq-null: 0

View File

@ -14,12 +14,15 @@ export const validators = {
min: conf.min || conf.is, min: conf.min || conf.is,
max: conf.max || conf.is max: conf.max || conf.is
}; };
if (!validator.isLength(value, options)) { 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`); 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}`); 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) { numericality: function(value, conf) {
@ -79,7 +82,9 @@ export function validate(value, conf) {
let validator = validators[conf.validation]; let validator = validators[conf.validation];
try { try {
checkNull(value, conf); checkNull(value, conf);
if (validator) validator(value, conf);
if (validator && value != null)
validator(value, conf);
} catch (e) { } catch (e) {
let message = conf.message ? conf.message : e.message; let message = conf.message ? conf.message : e.message;
throw new Error(message); throw new Error(message);
@ -93,7 +98,7 @@ export function validate(value, conf) {
* @param {Object} conf The validation configuration * @param {Object} conf The validation configuration
*/ */
export function checkNull(value, conf) { export function checkNull(value, conf) {
if (typeof value === 'undefined') { if (typeof value === 'undefined' || value === '') {
if (!conf.allowBlank) if (!conf.allowBlank)
throw new Error(`Value can't be blank`); throw new Error(`Value can't be blank`);
} else if (value === null && !conf.allowNull) } else if (value === null && !conf.allowNull)

View File

@ -45,7 +45,7 @@ export default class Textfield extends Component {
this.showClear(this.input.value); this.showClear(this.input.value);
} }
showClear(show) { showClear(show) {
show = document.activeElement === this.input; show = show && document.activeElement === this.input;
let clearButton = this.element.querySelector('button'); let clearButton = this.element.querySelector('button');
clearButton.style.visibility = show ? 'visible' : 'hidden'; clearButton.style.visibility = show ? 'visible' : 'hidden';
} }