基于另一个选择字段的值进行验证

huangapple go评论59阅读模式
英文:

Yup validation based on value of another select field

问题

我有两个选择输入 pickUpTimedropOffTime 在 react-hook-form 中,用一个字符串数组填充。数组 intervalTime 是按照15分钟间隔排列的时间段。

const IntervalTime = [
    '09:00',
    '09:15',
    '09:30',
     ...
]

如果 dropOffTimepickUpTime 之前,我想要添加一个错误,我考虑比较所选时间段的索引来实现这个目标。

resolver: yupResolver(yup.object({
    pickUpTime: yup.string().required('required'),
    dropOffTime: yup.string().when(['pickUpTime', 'dropOffTime'], {
        is: (pickUpTime, dropOffTime) =>
            IntervalTime.indexOf(dropOffTime) < IntervalTime.indexOf(pickUpTime),
        then: yup.string().required('Drop off time must be after pick up time'),
    }),
}))

但我得到了 Error: Cyclic dependency, node was:"dropOffTime" 的错误消息。

英文:

I have two select inputs pickUpTime and dropOffTime in react-hook-form populated with an array of strings. The array intervalTime is an ordered time slots with a 15 minutes interval

const IntervalTime = [
    &#39;09:00&#39;,
    &#39;09:15&#39;,
    &#39;09:30&#39;,
     ...
]

I want to add an error if dropOffTime is before pickUpTime and I thought of comparing the indexes of the selected time slots to do so

 resolver: yupResolver(yup.object({
            pickUpTime: yup.string().required(t(&#39;required&#39;)),
            dropOffTime: yup.string().when([&#39;pickUpTime&#39;, &#39;dropOffTime&#39;], {
                is: (pickUpTime, dropOffTime) =&gt;
                    IntervalTime.indexOf(dropOffTime) &lt; IntervalTime.indexOf(pickUpTime),
                then: yup.string().required(&#39;Drop off time must be after pick up time&#39;),
            }),
        }))

but I get Error: Cyclic dependency, node was:&quot;dropOffTime&quot;

答案1

得分: 1

只需使用.test()并编写您自定义的验证规则。

英文:

just use .test() and write your custom validation rules.

答案2

得分: 0

你可以使用评论中提到的 transform() 方法之外,作为另一种选择,你可以使用 test 方法。这可能会更容易使用。

文档链接

英文:

Other than using transform() stated in the comments, as an alternative, you can use the test method. It might feel easier to use.

Documentation

答案3

得分: 0

I solved it like this:

resolver: yupResolver(yup.object({
    pickUpDate: yup.date().required(t('required')),
    pickUpTime: yup.string().required(t('required')),
    dropOffDate: yup.date().min(yup.ref('pickUpDate'), t('date-before')).required(t('required')),
    dropOffTime: yup.string().test('time', 'Drop off time must be after pick up time', function (value) {
        const { pickUpTime, pickUpDate, dropOffDate } = this.parent;
        if (!pickUpTime || !pickUpDate || !dropOffDate || !value) return true; // skip validation if any field is empty
        return (dropOffDate > pickUpDate) || (dayjs(dropOffDate).date() === dayjs(pickUpDate).date() && dayjs(dropOffDate).month() === dayjs(pickUpDate).month() && IntervalTime.indexOf(value) >= IntervalTime.indexOf(pickUpTime));
    }).required(t('required')),
}))

I added a bit of validation to check if dropOffDate is after pickUpDate. In that case, there's no need to check time as well.

英文:

I solved it like this:

resolver: yupResolver(yup.object({
            pickUpDate: yup.date().required(t(&#39;required&#39;)),
            pickUpTime: yup.string().required(t(&#39;required&#39;)),
            dropOffDate: yup.date().min(yup.ref(&#39;pickUpDate&#39;), t(&#39;date-before&#39;)).required(t(&#39;required&#39;)),
            dropOffTime: yup.string().test(&#39;time&#39;, &#39;Drop off time must be after pick up time&#39;, function (value) {
                const { pickUpTime, pickUpDate, dropOffDate } = this.parent;
                if (!pickUpTime || !pickUpDate || !dropOffDate || !value) return true; // skip validation if any field is empty
                return (dropOffDate &gt; pickUpDate) || (dayjs(dropOffDate).date() === dayjs(pickUpDate).date() &amp;&amp; dayjs(dropOffDate).month() === dayjs(pickUpDate).month() &amp;&amp; IntervalTime.indexOf(value) &gt;= IntervalTime.indexOf(pickUpTime));
            }).required(t(&#39;required&#39;)),
        }))

I added a bit of validation to check if dropOffDate is after pickUpDate. In that case there's no need to check time as well.

huangapple
  • 本文由 发表于 2023年4月13日 18:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定