Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony rendering hidden form fields

Tags:

forms

php

symfony

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...

like image 627
Ben_hawk Avatar asked Apr 18 '26 06:04

Ben_hawk


2 Answers

<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.

like image 106
Cerad Avatar answered Apr 19 '26 21:04

Cerad


This is the only way really..

{{ form_widget(form._token) }}

Pretty stuiped how Symfony documentation shows you how to render each form field independently and then say you have to use {{ form_rest(form) }} to generate the rest of the required fields like CFtoken, as it also renders all the form fields in your form class that you purposely avoided rending by doing them independently instead of just using {{ form_widget() }}

GRRR

like image 44
Ben_hawk Avatar answered Apr 19 '26 20:04

Ben_hawk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!