[英]Yup How to get the value of field using .when on the same field?
How to get the value of field when using when on the same field?在同一字段上使用when时如何获取字段的值?
email: yup
.string()
.when([], {
is: (val: string) => {
console.log('val = ', val)
return true
},
then: (schema) => {
console.log('schema = ', schema)
return schema
},
})
})
I am assuming you want to get the value
of email in when
condition at is
location as well as at then
location.我假设你想在
when
condition at is
location 以及 at then
location 中获得 email 的value
。 then you have wrote everything correctly.那么你已经正确地编写了所有内容。 just remove starting
[]
empty array in when condition.只需删除 when 条件中的起始
[]
空数组。 It you don't provide any array
or string
then value of current item will get at is
location.如果您不提供任何
array
或string
,则当前项目的值将is
该位置。
ref: React-hook-form validation when input is empty.参考: 输入为空时的 React-hook-form 验证。 Conflict with Yup.matches
与 Yup.matches 冲突
I have updated the below as per your latest comment.我已根据您的最新评论更新了以下内容。 I have tested this in my JavaScript file.
我已经在我的 JavaScript 文件中对此进行了测试。 so you might need to convert it as per TSX syntax.
因此您可能需要根据 TSX 语法对其进行转换。
email: yup
.string()
.when({
is: (val) => {
console.log('val = ', val)
return true
},
then: yup.string()
.test('testKey', 'error', (value) => console.log('value at then', value)),
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.