简体   繁体   English

Select Material UI Tag 使用 Yup 和 formik 不显示错误

[英]Select Material UI Tag Does not show error using Yup and formik

When nothing is selected, it should print the error, however, by inspecting elements I could see the error label but does not appear.当没有选择任何内容时,它应该打印错误,但是,通过检查元素我可以看到错误 label 但没有出现。 Another problem is that the label "Country" does not appear.另一个问题是label“国家”没有出现。 This happens only in the select tags in MUI and in TextField below which is the "Zip" tag it is okay.这只发生在 MUI 中的 select 标记和 TextField 下面的“Zip”标记中是可以的。

    <Select
              labelId="demo-simple-select-label"
              required
              fullWidth
              label="Country" 
              name="country"
              id="demo-simple-select"
              value={formik.values.country}
              onChange={formik.handleChange}
              error={
                formik.touched.country && Boolean(formik.errors.country)
              }
              helperText={formik.touched.country && formik.errors.country}
            >
              <MenuItem value={"DE"}>Germany</MenuItem>
              <MenuItem value={"FR"}>France</MenuItem>
            </Select>

在此处输入图像描述

I managed to find an answer: Try this out:我设法找到了答案:试试这个:

import { FormHelperText } from "@mui/material";

  <FormControl>
      <Select
        labelId="demo-simple-select-label"
        required
        fullWidth
        label="Country" 
        name="country"
        id="demo-simple-select"
        value={formik.values.country}
        onChange={formik.handleChange}
        error={
           Boolean(formik.touched.country && formik.errors.country)
        }
      >
         <MenuItem value={"DE"}>Germany</MenuItem>
         <MenuItem value={"FR"}>France</MenuItem>
      </Select>
      {formik.touched.country && formik.errors.country ? (
         <FormHelperText
            sx={{ color: "#bf3333", marginLeft: "16px !important" }}
         >
            {formik.touched.country && formik.errors.country}
         </FormHelperText>
         ) : null}
  </FormControl>

Hope it helped希望它有所帮助

暂无
暂无

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

相关问题 Formik,是的,formik-material-ui 注册表单在单击表单时显示 email 错误 - Formik, Yup, formik-material-ui sign up form showing email error when the form is clicked 如何使用带有yupvalidationSchema的Formik显示Form错误摘要 - How to show Form error summary using Formik with yup validationSchema 是的,使用 Formik、Material-UI 的表单数组和嵌套表单组的模式生成器 - React - Yup schema generator for form array and nested form groups using Formik, Material-UI - React 如何使用 material-ui、formik、yup 和 react-input-mask 正确验证屏蔽输入? - How to correctly validate an masked-input using material-ui, formik, yup, and react-input-mask? 使用 Formik 和 Yup 和 React-select 进行验证 - Validation using Formik with Yup and React-select 使用 Formik 的 React 不会清除 Material UI 表单中的数据值 - React using Formik does not clear the data value in Material UI form Formik + Yup 表单字符串验证不适用于 Material UI TextField +useFormik 或 Formik 组件 - Formik + Yup form string validation not working with either Material UI TextField +useFormik or Formik component 在 formik 上显示 API 错误,如 yup 消息错误 - Show API error on formik like yup message error 在 Formik 表单中显示 Yup 验证错误后动态更改 (i18n) UI 语言,使用挂钩 -> 意外行为 - Dynamically changing (i18n) UI language after Yup validation errors show in Formik form, using hooks -> unexpected behaviour Formik 表单无法识别 Material ui 按钮 - Formik form does not recognize Material ui Button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM