简体   繁体   English

Formik 使用 React 和 Material UI 处理复选框验证

[英]Formik handle checkbox validation with React and Material UI

I'm trying to handle the Checkbox validation for an "Accept Terms of Service" checkbox with React and Material UI.我正在尝试使用 React 和 Material UI 处理“接受服务条款”复选框的复选框验证。

So far I did this:到目前为止,我这样做了:

Validation Schema验证模式

const validationSchema = yup.object().shape({
  email: yup
    .string()
    .email('Enter a valid email address')
    .required('Email address is required'),
  password: yup
    .string()
    .min(8, 'Password should be of minimum 8 characters')
    .required(),
  termsOfService: yup
    .boolean()
    .required('You must accept the Terms of Service to proceed')
    .oneOf([true], 'Error')
})

Form Component表单组件

const formik = useFormik({
    initialValues: {
      email: '',
      password: '',
      termsOfService: false
    },
    validationSchema,
    onSubmit: async values => {
      alert(JSON.stringify(values, null, 2))
    }
  })

Form Component JSX表单组件 JSX

<Box
      component="form"
      noValidate
      onSubmit={formik.handleSubmit}
      sx={{ mt: 1 }}
    >
      <TextField
        autoFocus
        margin="normal"
        required
        fullWidth
        id="email"
        label={t('email_address')}
        name="email"
        autoComplete="email"
        value={formik.values.email}
        onChange={formik.handleChange}
        error={formik.touched.email && Boolean(formik.errors.email)}
        helperText={formik.touched.email && formik.errors.email}
      />
      <TextField
        margin="normal"
        required
        fullWidth
        name="password"
        label={t('password')}
        type="password"
        id="password"
        autoComplete="current-password"
        value={formik.values.password}
        onChange={formik.handleChange}
        error={formik.touched.password && Boolean(formik.errors.password)}
        helperText={formik.touched.password && formik.errors.password}
      />
      <FormControlLabel
        control={<Checkbox value="terms-of-service" color="primary" />}
        label={t('terms_of_service')}
      />
      <Button type="submit" fullWidth variant="contained" sx={{ mt: 3, mb: 2 }}>
        {t('sign_up_button')}
      </Button>
    </Box>

How to do that, in the same way, I managed the validation on the text inputs?如何做到这一点,以同样的方式,我管理文本输入的验证?

Check this example检查这个例子

You can use formik.setFieldValue inside onChange event of checkbox to set checkbox value.您可以在复选框的onChange事件中使用formik.setFieldValue来设置复选框值。 Also, use mui FormHelperText to show validation error.此外,使用 mui FormHelperText显示验证错误。

<FormControl>
    <FormControlLabel
      control={
         <Checkbox
           onChange={(e) => { setFieldValue("termsOfService", e.target.checked) }}
           name="termsOfService"
           checked={values.termsOfService}
          />
      }
      label="terms_of_service"
    >
    <FormHelperText style={{ color: "red" }}>
       {touched.termsOfService && errors.termsOfService
        ? touched.termsOfService && errors.termsOfService
        : " "}
    </FormHelperText>
</FormControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM