【问题标题】:redux-forms field level validation function triggers for every field it associated to and not just the changing fieldredux-forms 字段级验证功能会触发与其关联的每个字段,而不仅仅是更改字段
【发布时间】:2019-11-05 10:26:59
【问题描述】:

我是新来的反应。我正在尝试实现字段级验证,并希望为更改字段触发验证函数(在本例中为 myFunction),而不是为现在正在发生的与函数关联的所有字段。

<Field
    name="username"
    type="text"
    component={renderField}
    label="Username"
    validate={myFunction}
    warn={alphaNumeric}
  />
  <Field
    name="email"
    type="email"
    component={renderField}
    label="Email"
    validate={myFunction}
    warn={aol}
  />

我的功能:

const maxLength = max => value => && value.length > max ? `Must be ${max} characters or less` : undefined

Reference

这是否有可能仅针对变化的字段做出反应来触发它?

【问题讨论】:

    标签: javascript reactjs react-native react-redux redux-form


    【解决方案1】:

    看起来这是不可能的,因为即使您的函数不使用它,redux-forms 也会将所有值传递给验证函数。因此,任何更改都会触发所有字段的所有验证。见:https://redux-form.com/6.6.3/docs/api/field.md/

    【讨论】:

    • 好的,有什么替代方法吗,比如一旦发现第一个无效字段就停止验证?
    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多