同步验证每个 - 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
值。有谁知道这是否得到支持或如何实现这一目标?
答案 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);