繁体   English   中英

带有 react-select formState 的 React-hook-form 无效但 controller 没有给出错误

[英]React-hook-form with react-select formState not valid but controller don't giving an error

我使用 react-hook-form 进行验证。 如果 formState.isValid 为 false,则禁用提交按钮。 如果我不 select 选项 formState.isValid 返回 false,它就可以工作。 但是 controller 没有给出错误。 我怎样才能让它给出错误消息? 这是 controller 代码

const { register, errors, formState, control } = useForm({
        mode: "all"
    });

function changeValue(inputValue) {
        onChange({ key: "expiryMonth", value: inputValue.value });
    }
...

{errors.expiryMonth && <span className="input__error-message">required.</span>}
                <Controller
                    name="expiryMonth"
                    defaultValue={null}
                    control={control}
                    value={expiryMonth}
                    render={({ onChange }) => <Select isSearchable={false} placeholder="Month" onChange={(inputValue) =>
                        onChange({ value: changeValue(inputValue) })} options={months} />}
                    rules={{ required: true }}
                    register={register}
                />

当您使用Controller时,它会为您register

<Controller
  render={(props) => 
    <ReactSelect {...props} 
      options={[
        { value: "chocolate", label: "Chocolate" },
        { value: "strawberry", label: "Strawberry" },
        { value: "vanilla", label: "Vanilla" }
    ]} 
  />}
  name="ReactSelect"
  control={control}
/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM