From 518ffd7341ace45b399a45aa4ad69cd8178e4c2c Mon Sep 17 00:00:00 2001 From: Kate Lizogubova Date: Tue, 12 Dec 2017 18:29:59 +0200 Subject: [PATCH 1/4] :wrench: neoform-validation: add event type support --- packages/neoform-validation/src/fieldValidation.js | 5 +++-- packages/neoform-validation/src/formValidation.js | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/neoform-validation/src/fieldValidation.js b/packages/neoform-validation/src/fieldValidation.js index 9130926..8b4681d 100644 --- a/packages/neoform-validation/src/fieldValidation.js +++ b/packages/neoform-validation/src/fieldValidation.js @@ -32,9 +32,10 @@ const fieldValidation = (Target) => { } } - validate() { + validate(event) { if (this.props.validator) { - this.context.neoform.validate(this.props.name); + const type = event ? event.type : undefined; + this.context.neoform.validate(this.props.name, type); } } diff --git a/packages/neoform-validation/src/formValidation.js b/packages/neoform-validation/src/formValidation.js index 543c560..d050064 100644 --- a/packages/neoform-validation/src/formValidation.js +++ b/packages/neoform-validation/src/formValidation.js @@ -52,11 +52,11 @@ const formValidation = (Target) => { return this.state.fields[name] || {}; } - validateField(name) { + validateField(name, type) { const validator = this.validators[name]; const value = this.context.neoform.getValue(name); - validator(value) + validator(value, type) .then((message) => { this.setState((prevState) => ({ fields: { @@ -90,7 +90,7 @@ const formValidation = (Target) => { const validator = this.validators[name]; const value = this.context.neoform.getValue(name); - return validator(value) + return validator(value, 'submit') .then((message) => { fields[name] = { status: true, From 01e4c421d4bbd440a18869f5ebf2e7027e7f750f Mon Sep 17 00:00:00 2001 From: Kate Lizogubova Date: Tue, 12 Dec 2017 18:30:38 +0200 Subject: [PATCH 2/4] :pencil: update readme --- readme.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 174d4ec..6049d2e 100644 --- a/readme.md +++ b/readme.md @@ -308,7 +308,7 @@ Where: "Validator" is just a Promise. Rejected one is for `validationStatus: false` prop and resolved is for `validationStatus: true`. An optional argument passed to a rejected or fulfilled Promise becomes `validationMessage` prop. ```js -export const requiredValidator = (value) => { +export const requiredValidator = (value, type) => { if (value === '') { return Promise.reject('💩'); } @@ -317,6 +317,11 @@ export const requiredValidator = (value) => { }; ``` +Where: + +* `value` – field value for validation +* `type` – event type. Can be `submit`, `change`, `blur` or anythin you will provide to field `validate` method + It's up to you how to manage multiple validators — with a simple `Promise.all()` or some complex asynchronous sequences — as long as validator returns a single Promise. To use a validator you should just pass it in a `validator` prop to an individual field: From c10ddd16902809bcc7231172a44d8510029dbcf2 Mon Sep 17 00:00:00 2001 From: Kate Lizogubova Date: Tue, 12 Dec 2017 18:47:18 +0200 Subject: [PATCH 3/4] :heavy_check_mark: fix eslint errors --- packages/neoform-validation/src/fieldValidation.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/neoform-validation/src/fieldValidation.js b/packages/neoform-validation/src/fieldValidation.js index 8b4681d..7057f7d 100644 --- a/packages/neoform-validation/src/fieldValidation.js +++ b/packages/neoform-validation/src/fieldValidation.js @@ -34,7 +34,8 @@ const fieldValidation = (Target) => { validate(event) { if (this.props.validator) { - const type = event ? event.type : undefined; + const type = event ? event.type : 'unknown'; + this.context.neoform.validate(this.props.name, type); } } From d3bef3314c959488508092b87cce913c851974ad Mon Sep 17 00:00:00 2001 From: Kate Lizogubova Date: Wed, 13 Dec 2017 11:15:30 +0200 Subject: [PATCH 4/4] :pencil: fix typo in readme --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 6049d2e..74c7e35 100644 --- a/readme.md +++ b/readme.md @@ -320,7 +320,7 @@ export const requiredValidator = (value, type) => { Where: * `value` – field value for validation -* `type` – event type. Can be `submit`, `change`, `blur` or anythin you will provide to field `validate` method +* `type` – event type. Can be `submit`, `change`, `blur` or anything you will provide to field `validate` method It's up to you how to manage multiple validators — with a simple `Promise.all()` or some complex asynchronous sequences — as long as validator returns a single Promise.