如何在Formik中禁用提交时的自动重置表单?

人气:277 发布:2023-01-03 标签: reactjs formik

问题描述

我有一个由formik控制的表单,当我填写所有字段并按下提交按钮时,函数onSubmit被调用,并且我的表单将重置此值。

有时我的数据不正确(如重复的电子邮件),我需要保存此数据。

如何执行此操作?

这是我的代码:

    const schema = Yup.object().shape({
        login: Yup.string()
            .email('Email não possui formato válido')
            .required('Informe o email!'),
        password: Yup.string().required('Informe a senha!'),
    })

    const formik = useFormik({
        initialValues: {
          login: '', password: '', inactive: false
        },

        validationSchema: schema,
        onSubmit: values => {
            registerUser(values)
        }
    })

return (
    <form onSubmit={formik.handleSubmit} className={classes.form} noValidate>
        <Grid container spacing={3}>

            <Grid item xs={12}>
                <Typography className={classes.observation} component="h6">* Necessário preenchimento do cadastro geral para liberar permissão de telas</Typography>
            </Grid>

            <Grid item xs={5}>
                <TextField
                    value={formik.values.login}
                    onChange={formik.handleChange}
                    onBlur={formik.handleBlur}
                    helperText={formik.touched.login ? formik.errors.login : ""}
                    error={formik.touched.login && Boolean(formik.errors.login)}
                    variant="outlined"
                    margin="normal"
                    required
                    fullWidth
                    id="email"
                    label="E-mail"
                    name="login"
                    autoComplete="email"
                />
            </Grid>

            <Grid item xs={5}>
                <TextField
                    value={formik.values.password}
                    onChange={formik.handleChange}
                    onBlur={formik.handleBlur}
                    helperText={formik.touched.password ? formik.errors.password : ""}
                    error={formik.touched.password && Boolean(formik.errors.password)}
                    variant="outlined"
                    margin="normal"
                    required
                    fullWidth
                    type="password"
                    id="password"
                    label="Senha"
                    name="password"
                />
            </Grid>

            <Grid item xs={2}>
                <FormControlLabel
                    onChange={formik.handleChange}
                    onBlur={formik.handleBlur}
                    value={formik.values.inactive}
                    control={<Switch color="primary" />}
                    label="Inativo"
                    labelPlacement="top"
                />
            </Grid>

            <Grid item xs={3}>
                <Button fullWidth 
                    variant="contained" 
                    color="primary"
                    type="submit"
                >
                    Cadastrar
                </Button>
            </Grid>

        </Grid>
    </form>
);

推荐答案

您需要更新以下行:

    <form onSubmit={(e) => { e.preventDefault(); formik.handleSubmit(e)}} className={classes.form} noValidate>

29