提交时的Redux表单验证

时间:2016-12-24 07:39:38

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

我有一个问题,当我将鼠标指针聚焦到文本框中然后删除焦点时,它会显示验证错误。我想要实现的是当用户只提交表单或按下提交按钮时,验证将会发生。

到目前为止,这是我的代码:

const form = reduxForm({
    form: 'BoardAddModalForm',
    validate
});

const renderField = field => (
    <div className="form-group">
        <input className={[forms.inputTextboxMd,"form-control"].join(' ')} {...field.input} type={field.type} placeholder={field.placeholder} autoComplete="off" />
        <div className={utils.errorText}>{field.meta.touched && field.meta.error && <span>{field.meta.error}</span>}</div>
    </div>
);

function validate(formProps){
    const errors = {};

    if(!formProps.name){
        errors.name = "Board name is required";
    }

    return errors;
}
class BoardAddModal extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            name: ''
        };
    }

    addBoard(formProps) {
        this.props.dispatch(reset('BoardAddModalForm'));
    }

    render() {

        const { error, handleSubmit } = this.props;
        return (
        <Modal show={this.props.show} onHide={this.props.onHide} bsSize="small" aria-labelledby="contained-modal-title-sm">
            <Modal.Header closeButton>
                <Modal.Title id="contained-modal-title-sm">Create Board</Modal.Title>
            </Modal.Header>
            <Modal.Body>
                <form onSubmit={handleSubmit(this.addBoard.bind(this))}>

                    <Field name="name" component={renderField} type="text" placeholder="What are you organizing" />
                    <button className="btn">Submit</button>
                </form>

            </Modal.Body>
        </Modal>
        );
    }
}

export default (form(BoardAddModal));

1 个答案:

答案 0 :(得分:4)

.previousSibling选项设置为touchOnBlur(默认情况下为false):

true

Docs