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.
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.
Most helpful comment
Please try to use StackOverflow for questions using the tag
redux-form.Current API only supports one
validatecallback. However you can write it in a way that it chooses the right validator. The easiest way is to check for a special value:The above solution reads the value of the
__typefield and apply the correct validator. According to your question, you are not depending on any field so you have to trickvalidate.