簡體   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