diff --git a/packages/neoform-validation/src/fieldValidation.js b/packages/neoform-validation/src/fieldValidation.js index 9130926..7057f7d 100644 --- a/packages/neoform-validation/src/fieldValidation.js +++ b/packages/neoform-validation/src/fieldValidation.js @@ -32,9 +32,11 @@ const fieldValidation = (Target) => { } } - validate() { + validate(event) { if (this.props.validator) { - this.context.neoform.validate(this.props.name); + const type = event ? event.type : 'unknown'; + + 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, diff --git a/readme.md b/readme.md index 174d4ec..74c7e35 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 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. To use a validator you should just pass it in a `validator` prop to an individual field: