React - Redux Form not validating after form reset

Here is the code

import React from 'react';
import PropTypes from 'prop-types';
import { Link, Redirect } from 'react-router-dom';
import { Field, reduxForm } from 'redux-form';
import { connect } from 'react-redux';
import { Section, RenderField, Block } from 'components';
import { validateRegisterForm as validate } from 'utils/validations';
import { forgotPassword } from 'redux/modules/auth';

const mapStateToProps = state => {
    const { sending, sendSuccess } = state.auth;

    return { sending, sendSuccess };
};

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

const reduxConnector = connect(mapStateToProps, { forgotPassword });

class ForgotPassword extends React.Component {
    static propTypes = {
        sending: PropTypes.bool,
        forgotPassword: PropTypes.func.isRequired,
        handleSubmit: PropTypes.func.isRequired,
    };

    static defaultProps = {
        sending: false,
        sendSuccess: false,
    };

    componentWillReceiveProps(nextProps) {
        if (nextProps.sendSuccess) {
            this.props.reset();
        }
    }

    handleFormSubmit = data => {
        this.props.forgotPassword(data);
    };

    render() {
        const { handleSubmit, reset, sending } = this.props;

        return (
            <div>
                <Section>
                    <div className="form-popup">
                        <div className="form-popup-content">

                            <Block loading={sending}>
                                <form
                                    id="register-form"
                                    name="register-form"
                                    method="POST"
                                    onSubmit={handleSubmit(this.handleFormSubmit)}
                                >

                                    <Field
                                        name="email"
                                        type="email"
                                        component={RenderField}
                                        label="Alamat Email"
                                        description="contoh: email@example.com"
                                    />

                                    <button type="submit" className="button mid primary m-bottom-25">
                                        Reset
                                    </button>
                                </form>

                            </Block>
                        </div>
                    </div>
                </Section>
            </div>
        );
    }
}

export default reduxConnector(reduxFormDecorator(ForgotPassword));

      

Validation works well in the beginning. but when the form is submitted and called this.props.reset()

(input becomes empty) and I try to submit it again, it does not validate the input (input is empty).

Any solution?


validation.js

export function validateRegisterForm(values) {
    const errors = {};
    let hasErrors = false;

    if (!values.first_name || values.first_name.trim() === '') {
        errors.first_name = 'Required';
        hasErrors = true;
    }

    if (!values.email || values.email.trim() === '') {
        errors.email = 'required';
        hasErrors = true;
    } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) {
        errors.email = 'required';
        hasErrors = true;
    }

    if (!values.password || values.password.trim() === '' || values.password.length < 6) {
        errors.password = 'required';
        hasErrors = true;
    }

    return hasErrors && errors;
}

      

+4


source to share


2 answers


Use this.props.initialize()

instead of using this.props.reset()

.



0


source


This seems to be a known issue (check that validation fails after reset # 2971 )

There are several possible workarounds in the discussions, like this one :

this.props.destroy();
this.props.initialize();

      



or is it

shouldError: ({ props }) => {
  return props.invalid;
}

      

0


source







All Articles