Tibo
Tibo

Reputation: 197

Symfony not rendered form fields put to Null

I have a form that has let's say : formField1, formField2, ... formField10.

In my twig view, I want the 5 first form fields to be displayed.

My code looks like something like this :

{{ form_widget(form.formField1, {'attr': {'class': 'form-control'}}) }}
{{ form_widget(form.formField2, {'attr': {'class': 'form-control'}}) }}
{{ form_widget(form.formField3, {'attr': {'class': 'form-control'}}) }}
{{ form_widget(form.formField4, {'attr': {'class': 'form-control'}}) }}
{{ form_widget(form.formField5, {'attr': {'class': 'form-control'}}) }}

So when I submit this, it's fine for those fields : everything works fine in the databse.

But for the other fields (6 to 10), my database shows "NULL" for everything...

So my solution for now is to do this :

<div class="form-group hidden">
     <div class="col-md-10 col-sm-9 col-xs-12">
          {{ form_rest(form) }}
     </div>
</div>

But it's not very safe (F12 + remove hidden property :))

Any idea how NOT to touch those other fields ?

I tried to disable them too :

{{ form_widget(form.formFieldX, {'attr': {'class': 'form-control'}, 'disabled': true}) }}

but it doesn't work either (set to NULL).

Upvotes: 0

Views: 83

Answers (1)

xabbuh
xabbuh

Reputation: 5881

The only safe solution is to exclude these fields from the form type (for example by adding options to turn them on/off under certain conditions).

The Form component, by default, clears (i.e. sets them to null) all form fields that are part of the form type, but not of the request. This can be turned off, but this option is rather meant to allow support for HTTP PATCH requests. Furthermore, if you still kept the fields in your form type, your users could as well manually add the needed HTML code for it and submit them meaning they would again be changed.

Upvotes: 1

Related Questions