You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用@mui TextField设为required时的原生验证?

禁用MUI TextField原生required验证提示(搭配yup + react-hook-form时)

问题背景

使用@mui/material的TextField并标记required属性时,浏览器会触发原生表单验证警告提示,但项目已通过yup和react-hook-form实现自定义表单验证,无需原生提示。

解决方案

方案1:给表单元素添加noValidate属性(修正你的尝试1)

你之前的代码报错是因为noValidate在JSX中需要显式赋值为布尔值,直接写变量名会被当作未定义变量。正确写法如下:

<Dialog
  open={true}
  {...props}
  PaperProps={{
    ...PaperProps,
    component: 'form',
    noValidate: true, // 显式设置为布尔值true
    onSubmit,
  }}
>

noValidate是HTML表单的原生属性,添加后会禁用浏览器的原生表单验证逻辑,完全交由react-hook-form和yup处理验证。

方案2:移除TextField的required属性

既然已经通过自定义验证实现必填校验,可直接移除TextField上的required属性,从根源避免触发原生验证:

<TextField
  {...methods.register('title')}
  className="col-span-full"
  // 移除required属性
  error={!!methods.formState?.errors?.title?.message}
  helperText={methods.formState?.errors?.title?.message?.toString()}
/>

这种方式更贴合自定义表单验证的逻辑,完全摆脱原生验证的干扰。

内容的提问来源于stack exchange,提问作者Ali Majed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 06:11:01