Redux-form validation on submit

4.2k views Asked by At

I have a problem where in when I focus my mouse pointer into the textbox and then removed the focus it displays a validation error. What I want to achieve is when the user just submit the form or press the submit button that's the time when validation will happen.

Here's my code so far:

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

There are 1 answers

2
gustavohenke On BEST ANSWER

Set the touchOnBlur option to false (it's true by default):

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

Docs