如何从 redux-form 获取字段值
How to get field values from redux-form
我无法在提交回调中获取字段值。相反,我在那里收到一个事件。
谁能告诉我我做错了什么
render() {
const { fields, handleSubmit, submitting, buttonWrapper, btnText } = this.props;
return (
<form name="AddEvent" onSubmit={handleSubmit}>
{fields.map(field => (
<div className={field.wrapperClass}>
<Field
name={field.name}
type={field.type || 'text'}
component={mapComponentClass(field.componentClass)}
validate={mapValidators(field.validate)}
props={field.inputProps}
label={field.label}
/>
</div>
))}
<div className="form-submit-wrap container">
<button
type="submit"
disabled={submitting}
className="form-submit"
>
{submitting ? 'Submitting' : 'Submit'}
</button>
</div>
</form>
);
}
handleSubmit
应该是 onSubmit
函数的包装器。
这样试试:
<form onSubmit={handleSubmit(onSubmit)}>
</form>
现在 onSubmit
将收到 1 个包含所有表单值的参数。
不是您要求的,但作为旁注,如果您愿意,可以从 onSubmit
函数内部触发提交验证错误,例如:
throw ReduxForm.SubmissionError({name: 'There\'s something wrong in the name field'});
...由于您的 Ajax 调用使 name
无效。
我无法在提交回调中获取字段值。相反,我在那里收到一个事件。 谁能告诉我我做错了什么
render() {
const { fields, handleSubmit, submitting, buttonWrapper, btnText } = this.props;
return (
<form name="AddEvent" onSubmit={handleSubmit}>
{fields.map(field => (
<div className={field.wrapperClass}>
<Field
name={field.name}
type={field.type || 'text'}
component={mapComponentClass(field.componentClass)}
validate={mapValidators(field.validate)}
props={field.inputProps}
label={field.label}
/>
</div>
))}
<div className="form-submit-wrap container">
<button
type="submit"
disabled={submitting}
className="form-submit"
>
{submitting ? 'Submitting' : 'Submit'}
</button>
</div>
</form>
);
}
handleSubmit
应该是 onSubmit
函数的包装器。
这样试试:
<form onSubmit={handleSubmit(onSubmit)}>
</form>
现在 onSubmit
将收到 1 个包含所有表单值的参数。
不是您要求的,但作为旁注,如果您愿意,可以从 onSubmit
函数内部触发提交验证错误,例如:
throw ReduxForm.SubmissionError({name: 'There\'s something wrong in the name field'});
...由于您的 Ajax 调用使 name
无效。