【问题标题】:How do the yup validation for react-selectreact-select的yup验证如何
【发布时间】:2019-07-23 02:49:16
【问题描述】:

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

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

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

【问题讨论】:

    标签: reactjs react-select yup


    【解决方案1】:

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

    • topics 更改为topic
    • 将主题验证类型从数组更改为字符串
    • 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{" "}
    

    【讨论】:

    • 谢谢@lecstor
    • @lector,我遵循了同样的例子。花了一天的时间试图为单个“选定”项目找出这个问题。非常感谢。
    猜你喜欢
    • 2019-12-26
    • 2020-01-01
    • 2020-12-06
    • 2021-11-01
    • 2021-10-05
    • 2019-10-09
    • 2019-09-12
    • 1970-01-01
    • 2020-02-05
    相关资源
    最近更新 更多