Php – Symfony rendering hidden form fields

formsPHPsymfony

The documentation suggesting making a form class based on your classes, so forms can be reused. It also shows you how to render different fields independently from your form class rather than rendering all of them using {{ form_widget() }}.

As I am building a simple sign up page I only want to display a few of the fields from the User Form class so I render them like this {{ form_widget(form.email) }}.

However because im rendering the fields independently the hidden form field 'CFTOKEN' is not rendered which is required by the symfony framework. So ge the error : The CSRF token is invalid. Please try to resubmit the form.

Nothing in the doucmnetation mentions this or how to render hidden form fields independently…

Best Answer

<div style="display: none;">{{ form_rest(form) }}</div>

Will get your started. Once you get up to speed on S2 then you will find that there are plenty of alternatives.

For example, passing a parameter to your UserFormType's constructor will make it easy to control which fields are created.

Related Topic