redux-form: How to disable submit button if at least one Field is not valid?

react disable button on form submit
disable submit button if form is empty react
redux-form submitting
react-final-form disable submit
react disable button onclick
react hook form disable button
redux-form invalid prop
formik disable submit button

I'm rendering the below simple form using redux-form and it's working nicely. Now, I'd like to have the submit button disabled in one more situation: If any of the Field's has an error (i.e. it's meta.error is set).

From lokking into the docs, I suppose it is not possible for the surrounding <form> to know if its <Field> components have an error. Maybe anyone has an idea, how to solve it as easy as using disabled={hasErrors || submitting || pristine}

const EditBlogEntryForm = ({ onSubmit, reset, handleSubmit,
                         pristine, submitting, ...rest }) => {
    console.log('rest: ', rest);
    return (
        <form onSubmit={handleSubmit(onSubmit)}>
            <div className="form-group">
                <Field name="title"
                    type="text"
                    component={renderField}
                    label="Titel"
                    className="form-control"
                    placeholder="Titel eingeben..." />
            </div>
            <div className="form-group">
                <Field name="text"
                    component={renderTextArea}
                    label="Text"
                    className="form-control"
                    placeholder="Textinhalt eingeben..." />
            </div>  
            <div className="form-group">
                <Field name="image"
                    type="text"
                    component={renderField}
                    label="Bild-URL:"
                    className="form-control"
                    placeholder="Bildadresse eingeben..." />
            </div>  
            <div>
                <button type="submit" className="btn btn-default"
                    disabled={submitting || pristine}>
                    Blogeintrag speichern
                </button>
                <button type="button" className="btn btn-default"
                    disabled={pristine || submitting}
                    onClick={reset}>
                    Formular leeren
                </button>
            </div>
        </form>
    );
};

Don't abuse state you need just using this.props for each setState Component one more time will be render

const {invalid} = this.props

return(
<button type="submit" className="btn btn-default"
     disabled={invalid|| submitting || pristine}>
     Blogeintrag speichern
 </button>)

More Document: https://redux-form.com/6.0.0-alpha.4/docs/api/props.md/

Disable form submit button if form is invalid, but enable it for async , Don't abuse state you need just using this.props for each setState Component one more time will be render const {invalid} = this.props return( <button  If you are using the react-redux-form library you can eventually use the onUpdate event on the form to capture the validity state of the form and like this you can change some internal state variable which can be used to deactivate a button.

What you should be able to do is just have a variables called Errors that will be true once your api call comes back with an error

 constructor(super) {
      this.state = {
         errors: false,
      }
 }

 componentWillReceiveProps(nextProps) {
     const that = this;
     if (nextProps.errors) {
        that.setState({errors: true})
     }    
 }

 <button type="submit" className="btn btn-default"
     disabled={this.state.errors || submitting || pristine}>
     Blogeintrag speichern
 </button>

Synchronous Validation Example, Now there are some fields that can receive server-side validation Disable form submit button if form is invalid, but enable it for async I have a use case that I need to disable the Submit button for a form until the form is in a valid state, peterox added a commit to peterox/react-redux-form that referenced  Now our form can be reset and the submit button will be disabled when the form is not valid, when it’s pristine or when it’s in the process of submitting. We also added a select field with a few options populated from an array of available options. Custom Field Components

Redux forms already passes lots of properties into the form. One is invalid. That's what I am using to determine if any of the field validations failed and then disable submit.

https://redux-form.com/6.0.0-alpha.4/docs/api/props.md/

Field-Level Validation Example, IMPORTANT: Synchronous validation happens on every change to your form data, so, if your field value is invalid, your field.error value will always be present. The Field component comes from the redux-form package and it’s how we write the input field. The type prop indicates what type of input it should be, that is, a radio input, a checkbox input, a text input or an email input.

Alastair pointed me into the correct direction (Thanks for that!). I guess this is one of the cases where a local UI-related state is actually very useful. So I refactored the SFC into a react class. That classes' constructor and componentWillReceiveProps look like this:

constructor(props) {
    super(props);
    this.state = {
        errors: false
    };
}

componentWillReceiveProps(nextProps) {
    if (nextProps.invalid) {
        this.setState({errors: true});
    } else {
        this.setState({errors: false});
    }
}

Now using this.state.errors to have the button disabled is working perfectly. As you can see, I had to use the invalid prop form redux-form, because its error prop was always undefined and not to forget to set it true again, if the form is valid. Furthermore I don't know, why you copied the this reference into that in your answer. It wouldn't change any behaviour as it's still pointing to the same object.

redux-form: How to disable submit button if at least one - html, You will probably only want to show validation errors once your field has been When you submit the form, all the fields are marked as touched, allowing any of their [A-Z]{2,4}$/i.test(values.email)) { errors.email = 'Invalid email address' } if else if (Number(values.age) < 18) { errors.age = 'Sorry, you must be at least 18​  Form recipe: Conditionally disabling the Submit button. 28 Nov 2016. You now know about the differences between the controlled vs. uncontrolled form inputs.. You know that controlled inputs are way more powerful and allow to provide a better user experience.

If you are using the react-redux-form library you can eventually use the onUpdate event on the form to capture the validity state of the form and like this you can change some internal state variable which can be used to deactivate a button. Here is some sample code to demonstrate what you can do:

import React, { Component } from 'react';
import { Button, Modal, ModalHeader, ModalBody, Row, Label, Col } from 'reactstrap';
import { Control, LocalForm, Errors } from 'react-redux-form';

const required = (val) => val && val.length;

const maxLength = (len) => (val) => !(val) || (val.length <= len);

const minLength = (len) => (val) => (val) && (val.length >= len);

class CommentForm extends Component {

    constructor(props) {
        super(props);
        this.state = {
            isModalOpen: false,
            isFormInValid: true
        };
        this.toggleModal = this.toggleModal.bind(this);
    }

    toggleModal() {
        this.setState({
            isModalOpen: !this.state.isModalOpen
        })
    }

    handleSubmit(values) {
        console.log("Current state is: " + JSON.stringify(values));
        alert(JSON.stringify(values));
    }

    handleUpdate(form) {
        this.setState({ isFormInValid: !form['$form'].valid });
    }

    render() {
        return (
            <>
                <Button outline onClick={this.toggleModal}>
                    <span className="fa fa-pencil fa-lg"></span> Submit Comment
                </Button>
                <Modal isOpen={this.state.isModalOpen} toggle={this.toggleModal}>
                    <ModalHeader toggle={this.toggleModal}>Submit Comment</ModalHeader>
                    <ModalBody>
                        <LocalForm
                            onUpdate={(form) => this.handleUpdate(form)}
                            onSubmit={(values) => this.handleSubmit(values)}>
                            <Row className="form-group">
                                <Label htmlFor="author" md={12}>Your Name</Label>
                                <Col md={12}>
                                    <Control.text model=".author" id="author" name="author"
                                        placeholder="First Name" className="form-control"
                                        validators={{ required, minLength: minLength(3), maxLength: maxLength(15) }} />
                                    <Errors className="text-danger" model=".author" show="touched"
                                        messages={{ required: 'Required ', minLength: 'Must be greater than 2 characters', maxLength: 'Must be 15 characters or less' }} />
                                </Col>
                            </Row>
                            <Row className="form-group">
                                <Col md={12}>
                                    <Button type="submit" color="primary" disabled={this.state.isFormInValid}>Submit</Button>
                                </Col>
                            </Row>
                        </LocalForm>
                    </ModalBody>
                </Modal>
            </>
        );
    }
}

Form Handling Using Redux Form ← Alligator.io, If the value is invalid, the validation function should return an error. This is usually a string, but it does not have to be. Obviously, you will want to define your  How to use React’s controlled inputs for instant form field validation. Controlled inputs enable simple things, like disabling the Submit button when some fields are missing or invalid. But we’re not stopping there, of course. While a disabled button is nice, the user might not know why they can’t click that button.

Dynamic React Form with Formik, Warnings are errors that do not mark a form as invalid, allowing for two tiers of You will probably only want to show validation errors once your field has been When you submit the form, all the fields are marked as touched, allowing any of else if (Number(values.age) < 18) { errors.age = 'Sorry, you must be at least 18  Man, React form handling sure sucks. What lead me to this ticket is that my submit button is outside of my form component. I want to access properties like "dirty" or "anyTouched" to decide whether to show the save button at all, or whether to disable it.

I'm rendering the below simple form using redux-form and it's working nicely. Now, I'd like to have the submit button disabled in one more situation: If any of the​  3. Implementing redux-form — Update Presentational Component. Click on the below picture to compare *before* & *after* PostsForm. I’ll go over each change in details in the following sections.

Let's explore one easy way to deal with forms in a React/Redux app using the to forms and input fields in web apps is not most people's idea of fun. It expects a config object with at least a unique form name. Now our form can be reset and the submit button will be disabled when the form is not valid,  // Disable the submit button when the form is invalid <Control.button model="user" disabled={{ valid: false }} > Submit! </Control.button> For example: disabled={true} or disabled={false} will disable or enable the control respectively, as will any other primitive value, such as undefined , null , or a number

Comments
  • what you can do is just add in your own variable and put it in the state like Errors. Once that value is false then you can hit the submit button
  • as mentioned by @masoud-soroush, better not abuse the state. See his solution.
  • This suggestion is taken from the docs and does not provide a complete solution.