同步验证以抛出表单级别错误(而不是字段级别)

时间:2018-01-08 04:25:52

标签: redux-form

同步验证每个 - https://redux-form.com/7.1.2/examples/syncvalidation/ - 我试图抛出表单级错误。就像我们做throw new SubmissionError({ _error:'form level error from on submit' })时一样,但我无法弄明白。我尝试使用特殊的_error密钥,甚至只使用error密钥:

reduxForm({
    validate: values => ({ error:'form level??', _error:'form level alt??' })
})

但是,它未设置FormProp s error值。有谁知道这是否得到支持或如何实现这一目标?

1 个答案:

答案 0 :(得分:3)

要在验证后将表单级错误传递给表单,您应该将_error属性添加到redux-form config validate函数返回的对象。

错误将作为error道具传递给您的表单组件。

例如:

import React from 'react';
import { Field, reduxForm } from 'redux-form';

const ExampleForm = (props) => {
  const { handleSubmit, pristine, submitting, error } = props;
  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label>Name</label>
        <div>
          <Field
            name="name"
            component="input"
            type="text"
            placeholder="Name"
          />
        </div>
      </div>
      <div>Error: {error}</div>
      <div>
        <button type="submit" disabled={pristine || submitting}>Submit</button>
      </div>
    </form>
  );
};

const validate = (values) => ({
  _error: `form level error for firstName = '${values.name || ''}'!`,
});

export default reduxForm({
  form: 'exampleForm',
  validate,
})(ExampleForm);

Edit Redux Form - Simple Example