3

What am doing here is on clicking a link verify if all fields are filled if filled submit form as usual else will show some errors. But how can I continue submiting a form normally after using e.preventdefault in react?

var formComponent = React.createClass({
verifyFields: function (e) {
e.preventDefault();
    if (this.state.primaryGuestName && this.state.primaryGuestMobile) {
       // how can i continue with form submission here(not using xhr)
    } else {
        showErrors();
    }
},

render: function () {
<form action={this.props.action} ref="booking_form" acceptCharset="UTF-8" method="post">
    {form contents}
    <a href="" name="submit_details" onClick={this.verifyFields}
                           className="btn-flat btn-large rd-button">Confirm Details</a>
 </form>
}
}

3 Answers 3

5

To submit the form explicitly you need to call the submit() method

Your verifyFields function would become something like this.

verifyFields: function (e) {
e.preventDefault();
    if (this.state.primaryGuestName && this.state.primaryGuestMobile) {
       // how can i continue with form submission here(not using xhr)
        document.getElementById('myForm').submit();
    } else {
        showErrors();
    }
},
Sign up to request clarification or add additional context in comments.

Comments

1

You shouldn't really query the dom for the form, instead use the onSubmit event and a button then the implementation is a lot cleaner:

var formComponent = React.createClass({
    submit: function(e) {
        if (!this.verifyFields()) {
            e.preventDefault();
            showErrors();
            return false;
        }

        // continue here..
        return true;
    },

    verifyFields: function () {
        return this.state.primaryGuestName && this.state.primaryGuestMobile;
    },

    render: function () {
        return (
            <form action={this.props.action} onSubmit={this.submit} acceptCharset="UTF-8" method="post">
                {form contents}
                <button type="submit" className="btn-flat btn-large rd-button">Confirm Details</button>
            </form>
        );
    }
});

Comments

0

Call submit on the form.

e.preventDefault();
    if (this.state.primaryGuestName && this.state.primaryGuestMobile) {
       e.target.submit();
    } else {
        showErrors();
    }
}

2 Comments

this wont work as it throws undefined function submit()
You're right. I misread you're code as the event being on the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.