Redux-form: Two different ways to validate a form depend on two different buttons.

Created on 27 Apr 2016  路  2Comments  路  Source: redux-form/redux-form

Hi to everybody. Is it possible to have two different validations? So i have two buttons on my form and i want to invoke a different validation for each of them.

question

Most helpful comment

Please try to use StackOverflow for questions using the tag redux-form.


Current API only supports one validate callback. However you can write it in a way that it chooses the right validator. The easiest way is to check for a special value:

function validateA(values) { /* ... */ }
function validateB(values) { /* ... */ }

function validate(values) {
  if (values.__type === "A") {
    return validateA(values)
  } else {
    return validateB(values)
  }
}

The above solution reads the value of the __type field and apply the correct validator. According to your question, you are not depending on any field so you have to trick validate.

import {reduxForm, change} from "redux-form"

reduxForm(
  {
    form: "myform",
    fields: ["__type"],
    validate,
  },
  null,
  {
    change,
  }
)(
  class MyForm extends React.Component {
    handleSubmit(type) {
      this.props.change("__type", type)
      this.props.handleSubmit()
    }

    render() {
      return (
        <div>
          <button onClick={() => this.handleSubmit("A")}>Case A</button>
          <button onClick={() => this.handleSubmit("B")}>Case B</button>
        </div>
      )
    }
  }
)

All 2 comments

Please try to use StackOverflow for questions using the tag redux-form.


Current API only supports one validate callback. However you can write it in a way that it chooses the right validator. The easiest way is to check for a special value:

function validateA(values) { /* ... */ }
function validateB(values) { /* ... */ }

function validate(values) {
  if (values.__type === "A") {
    return validateA(values)
  } else {
    return validateB(values)
  }
}

The above solution reads the value of the __type field and apply the correct validator. According to your question, you are not depending on any field so you have to trick validate.

import {reduxForm, change} from "redux-form"

reduxForm(
  {
    form: "myform",
    fields: ["__type"],
    validate,
  },
  null,
  {
    change,
  }
)(
  class MyForm extends React.Component {
    handleSubmit(type) {
      this.props.change("__type", type)
      this.props.handleSubmit()
    }

    render() {
      return (
        <div>
          <button onClick={() => this.handleSubmit("A")}>Case A</button>
          <button onClick={() => this.handleSubmit("B")}>Case B</button>
        </div>
      )
    }
  }
)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Webkadabra picture Webkadabra  路  42Comments

alexw668 picture alexw668  路  48Comments

stefensuhat picture stefensuhat  路  83Comments

eliseumds picture eliseumds  路  38Comments

jervi picture jervi  路  42Comments