Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

reactjs - Submission error is not shown on react final form

I'm having troubles with React-Final-Form library. From the onSubmit function, I return a validation error, but it is not displayed on the form itself. Here you can see my code:

My form:

<Form
                                       onSubmit={this.onSubmit}
                                        initialValues={{}}
                                        validate={
                                            values => {
                                                const errors = {}
                                                if (!values.username) {
                                                    errors.username = "This field is required"
                                                }
                                                if (!values.password) {
                                                    errors.password = "This field is required"
                                                }
                                                return errors
                                            }
                                        }
                                        render={({submitError, handleSubmit, submitting, values}) => (
                                            <form onSubmit={handleSubmit}>
                                                <div className="form-group">
                                                    <label>First Name</label>
                                                    <Field name="username">
                                                        {({input, meta}) => (
                                                            <>
                                                                <input {...input} type="text" placeholder="Username"
                                                                       className={"form-control" + ((meta.error || meta.submitError) && meta.touched ? " is-invalid" : "")}/>
                                                                {(meta.error || meta.submitError) && meta.touched ?
                                                                    <div
                                                                        className="invalid-feedback">{meta.error || meta.submitError}</div> : null
                                                                }
                                                            </>
                                                        )}
                                                    </Field>
                                                    {submitError && (
                                                        <div>{submitError}</div>
                                                    )}
                                                </div>
                                                <div className="form-group">
                                                    <label>Password</label>
                                                    <Field name="password">
                                                        {({input, meta}) => (
                                                            <>
                                                                <input {...input} type="password"
                                                                       placeholder="Password"
                                                                       className={"form-control" + ((meta.error || meta.submitError) && meta.touched ? " is-invalid" : "")}/>
                                                                {(meta.error || meta.submitError) && meta.touched ?
                                                                    <div
                                                                        className="invalid-feedback">{meta.error || meta.submitError}</div> : null
                                                                }
                                                            </>
                                                        )}
                                                    </Field>
                                                </div>
                                                <button type="submit" disabled={submitting}
                                                        className="btn btn-block btn-outline-secondary">Log In
                                                </button>
                                                <pre>{JSON.stringify(values, 0, 2)}</pre>
                                            </form>
                                        )}
                                    />

My onSubmit function:

onSubmit = async values => {
        const errors = {}
        let loginData = {
            username: values.username,
            password: values.password
        }
        usersAPI.getCSRF().then((response) => {
            usersAPI.login(loginData)
                .then(response => {
                    if (response.data.login_status === "Incorrect data") {
                        console.log("Invalid data")
                        errors.username = "Invalid login or password"
                        errors.password = "Invalid login or password"
                        return errors
                    } else {
                        localStorage.setItem('token', response.data.token)
                        this.props.setAuthState(true)
                        console.log("Logged in successfully")
                    }
                })
                .catch(err => console.log(err))
        })
    }

The problem is in axios request. If my back-end server sends the response that credentials are invalid, then console.log("Invalid data") will appear in console but nothing will happen with form. If I comment my axios request and just leave this code:

onSubmit = async values => {
        return {username: "Invalid data"}
    }

So, in this case everything works correct. Who can tell me How can I optimize my axios request so after that my form will get submission errors that returned from

errors.username = "Invalid login or password"
errors.password = "Invalid login or password"
return errors
question from:https://stackoverflow.com/questions/65869015/submission-error-is-not-shown-on-react-final-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found mistake. Mistake was in my return function. It just returns this value to usersAPI.getCSRF() function, not to global onSubmit function. I made some changes so my final code looks like this:

onSubmit = values => {
        let loginData = {
            username: values.username,
            password: values.password
        }
        let success = false;
        usersAPI.getCSRF().then((response) => {
            usersAPI.login(loginData)
                .then(response => {
                    if (response.data.login_status !== "Incorrect data") {
                        success = true
                        localStorage.setItem('token', response.data.token)
                    }
                })
                .catch(err => console.log(err))
        })
        if (success) this.props.setAuthState(true)
        else return {username: "Incorrect username or password", password: "Incorrect username or password"}
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...