NewOldMax / react-form-validator-core

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

disable submit button with onError #53

Closed truescotian closed 5 years ago

truescotian commented 5 years ago

After going through the issue: https://github.com/NewOldMax/react-form-validator-core/issues/41

And trying to implement the solution in codepen, here: https://codesandbox.io/s/pedantic-davinci-sxudm

After switching the handleError to handle the promise from isFormValid instead of the old boolean, I'm having troubles toggling disabled in state to disable/enable the submit button.

If a form is invalid, this is fine as handleError sets disabled in state to true. However, when the form is valid I'd assume handleError doesn't get called, so other than creating our own isValid function which returns true or false and constantly calling it perhaps using validationListener (maybe?), is there a solid way to determine a valid form so that the button is enabled?

I'm also going to have additional login in handleSubmit, but figuring out a solution to the above problem would be useful to start! Any info is appreciated.

NewOldMax commented 5 years ago

Hi, since isFormValid is a promise, you should use it as promise, yes:

handleError = () => {
    this.form.isFormValid().then(isValid => {
      this.setState({ disabled: !isValid });
    });
};

using validationListener

yes, this is current way to do this

truescotian commented 5 years ago

Cheers, was able to add custom validators, listener, and that handleError to get it to work nicely. Thanks!