简体   繁体   English

是的如何在同一个字段上获取字段 using.when 的值?

[英]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.如果您不提供任何arraystring ,则当前项目的值将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.

相关问题 获取另一个字段的值以在 Yup Schema 中进行验证 - Get the value of another field for validation in Yup Schema 如何使用 formik 和 yup 在需要来自另一个字段的值的字段中创建自定义验证 - How to create custom validation in a field that need value from another field using formik and yup 使用 react.js 时出现此错误:TypeError: field.resolve is not a function - I get this error when using react.js yup: TypeError: field.resolve is not a function 如何使用 Yup 验证数字字段值? 我想检查提供的数字是否以 0.01 或 0.1 为增量 - How to validate a numeric field value in react using Yup? I want to check if the number provided is in increments of 0.01 or 0.1 如何使用 formik 和 yup 验证对数组字段进行验证 - How to perform validation of a array field using formik and yup validation 如何使用带有 yup 的反应钩子验证一个 object 中的两个字段 - How to validate the two field in one object using react hook with yup 如何使用yup检查不以'-','+','_'开头的输入字段 - How to check the input field that does not starts with '-','+','_' using yup 使用jQuery单击按钮时如何获取输入字段的值? - How to get the value of input field when button clicked using jquery? 是的 - 在错误消息中输出字段值 - Yup - Output a field value in the error message 是的,基于非字段值的条件验证 - Yup conditional validation based on a a non field value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM