[英]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.