formik and form。检查不捕获价值

发布于 2025-01-25 20:57:45 字数 4673 浏览 5 评论 0原文

我正在利用Formik和React-Bootstrap来构建用户注册表格。就目前而言,我只是台式。元素。

我非常感谢任何帮助,因为我一直在挣扎几个小时!

const Register = () => {
const formik = useFormik({
    initialValues: {
        firstname: '',
        surname: '',
        email: '',
        password: '',
        role: 'user',
        secondaryinterest: [''],
    },
    validationSchema: Yup.object({
        firstname: Yup.string()
        .min(2, 'Your name is too short')
        .max(100, 'Your name is too long')
        .required('We require your name'),
        surname: Yup.string()
        .min(2, 'Your name is too short')
        .max(100, 'Your name is too long')
        .required('We require your name'),
        email:Yup.string()
        .required('Sorry the email is required')
        .email('This is not a valid email'),
        password:Yup.string()
        .required('Sorry the password is required'),
        secondaryinterest: Yup.array()
        .min(1, "You need to select at least one"),           
    }),
    onSubmit:(values) => {
        console.log(values)
    }
})

return(
    <Form onSubmit={formik.handleSubmit}>
        <Form.Group>
        <Form.Label>Select the five most relevant for you</Form.Label>
            {['Football', 'Golf', 'Rugby', 'Tennis'].map((secondaryinterest) => (
                <div key={`${secondaryinterest}`} className="mb-3">
                    <Form.Check
                        type='checkbox'
                        name='secondaryinterest'
                        placeholder="secondaryinterest"
                        id={`${secondaryinterest}`}
                        label={`${secondaryinterest}`}
                        onChange={formik.handleChange}
                        {...formik.getFieldProps('secondaryinterest')}
                    />
                </div>
            ))}
        </Form.Group>
        <Row className="mb-3">
            <Form.Group as={Col} controlId="formGridFirstname">
                <Form.Label>First Name</Form.Label>
                <Form.Control 
                    type="text"
                    name="firstname"
                    variant="outlined"
                    {...formik.getFieldProps('firstname')}
                />
                {formik.touched.firstname && formik.errors.firstname ? (
                    <div>{formik.errors.firstname}</div>
                ) : null}

            </Form.Group>
            <Form.Group as={Col} controlId="formGridSurname">
                <Form.Label>Last Name</Form.Label>
                <Form.Control 
                    type="text"
                    name="surname"
                    variant="outlined"
                    {...formik.getFieldProps('surname')}
                />
                {formik.touched.surname && formik.errors.surname ? (
                    <div>{formik.errors.surname}</div>
                ) : null}
            </Form.Group>
        </Row>
        <Row className="mb-3">
            <Form.Group as={Col} controlId="formGridEmail">
                <Form.Label>E-mail</Form.Label>
                <Form.Control
                    type="email"
                    name="email"
                    variant="outlined"
                    {...formik.getFieldProps('email')}
                />
                {formik.touched.email && formik.errors.email ? (
                    <div>{formik.errors.email}</div>
                ) : null}
            </Form.Group>
            <Form.Group as={Col} controlId="formGridPassword">
                <Form.Label>Enter your password</Form.Label>
                <Form.Control
                    type="password"
                    name="password"
                    variant="outlined"
                    {...formik.getFieldProps('password')}
                />
                {formik.touched.password && formik.errors.password ? (
                    <div>{formik.errors.password}</div>
                ) : null}
            </Form.Group>
        </Row>
        <Button variant="primary" type="submit">
            Register
        </Button>
        
    </Form>
}

控制台日志输出是:

{firstname: "Dave", surname: "Kula", email: "[email protected]", password: "ddhshja", role: "user", secondary interest: [],...}

I'm utilising Formik and React-Bootstrap to construct a user registration form. For now, I'm just console.logging the values captured from the form but for some reason my form does not capture the values from <Form.Check> element.

I would really appreciate any help as I've been struggling for a few hours!

const Register = () => {
const formik = useFormik({
    initialValues: {
        firstname: '',
        surname: '',
        email: '',
        password: '',
        role: 'user',
        secondaryinterest: [''],
    },
    validationSchema: Yup.object({
        firstname: Yup.string()
        .min(2, 'Your name is too short')
        .max(100, 'Your name is too long')
        .required('We require your name'),
        surname: Yup.string()
        .min(2, 'Your name is too short')
        .max(100, 'Your name is too long')
        .required('We require your name'),
        email:Yup.string()
        .required('Sorry the email is required')
        .email('This is not a valid email'),
        password:Yup.string()
        .required('Sorry the password is required'),
        secondaryinterest: Yup.array()
        .min(1, "You need to select at least one"),           
    }),
    onSubmit:(values) => {
        console.log(values)
    }
})

return(
    <Form onSubmit={formik.handleSubmit}>
        <Form.Group>
        <Form.Label>Select the five most relevant for you</Form.Label>
            {['Football', 'Golf', 'Rugby', 'Tennis'].map((secondaryinterest) => (
                <div key={`${secondaryinterest}`} className="mb-3">
                    <Form.Check
                        type='checkbox'
                        name='secondaryinterest'
                        placeholder="secondaryinterest"
                        id={`${secondaryinterest}`}
                        label={`${secondaryinterest}`}
                        onChange={formik.handleChange}
                        {...formik.getFieldProps('secondaryinterest')}
                    />
                </div>
            ))}
        </Form.Group>
        <Row className="mb-3">
            <Form.Group as={Col} controlId="formGridFirstname">
                <Form.Label>First Name</Form.Label>
                <Form.Control 
                    type="text"
                    name="firstname"
                    variant="outlined"
                    {...formik.getFieldProps('firstname')}
                />
                {formik.touched.firstname && formik.errors.firstname ? (
                    <div>{formik.errors.firstname}</div>
                ) : null}

            </Form.Group>
            <Form.Group as={Col} controlId="formGridSurname">
                <Form.Label>Last Name</Form.Label>
                <Form.Control 
                    type="text"
                    name="surname"
                    variant="outlined"
                    {...formik.getFieldProps('surname')}
                />
                {formik.touched.surname && formik.errors.surname ? (
                    <div>{formik.errors.surname}</div>
                ) : null}
            </Form.Group>
        </Row>
        <Row className="mb-3">
            <Form.Group as={Col} controlId="formGridEmail">
                <Form.Label>E-mail</Form.Label>
                <Form.Control
                    type="email"
                    name="email"
                    variant="outlined"
                    {...formik.getFieldProps('email')}
                />
                {formik.touched.email && formik.errors.email ? (
                    <div>{formik.errors.email}</div>
                ) : null}
            </Form.Group>
            <Form.Group as={Col} controlId="formGridPassword">
                <Form.Label>Enter your password</Form.Label>
                <Form.Control
                    type="password"
                    name="password"
                    variant="outlined"
                    {...formik.getFieldProps('password')}
                />
                {formik.touched.password && formik.errors.password ? (
                    <div>{formik.errors.password}</div>
                ) : null}
            </Form.Group>
        </Row>
        <Button variant="primary" type="submit">
            Register
        </Button>
        
    </Form>
}

Console log output is:

{firstname: "Dave", surname: "Kula", email: "[email protected]", password: "ddhshja", role: "user", secondary interest: [],...}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找回味觉 2025-02-01 20:57:45

实际上,这部分没有任何作用。您想做的实际要做的事情,当您设置检查标记时,它应将其添加到Secondaryest的数组中。但是,您没有提供指示,而Formik不能只是猜测。

 {['Football', 'Golf', 'Rugby', 'Tennis'].map((secondaryinterest) => (
                <div key={`${secondaryinterest}`} className="mb-3">
                    <Form.Check
                        type='checkbox'
                        name='secondaryinterest'
                        placeholder="secondaryinterest"
                        id={`${secondaryinterest}`}
                        label={`${secondaryinterest}`}
                        onChange={formik.handleChange}
                        {...formik.getFieldProps('secondaryinterest')}
                    />
                </div>
            ))}

我建议您更轻松地为您创建每个复选框的新初始值。使它们分开。

This part actually doesn't do anything. What you want to do actually, when you set the checkmark, it should add it to the array of secondaryinterest. However you didn't give instructions and Formik cannot just guess it.

 {['Football', 'Golf', 'Rugby', 'Tennis'].map((secondaryinterest) => (
                <div key={`${secondaryinterest}`} className="mb-3">
                    <Form.Check
                        type='checkbox'
                        name='secondaryinterest'
                        placeholder="secondaryinterest"
                        id={`${secondaryinterest}`}
                        label={`${secondaryinterest}`}
                        onChange={formik.handleChange}
                        {...formik.getFieldProps('secondaryinterest')}
                    />
                </div>
            ))}

Easier change for you, I suggest, create new initial values for each checkbox. Make them separate.

久光 2025-02-01 20:57:45

我能够修复它,并想到答案,以防它帮助其他人AA后期

{['Football', 'Golf', 'Rugby', 'Tennis'].map((secondaryinterest) => (
            <div key={`${secondaryinterest}`} className="mb-3">
                <Form.Check
                    type='checkbox'
                    name='secondaryinterest'
                    value={`${secondaryinterest}`}
                    label={`${secondaryinterest}`}
                    onChange={formik.handleChange}
                />
            </div>
        ))}

我规定了名称和价值字段。 {... formik.getFieldProps('SecontaryEnterest')}也是多余的。

I was able to fix it and thought to post the answer in case it helps someone else a a later stage

{['Football', 'Golf', 'Rugby', 'Tennis'].map((secondaryinterest) => (
            <div key={`${secondaryinterest}`} className="mb-3">
                <Form.Check
                    type='checkbox'
                    name='secondaryinterest'
                    value={`${secondaryinterest}`}
                    label={`${secondaryinterest}`}
                    onChange={formik.handleChange}
                />
            </div>
        ))}

I stipulated the name and value fields. Also the {...formik.getFieldProps('secondaryinterest')} was redundant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文