,可以通过以下步骤来实现:
import React from "react";
import { useForm, Controller } from "react-hook-form";
import { TextField } from "@material-ui/core";
const MyForm = () => {
const { control, handleSubmit, watch } = useForm();
const onSubmit = (data) => {
console.log(data);
};
const password = watch("password", ""); // 监听password字段的值,默认为空字符串
return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
name="password"
control={control}
rules={{ required: true }}
render={({ field }) => (
<TextField {...field} label="Password" type="password" />
)}
/>
<Controller
name="confirmPassword"
control={control}
rules={{ required: true, validate: (value) => value === password }}
render={({ field }) => (
<TextField {...field} label="Confirm Password" type="password" />
)}
/>
<button type="submit">Submit</button>
</form>
);
};
validate: (value) => value === password
,用于判断"confirmPassword"字段的值是否与"password"字段的值相同。请注意,上述代码中的腾讯云产品和产品介绍链接地址是虚构的,仅供参考。实际使用时,请根据实际情况替换为相应的产品和链接地址。
领取专属 10元无门槛券
手把手带您无忧上云