yup验证如何进行反应选择

All*_*yam 3 reactjs react-select yup

我发现了对react-select(多重选择)的yup验证。请在yup中参考链接选择多个react-select验证项。但是我不需要使用yup的多重选择验证。我需要对react-select的基本验证我尝试过,但是我无法使用yup实现反应选择验证。

我如何使用yup实现它。请帮我解决这个问题。

提前致谢。最好的答案将不胜感激。

lec*_*tor 5

https://codesandbox.io/s/03zxq01okp

  • 更改topicstopic
  • 将主题验证类型从数组更改为字符串
  • mapPropsToValues topic类型从数组更改为字符串
  • 使用valuetopic有效载荷

差异:

15,22c15,17
<     topics: Yup.array()
<       .max(1, "Pick at least 3 tags")
<       .of(
<         Yup.object().shape({
<           label: Yup.string().required(),
<           value: Yup.string().required()
<         })
<       )
---
>     topic: Yup.string()
>       .ensure()
>       .required("Topic is required!")
26c21
<     topics: []
---
>     topic: ""
31c26
<       topics: values.topics.map(t => t.value)
---
>       topic: values.topic.value
72c67
<         value={values.topics}
---
>         value={values.topic}
75,76c70,71
<         error={errors.topics}
<         touched={touched.topics}
---
>         error={errors.topic}
>         touched={touched.topic}
107c102
<     this.props.onChange("topics", value);
---
>     this.props.onChange("topic", value);
112c107
<     this.props.onBlur("topics", true);
---
>     this.props.onBlur("topic", true);
118c113
<         <label htmlFor="color">Topics (select at least 3) </label>
---
>         <label htmlFor="color">Topic</label>
156c151
<       <code>topics</code> that uses Jed Watson's{" "}
---
>       <code>topic</code> that uses Jed Watson's{" "}
Run Code Online (Sandbox Code Playgroud)