gpt4 book ai didi

javascript - Redux 表单验证在 onChange 处理函数之后运行

转载 作者:行者123 更新时间:2023-12-03 02:02:50 26 4
gpt4 key购买 nike

我有以下 redux 形式:

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

import { createExpense } from '../../actions';
import { validateName, validateImage } from '../../helpers/expense_utils';
import { renderInputField, validate, renderTextAreaField, renderDropzoneField } from '../../helpers/form_utils';

const validators = [
{
field: 'title',
validator: validateName
},
{
field: 'description',
validator: validateDescription
},
{
field: 'amount',
validator: validateAmount
},
{
field: 'image',
validator: validateImage
}
];

class NewExpense extends Component {

constructor(props) {
super(props);
this.state = {
error: undefined,
imagePreviewUrl: undefined
};
}

_onSubmit = values => {
let formData = new FormData();
formData.append('title', values.title);
formData.append('description', values.description);
formData.append('amount', values.amount);
formData.append('file', values.image[0]);
this.props.createExpense(formData, this.props.groupId, () => this.props.onClose(), error => this.setState({error: error}));
}

_onImagePreviewChange = files => {
//this.props.inputErrors.image is one step behind
this.setState({
imagePreviewUrl: files[0].preview
});
}

render() {
const { handleSubmit } = this.props;
const { imagePreviewUrl } = this.state;
return (
<div>
<form onSubmit={ handleSubmit(this._onSubmit.bind(this)) }>
<Field name="title" label="Name" type="text" component={ renderInputField }/>
<Field onChange={this._onImagePreviewChange.bind(this)} previewUrl={ imagePreviewUrl } name="image" label="Image" component={ renderDropzoneField } />
<button type="submit" className="btn btn-primary">Create</button>
<button type="button" className="btn btn-primary" onClick={this.props.onClose}>Cancel</button>
</form>
{ this.state.error ? <span>{this.state.error}</span> : <noscript/> }
</div>
);
}
}

export default connect(state => ({
inputErrors: getFormSyncErrors('NewExpense')(state)
}), { createExpense })(reduxForm({
validate,
form:'NewExpense',
validators
})(NewExpense));

我想访问_onImagePreviewChange内表单的字段级错误,并在mapStateToProps内设置inputErrors

问题是验证函数在 onChange 处理函数之后运行。因此,错误消息始终是陈旧的。例如,如果用户选择了不允许的类型的文件,则消息将为未定义,下次如果用户选择允许的类型的文件,这次就不会是未定义 code> 作为上一步中分配给它的错误。

为什么会发生这种情况以及我应该如何解决它?

最佳答案

验证只能在某些字段发生更改后运行,这就是为什么第一次使用无效值调用 _onImagePreviewChange 时,仍然没有验证错误(正如您所说,验证落后一步)。

我建议使用 componentWillReceiveProps 方法,并在此方法中访问下一个和当前的验证错误,检查是否修复或生成了某些错误,并采取您需要采取的操作。例如:

 componentWillReceiveProps(newProps) {
// here newProps.inputErrors is up-to-date while this.props.inputErrors is the current
if (newProps.inputErrors && newProps.inputErrors !== this.props.inputErrors) {
console.log(newProps.inputErrors); // up-to-date input errors
if (newProps.inputErrors.image) {
// clear invalid image field if you need to
this.props.change('image', '');
}
}
}

关于javascript - Redux 表单验证在 onChange 处理函数之后运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49936192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com