diff --git a/src/form.field.js b/src/form.field.js index 61250716..25368474 100644 --- a/src/form.field.js +++ b/src/form.field.js @@ -154,8 +154,9 @@ export default class Field { } @action - validate(force = false, showErrors = true, form = null) { + validate(showErrors = true, form = null) { if (form) this.form = form; + this.setValid(); // exit on silent mode (on reset and clear) if (this.silent === true) { @@ -164,10 +165,7 @@ export default class Field { } // not execute if no valid function or ajv rules - if (!this.validateProperty && !this.form.ajv) { - this.setValid(); - return; - } + if (!this.validateProperty && !this.form.ajv) return; // Use "ajv" Rules if (this.form.ajv) this.handleAjvValidationRules(showErrors); @@ -196,9 +194,6 @@ export default class Field { return; } } - - this.setValid(); - return; } handleValidateProperty(showErrors) { diff --git a/src/form.js b/src/form.js index 10108187..8902681c 100644 --- a/src/form.js +++ b/src/form.js @@ -75,19 +75,19 @@ export default class Form { this.validateField(key, true))); } - validateFields(force = true, showErrors = true) { + validateFields(showErrors = true) { _.forEach(this.fields, (field) => - field.validate(force, showErrors, this)); + field.validate(showErrors, this)); } - validateField(key = null, recursive = false, force = true, showErrors = true) { + validateField(key = null, recursive = false, showErrors = true) { if (!key) throw new Error('validateField: No field key provided'); // validate field by key - this.fields[key].validate(force, showErrors, this); + this.fields[key].validate(showErrors, this); /* - validate related fields if specified + validate 'related' fields if specified and recursive validation allowed */ if (!recursive) return; diff --git a/tests/data/form.m.js b/tests/data/form.m.js new file mode 100644 index 00000000..d9698b23 --- /dev/null +++ b/tests/data/form.m.js @@ -0,0 +1,27 @@ +import Form from '../../src/index'; + +const fields = { + username: { + label: 'Username', + value: 'SteveJobs', + }, + email: { + label: 'Email', + value: 's.jobs@apple.com', + }, + password: { + label: 'Password', + value: 'thinkdifferent', + }, +}; + +const schema = { + type: 'object', + properties: { + username: { type: 'string', minLength: 6, maxLength: 20 }, + email: { type: 'string', format: 'email', minLength: 5, maxLength: 20 }, + password: { type: 'string', minLength: 6, maxLength: 20 }, + }, +}; + +export default new Form({ fields, schema }); diff --git a/tests/test.js b/tests/test.js index 7444cd27..02680084 100644 --- a/tests/test.js +++ b/tests/test.js @@ -11,13 +11,18 @@ import $G from './data/form.g.js'; import $H from './data/form.h.js'; import $I from './data/form.i.js'; import $L from './data/form.l.js'; +import $M from './data/form.m.js'; -// do some stuff +// do some stuff (DIFFERENT FORMS) $C.invalidate('The user already exist'); $D.update({ username: 'JonathanIve' }); $I.update({ username: 'JonathanIve' }); -$I.reset(); -$L.clear(); +$I.reset(); // to original values +$L.clear(); // to empty values + +// subsequent clear and reset (SAME FORM) +$M.clear(); // to empty values +$M.reset(); // to original values describe('isValid', () => { it('$A isValid should be true', () => expect($A.isValid).to.be.true); @@ -29,7 +34,8 @@ describe('isValid', () => { it('$G isValid should be true', () => expect($G.isValid).to.be.true); it('$H isValid should be true', () => expect($H.isValid).to.be.true); it('$I isValid should be true', () => expect($I.isValid).to.be.true); - it('$L isDirty should be false', () => expect($L.isValid).to.be.false); + it('$L isValid should be false', () => expect($L.isValid).to.be.false); + it('$M isValid should be true', () => expect($M.isValid).to.be.true); }); describe('isDirty', () => { @@ -43,6 +49,7 @@ describe('isDirty', () => { it('$H isDirty should be true', () => expect($H.isDirty).to.be.true); it('$I isDirty should be true', () => expect($I.isDirty).to.be.true); it('$L isDirty should be false', () => expect($L.isDirty).to.be.false); + it('$M isDirty should be true', () => expect($M.isDirty).to.be.true); }); describe('validate()', () => { @@ -54,6 +61,8 @@ describe('validate()', () => { it('$G validate() should return true', () => expect($G.validate()).to.be.true); it('$H validate() should return true', () => expect($H.validate()).to.be.true); it('$I validate() should return true', () => expect($I.validate()).to.be.true); + it('$L validate() should return false', () => expect($L.validate()).to.be.false); + it('$M validate() should return true', () => expect($M.validate()).to.be.true); }); describe('fieldKeys()', () => {