Skip to content

Conversation

@cheton
Copy link

@cheton cheton commented Oct 23, 2017

  • Added a hasErrors() method to return a boolean value
  • validate() and validateAll() will return a boolean value after validation.

This PR enables support for the following use case.

<button
    type="button"
    onClick={event => {
        const ok = this.form.validateAll();
        if (!ok) {
            return;
        }

        const { name, password } = this.form.getValues();
        // Save changes
    }}
>
    Save
</button>

@Lesha-spr
Copy link
Owner

@cheton , did you tested it? I believe on that loop errors setup isn't made yet since _setErrors called asynchronously and hasErrors will get prevState.

@cheton
Copy link
Author

cheton commented Oct 23, 2017

Sorry for that I did make a mistake, the _setErrors callback is asynchronous and it will be called when the component state has actually updated.

How about using an error-first callback for the validate function?

this.form.validateAll((err) => {
    if (err) {
        return;
    }

    const { name, password } = this.form.getValues();
    // Save changes
});

@cheton
Copy link
Author

cheton commented Oct 23, 2017

I made a little change to the code using the error-first callback, this should fix the issue existed in this PR.

@jayu
Copy link

jayu commented Jul 24, 2018

I'd love this feature to validate the whole form before submitting. I have to do I manually with refs to all inputs inside a form and it's awful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants