Closed coolbeatz71 closed 4 years ago
I am having a form for signing, having as state
this.state = { email: '', fullName: '', }
I am using the
ValidatorForm
alongside theValidatorComponent
. 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 validationAnd 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
this.form = createRef();
...
<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();
I am having a form for signing, having as state
I am using the
ValidatorForm
alongside theValidatorComponent
. 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:
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 validationAnd now my
handleSubmit()
methods looks like this:But I am getting this error: