yup验证如何进行反应选择

时间:2019-03-01 05:20:39

标签: reactjs react-select yup

我找到了对react-select(多次选择)的yup验证。请参考链接select multiple items of react-select validation in yup。但是我不需要使用yup进行多次选择验证。我需要对使用yup的react-select进行基本验证我尝试过,但是我无法使用yup来实现反应选择验证。

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

先谢谢了。最好的答案将不胜感激。

1 个答案:

答案 0 :(得分:3)

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

  • topics更改为topic
  • 将主题验证类型从数组更改为字符串
  • mapPropsToValues topic的类型从数组更改为字符串
  • value中的topic用于有效载荷

差异:

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{" "}