Skip to content

Async Validator #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed

Async Validator #103

wants to merge 6 commits into from

Conversation

stuyam
Copy link
Collaborator

@stuyam stuyam commented Jun 14, 2019

Ability to define async validators that are called with callbacks and and get called after regular validation happens.

References: #98 from @ej2015.
Got this branch working.

Usage

  1. Define rule
this.validator = new SimpleReactValidator({
  validators: {
    unique: {
      message: 'Not a unique email.',
      asyncRule: function(val, params, validator, completion) {
        // call validator.pass(completion) to pass onto next async validator or pass validation
        // call validator.fail(completion) if something goes wrong
        setTimeout(() => validator.fail(completion), 1000);
      }
    }
  }
});
  1. Add to message
{this.validator.message('email', this.state.email, 'email|unique')}
  1. New pass fail completion blocks
submitForm() {
  this.setState({processing: true});
  this.validator.asyncValid({
    pass: () => {
      alert('You submitted the form and stuff!');
      this.setState({processing: false});
    },
    fail: () => {
      this.validator.showMessages();
      this.setState({processing: false});
    }
  });
}

@stuyam stuyam mentioned this pull request Jun 14, 2019
@stuyam
Copy link
Collaborator Author

stuyam commented Jun 14, 2019

@ej2015 I am not as familiar with promises so not sure if I would be able to use one in this case to clean it up?

@stuyam stuyam self-assigned this Jul 8, 2019
@stuyam stuyam closed this Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant