Skip to content
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

Using the error prop causes console errors on validations #471

Closed
idmadj opened this issue Sep 26, 2018 · 2 comments
Closed

Using the error prop causes console errors on validations #471

idmadj opened this issue Sep 26, 2018 · 2 comments
Assignees
Labels
Type: Bug Bug reports and their fixes

Comments

@idmadj
Copy link
Contributor

idmadj commented Sep 26, 2018

When using the error prop with a ValidatedForm, ValidatedQuickForm and AutoForm, an uncaught promise error is logged to the console whenever the model is validated after a submit (such as when manipulating model data).

Demo: https://codesandbox.io/s/lx4rz1001q

The problem seems to lie in ValidatedForm.onValidateModel. Whenever the error prop is set or schema validation throws, it inevitably leads the returned promise to reject , and both calls to this.onValidate() and this.onValidateModel() in componentWillReceiveProps are unhandled, causing the console error.

componentWillReceiveProps ({model, schema, validate, validator}) {
    super.componentWillReceiveProps(...arguments);

    if (this.props.schema !== schema || this.props.validator !== validator) {
        this.setState(({bridge}) => ({
            validator: bridge.getValidator(validator)
        }), () => {
            if (validate === 'onChange' || validate === 'onChangeAfterSubmit' && this.state.validate) {
                this.onValidate();
            }
        });
    } else if (!isEqual(this.props.model, model)) {
        if (validate === 'onChange' || validate === 'onChangeAfterSubmit' && this.state.validate) {
            this.onValidateModel(model);
        }
    }
}

onValidateModel (model) {
    model = this.getModel('validate', model);

    let catched = this.props.error || null;
    try {
        this.state.validator(model);
    } catch (error) {
        catched = error;
    }

    return new Promise((resolve, reject) => {
        this.props.onValidate(model, catched, (error = catched) => {
            // Do not copy error from props to state.
            this.setState(() => ({error: error === this.props.error ? null : error}), () => {
                if (error) {
                    reject(error);
                } else {
                    resolve();
                }
            });
        });
    });
}
@radekmie radekmie self-assigned this Sep 26, 2018
@radekmie radekmie added the Type: Bug Bug reports and their fixes label Sep 26, 2018
@radekmie
Copy link
Contributor

Hi @idmadj - good catch! Well, indeed, componentWillReceiveProps is not handling it at all. Maybe it's a good idea to silent it, just like in #onSubmit. Maybe you'd like to submit a PR for it?

idmadj added a commit to idmadj/uniforms that referenced this issue Sep 26, 2018
@idmadj
Copy link
Contributor Author

idmadj commented Sep 26, 2018

Hey @radekmie, sure thing! Silencing seems like the way to go. Just submitted PR #472.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Bug reports and their fixes
Projects
Archived in project
Development

No branches or pull requests

2 participants