如何验证React Hook形式选择字段

发布于 2025-02-08 16:42:35 字数 1214 浏览 2 评论 0原文

选择任何选项后,我可以在onChange < /code>函数上获取值,但是 /我尝试提交表单时出现错误消息。

我的代码:

<div className="form-group mb-3">
    <label>Country</label>
    <Controller
        control={control}
        name="country"
        render={({ 
            field: { onChange, onBlur, value, ref }
        }) => (
            <select
                className='form-control form-select'
                onChange={(e) => setCountry(e.target.value)}
                inputRef={register("country", { required: true })}
            >
                <option>Select Country</option>
                {
                    Country.getAllCountries().map((item) => <option value={item.isoCode} key={item.isoCode}>{item.name}</option>)
                }
            </select>
        )}
    />
    {errors.country && <span className="error-text">Country is required</span>}
</div>

错误消息:

”在此处输入图像描述”

I'm able to get the value on the onChange function after I select any option, but an error message appears when /I try to submit the form.

My code:

<div className="form-group mb-3">
    <label>Country</label>
    <Controller
        control={control}
        name="country"
        render={({ 
            field: { onChange, onBlur, value, ref }
        }) => (
            <select
                className='form-control form-select'
                onChange={(e) => setCountry(e.target.value)}
                inputRef={register("country", { required: true })}
            >
                <option>Select Country</option>
                {
                    Country.getAllCountries().map((item) => <option value={item.isoCode} key={item.isoCode}>{item.name}</option>)
                }
            </select>
        )}
    />
    {errors.country && <span className="error-text">Country is required</span>}
</div>

Error message:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多彩岁月 2025-02-15 16:42:35

当您使用React-Hook-Form的控制器使用控制器时,这是正确的方法:

<Controller
    control={control}
    name="country"
    render={({field, fieldState: {invalid}}) => (
        <>
            <label>Country</label>
            <select
               {...field} 
               className='formcontrol form select'
               onChange={({target:{value}) => {
                  changeCountry(value)
                  field.onChange(value)
               }}>
                <option>Select Country</option>
                {Country.getAllCountries().map((item) => <option
                    value={item.isoCode}
                    key={item.isoCode}>{item.name}</option>)}
            </select>
            {invalid && <span className="error-text">Country is required</span>}
        </>
    }/>

如果您没有任何特殊原因,请勿将field删除,并使用fieldStateController提供的中的值与呈现的字段相关。

This is the right way when your using Controller from react-hook-form:

<Controller
    control={control}
    name="country"
    render={({field, fieldState: {invalid}}) => (
        <>
            <label>Country</label>
            <select
               {...field} 
               className='formcontrol form select'
               onChange={({target:{value}) => {
                  changeCountry(value)
                  field.onChange(value)
               }}>
                <option>Select Country</option>
                {Country.getAllCountries().map((item) => <option
                    value={item.isoCode}
                    key={item.isoCode}>{item.name}</option>)}
            </select>
            {invalid && <span className="error-text">Country is required</span>}
        </>
    }/>

If you don't have any special reasons do not desagregate field and use the values in fieldState provided by Controller they are related to the field rendered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文