question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validate method not called in redux-form

See original GitHub issue

I am using redux-form and took example from official website, here i am facing issue that Validate method not getting called on button click. below is my code

import React, { Component } from 'react';
import { Field, reduxForm, initialize } from 'redux-form';
import { connect } from 'react-redux';
import * as actions from '../../actions';

const renderField = field => (
    <div className="form-group">
      <label>{field.input.label}</label>
      <input {...field.input} value={field.value} onChange={(e)=> console.log(e.target.value) } />
      {field.touched && field.error && <div className="error">{field.error}</div>}
    </div>
);


const renderSelect = field => (
  <div>
    <label>{field.input.label}</label>
    <select {...field.input}/>
    {field.touched && field.error && <div className="error">{field.error}</div>}
  </div>
);



function validate(formProps) {console.log("vvv---", formProps);
  const errors = {};

  if (!formProps.firstName) {
    errors.firstName = 'Please enter a first name';
  }

  if (!formProps.lastName) {
    errors.lastName = 'Please enter a last name';
  }

  if (!formProps.email) {
    errors.email = 'Please enter an email';
  }

  if (!formProps.phoneNumber) {
    errors.phoneNumber = 'Please enter a phone number'
  }

  if(!formProps.sex) {
    errors.sex = 'You must enter your sex!'
  }

  return errors;
}
class ReduxFormTutorial extends Component {

  componentDidMount() {
    this.handleInitialize();
  }

  handleInitialize() {
    const initData = {
      "firstName": "Puneet",
      "lastName": "Bhandari",
      "sex": "male",
      "email": "test@gmail.com",
      "phoneNumber": "23424234"
    };

    this.props.initialize(initData);

  }

  handleFormSubmit(formProps) {
    //console.log(formProps);
    this.state = { firstName : null };
    this.props.submitFormAction(formProps);
  }

  //our other functions will go here

  render() {
    const { fields : [firstName, lastName], handleSubmit } = this.props;
    return (
      <div>
        <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>

          <Field name="firstName" value="" type="text" component={renderField} label="First Name"  />
          <Field name="lastName" value="" type="text" component={renderField} label="Last Name"/>
          <Field name="sex" component={renderSelect} label="Gender">
            <option></option>
            <option name="male">Male</option>
            <option name="female">Female</option>
          </Field>
          <Field name="email" type="email" component={renderField} label="Email" />
          <Field name="phoneNumber" type="tel" component={renderField} label="Phone Number"/>

          <button action="submit">Save changes</button>
        </form>
      </div>
    )
  }
}

const form = reduxForm({
  form: 'ReduxFormTutorial',
  fields: [ 'firstName', 'lastName' ],
  validate
});

function mapStateToProps(state) {
  return {
    user: state.user
  };
}

export default connect(mapStateToProps, actions)(form(ReduxFormTutorial));

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
erikrascommented, May 4, 2017

It doesn’t get called when you click the submit button. It has already been called when the form was rendered (and again on every change), so it knows the validity of the form.

A few pointers about your code:

  handleFormSubmit(formProps) {
    //console.log(formProps);
    this.state = { firstName : null }; // <--------- 🚫
    this.props.submitFormAction(formProps);
  }

It’s a really bad idea to be setting a member variable called state, as that has special meaning in React.

const form = reduxForm({
  form: 'ReduxFormTutorial',
  fields: [ 'firstName', 'lastName' ], // <--------- 🚫
  validate
});

Passing fields to reduxForm() like that is the v5 API. See Migration Guide.

1reaction
danielrobcommented, Jul 20, 2017

Closing due to non response + probable user error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validate method not called in redux-form · Issue #2887 - GitHub
I am using redux-form and took example from official website, here i am facing issue that Validate method not getting called on button ......
Read more >
Validate function not called in redux-form - Stack Overflow
I am using redux-form 6.3 version and in this validate function getting called. below is my code. Please check what is issue in...
Read more >
Synchronous Validation Example - Redux Form
IMPORTANT: If the validation function returns errors and the form does not currently render fields for all of the errors, then the form...
Read more >
Field-Level Validation Example - Redux Form
If the value is invalid, the validation function should return an error. This is usually a string, but it does not have to...
Read more >
Async Change Validation Example - Redux Form
Asynchronous validation will not be called if synchronous validation is failing for the field just blurred. The errors are displayed in the exact...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found