我找到了对react-select(多次选择)的yup验证。请参考链接select multiple items of react-select validation in yup。但是我不需要使用yup进行多次选择验证。我需要对使用yup的react-select进行基本验证我尝试过,但是我无法使用yup来实现反应选择验证。
如何使用yup实现它。请帮我解决这个问题。
先谢谢了。最好的答案将不胜感激。
答案 0 :(得分:3)
https://codesandbox.io/s/03zxq01okp
topics
更改为topic
max
替换为required
ensure
默认添加为空字符串以处理已清除的选定值(https://github.com/jquense/yup#stringensure-schema)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{" "}