dockwa / simple-react-validator

A simple react form validator inspired by Laravel validation.
https://dockwa.github.io/simple-react-validator
MIT License
278 stars 78 forks source link

purgeFields() for react functional component #390

Closed haresh6x closed 2 years ago

haresh6x commented 2 years ago

According to the docs, we can use purgeFields() method in class based components like this

render() {
  this.validator.purgeFields();
  return (
    <div>
      <div className="form-group">
        <label>Address Line 1</label>
        <input className="form-control" value={this.state.title} onChange={this.setTitle} />
        {this.validator.message('title', this.state.title, 'required|alpha')}
      </div>
      {this.optinallyAddAnotherAddressLine()}
      <button className="btn btn-primary" onClick={this.submitForm}>Save Review</button>
    </div>
  );
}

The doubt is that how do I use it in functional based components?

stuyam commented 2 years ago

It would just run before the code is returned and should be the same for functional components.