NewOldMax / react-form-validator-core

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

Why does it that isFormValid delayed? #71

Closed classicalguss closed 4 years ago

classicalguss commented 4 years ago

I have the following code

    handleFullnameChange = (fullname) => {
        console.log(fullname);
        this.setState({ fullname });
        this.refs['form'].isFormValid(false).then((response) => {
            console.log("is Valid "+response)
        });
    }

And this too

                        <Form
                            ref="form"
                            onSubmit={this.handleSubmit}
                        >
                            <TextValidator
                                name="fullname"
                                placeholder="Full Name"
                                validators={['minStringLength:3']}
                                value={fullname}
                                onChangeText={this.handleFullnameChange} />

I'm expecting that when I type 3 characters that the log will return "is Valid true". Instead it returns "is Valid false" on the first 3 times I type a letter, and then returns "is Valid true" on the 4th time.

NewOldMax commented 4 years ago

@classicalguss Hi Validation works with promises. Also setState is asynchronous. So you cannot predict when it will be ready. As a workaround you can try this:

handleFullnameChange = (fullname) => {
        console.log(fullname);
        this.setState({ fullname }, () => {
             this.refs['form'].isFormValid(false).then((response) => {
                console.log("is Valid "+response)
             });
        });
    }
NewOldMax commented 4 years ago

closed due no activity