5

I am using required attribute of html5 that make sure the field is not empty. I have a submit button and the validation is triggered after this button is pressed. I just want to keep this validation and not submit the form.

render() {
  <form role="form">
    <input type="number" value={this.state.value} required/>
    <button type="submit" onClick={this.onFinish.bind(this)}>Submit</button>
  </form>
}

onFinish(event) {
    // event.target.checkValidity(); // something like this?
    event.preventDefault();
  }

When I use event.preventDefault() the form is not submitted but the validations are also not run. How can I do this? I do not want to write a custom validation for things like required, as they are already available and I want to use the existing one.

5 Answers 5

7

Sorry, I'm late to the party. You can use e.target.checkValidity() which checks whether the element has any constraints and whether it satisfies them. If the element fails its constraints, the browser fires a cancelable invalid event at the element and then returns false. use along with on submit. Here is how I did it:

handleSubmit(e){
  e.preventDefault();
  if(e.target.checkValidity()){ // use can also use e.target.reportValidity
   // submitForm
 }
}
render(){
 return(
  <form onSubmit={e => this.handleSubmit(e)}>
    <input type="number" value={this.state.value} required/>
    <button type="submit">Submit</button
  </form>
 )
}

just added onSubmit to the form and passed the event.

Sign up to request clarification or add additional context in comments.

Comments

3

I ran into the same problem, but I need the onClick handle because I have several submit buttons.

I wanted to differentiate both buttons and still use the built-in form validation for required fields.

This was working for me:

render() {
  <form role="form" ref="submitForm">
    <input type="number" value={this.state.value} required/>
    <button type="submit"
            onClick={this._onFinishOne.bind(this)}>
      Submit One
    </button>
    <button type="submit"
            onClick={this._onFinishTwo.bind(this)}>
      Submit Two
    </button>
  </form>
}

_onFinishOne(event) {
    if (this.refs.submitForm.reportValidity()) {
      event.preventDefault();
      // do something here
    }
}

_onFinishTwo(event) {
    if (this.refs.submitForm.reportValidity()) {
      event.preventDefault();
      // do something here
    }
}

1 Comment

I was looking for this
0

If you don't want to submit form on click of button, why won't you remove type="submit" from button and use type="button" instead?

Seems you are calling you validation actions on submit of form which is causing this issue. Can you please paste your rest of code here or on fiddler for better input?

Regards, sachin

1 Comment

@AnkitSinghaniya Can you please share your validation binding code. Are you triggering your validation binding on Submit?
0

Via trial and error, I've found a solution that seems to work fine. If you know a better or different way please let me know. This is what I did:

render() {
  <form role="form" onSubmit={this.onFinish.bind(this)>
    <input type="number" value={this.state.value} required/>
    <button type="submit">Submit</button>
  </form>
}

onFinish(event) {
    // All Validations triggered and this function is only called
    // when everything is valid, so I do my custom post here.
  }

Just moved the method form onClick of submit button to onSubmit of form.

Comments

0

There are various ways in which you can validate the form, i use semantic ui framework, so i prefer its inbuilt validation and for special cases, i validate the data in submit function handler,

so its just a matter of choice, how you prefer to validate. Also i don't use normal form submission so all form in my case are ajax forms and data submission is handled by Actions

2 Comments

I checked out semantic ui framework, it looks great, but I am using a custom theme thus I will have to use these validations I guess.
You can include other validation framework too, there is no restrictions what library you include

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.