reactjs - 使用材质 ui 时,表单元素上的多个验证不起作用
问题描述
我正在为我的网站 ui 使用材料 ui,并且还使用 react-hook-form。我的代码如下:
<TextField
name="firstName"
id="firstName"
label="First Name"
ref={register({ required: true, minLength: 6 })}
/>
<div>
{errors.firstName?.type === "required" && (
<div className={classes.errorMsg}>First Name is required.</div>
)}
{errors.firstName?.type === "minLength" && (
<div className={classes.errorMsg}>
First Name length should be more than 6 characters.
</div>
)}
</div>
我正在尝试对 firstName 元素应用多个验证,但只有“必需”有效。我输入任何小于 6 个字符的值,然后 react-hook-form 应该显示“minLength”错误,该错误不起作用。
请建议我这是 react-hook-form 的问题,或者我做错了申请。非常感谢。
解决方案
您可以使用 inputRef 代替 ref
<TextField
name="firstName"
id="firstName"
label="First Name"
inputRef={register({ required: true, minLength: 6 })}
/>
推荐阅读
- python - cPickle.PicklingError:无法序列化对象:NotImplementedError
- swift - 使用 DEFLATE (RFC 1951) 函数压缩和解压缩 zlib (RFC 1950)
- c - 接收多播数据包的用户缓冲区大小?
- javascript - 如何处理 XMLHttpRequest 连接错误
- python - 如何跳过导致UnicodeDecodeError的字符:使用像errors="replace"这样的文本?
- irc - 微信在每个发送消息中添加钩子命令
- google-cloud-platform - 特定区域的子域
- mysql - 从表 1 中获取记录,与表 2 中的多条记录进行比较
- r - 如何根据 2 个变量用 ggplot2 为绘图着色?
- c# - 尝试使用单选按钮启用组合框时出了什么问题?