Coder Social home page Coder Social logo

Comments (1)

coolbeatz71 avatar coolbeatz71 commented on September 26, 2024

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();

from react-form-validator-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.