2

I have a form with three buttons. One for adding objects, one for deleting and one for editing objects.

I now want to disable html5-validation in the add-button using the new form buttons introduced in Symfony 2.3. Is this possible or do I have to create a new button type? How could I do that?

x0rg
  • 117
  • 2
  • 6

2 Answers2

7

To disable form validation, use formnovalidate on submit button.

For example:

<div class="row">
  <input type="email">
  <input formnovalidate type="submit" value="Remove row">
</div>
<div class="row">
  ...
</div>
<input type="submit" value="Submit rows">

Note the formnovalidate is on the remove button, so even if field next to it contains invalid e-mail, it still can be removed. The real submit button does not have the formnovalidate attribute, so all e-mail must be valid before actually submitting the form.

Josef Kufner
  • 2,851
  • 22
  • 28
4

All you need to do is to add the html attribute novalidate or novalidate="novalidate" to the element. You can do this same way you would add any other attribute from either the form type of the twig template. For example:

{{ form_widget(form.add, { 'attr': {'novalidate': 'novalidate' }}) }}

Actually, reading your question again, I'm not entirely sure what sort of validation you are trying to do on a button. You have to put novalidate on all the elements that you don't want to validate.

You can also put novalidate on the form itself:

<form action="demo_form.asp" novalidate>
Cerad
  • 48,157
  • 8
  • 90
  • 92
  • The point of novalidate on a button is to disable form validation for all other fields. For example when there is submit button and remove row button, you may want to remove row with invalid field, but you want to submit only a valid form. (Unfortunately this is not what `novalidate` does.) – Josef Kufner Sep 03 '15 at 07:17