NewOldMax / react-form-validator-core

Core validator component for react forms
MIT License
95 stars 44 forks source link

Unhandled Promise Rejection on using resetValidations() #70

Closed coolbeatz71 closed 4 years ago

coolbeatz71 commented 4 years ago

I am having a form for signing, having as state

this.state = {
  email: '',
  fullName: '',
}

I am using the ValidatorForm alongside the ValidatorComponent. And it is working successfully.

But I would like to clean my fields values on success submit, the idea was naturally to clean state value as follow:

handleSubmit = () => {
    ...call the backend
    setState({
       email: '',
       fullName: '',
    })  
};

This is retriggering the validation process, causing the required field message to reappear.

I have got the idea of using ValidationForm.resetValidations(); to reset all validation

And now my handleSubmit() methods looks like this:

import { ValidatorForm } from 'react-form-validator-core';

...

handleSubmit = () => {
    ...call the backend
    ValidationForm.resetValidations();
    setState({
       email: '',
       fullName: '',
    })  
};

But I am getting this error:

Possible Unhandled Promise Rejection: TypeError: react_form_validator_core__WEBPACK_IMPORTED_MODULE_3__.ValidatorForm.resetValidations is not a function
    at Object.Contact.handleSubmit [as onSubmit] (Contact.jsx:93)
    at ValidatorForm.js:83
    at index.js:53
    at run (setImmediate.js:40)
    at runIfPresent (setImmediate.js:69)
    at onGlobalMessage (setImmediate.js:109)
coolbeatz71 commented 4 years ago

I am having a form for signing, having as state

this.state = {
  email: '',
  fullName: '',
}

I am using the ValidatorForm alongside the ValidatorComponent. And it is working successfully.

But I would like to clean my fields values on success submit, the idea was naturally to clean state value as follow:

handleSubmit = () => {
    ...call the backend
    setState({
       email: '',
       fullName: '',
    })  
};

This is retriggering the validation process, causing the required field message to reappear.

I have got the idea of using ValidationForm.resetValidations(); to reset all validation

And now my handleSubmit() methods looks like this:

import { ValidatorForm } from 'react-form-validator-core';

...

handleSubmit = () => {
    ...call the backend
    ValidationForm.resetValidations();
    setState({
       email: '',
       fullName: '',
    })  
};

But I am getting this error:

Possible Unhandled Promise Rejection: TypeError: react_form_validator_core__WEBPACK_IMPORTED_MODULE_3__.ValidatorForm.resetValidations is not a function
    at Object.Contact.handleSubmit [as onSubmit] (Contact.jsx:93)
    at ValidatorForm.js:83
    at index.js:53
    at run (setImmediate.js:40)
    at runIfPresent (setImmediate.js:69)
    at onGlobalMessage (setImmediate.js:109)

I solved the issue by creating a reference for the form validator as follow: 🚨 This solution can help other devs facing this problem

In my component constructor

this.form = createRef();

My form

...
<ValidatorForm
    ref={this.form}
    autoComplete="off"
    noValidate
    onSubmit={this.handleSubmit}
 >

This will allow me to access the resetValidations() anywhere in my class as follow:

this.form.current.resetValidations();