![](/img/trans.png)
[英]Formik, Yup, formik-material-ui sign up form showing email error when the form is clicked
[英]Select Material UI Tag Does not show error using Yup and formik
當沒有選擇任何內容時,它應該打印錯誤,但是,通過檢查元素我可以看到錯誤 label 但沒有出現。 另一個問題是label“國家”沒有出現。 這只發生在 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>
我設法找到了答案:試試這個:
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>
希望它有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.