2

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

3 Answers 3

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

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

Comments

4

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

7 Comments

Ben, read the description on form_rest -- "It's a good idea to always have this somewhere inside your form as it'll render hidden fields for you and make any fields you forgot to render more obvious (since it'll render the field for you)." --- Nowhere in the Symfony documentation does it say "render each form field you want to display, and ignore the rest of the fields you have defined in your Form Type".
So what would be the point in supporting the rending of individual form fields...
To customize the display of your forms. The way it looks.
And why would it be such an ask to have the flexibility of rendering different fields within your form class for different things. Surely its an obvious thing, to be able to render the form differently for different things for example login and signup
So the only purpose is the customise the display of my form whilst completely bypassing the ability to customise what fields the form contains..anyway we could argue about that point all day, its just my opinion that it should be better supported for that kind of thing as form classes would differ from page to page depending on the action required again like logging in and signing up
|
2

Render the rest of your fields with: {{ form_rest(form) }}

http://symfony.com/doc/2.0/reference/forms/twig_reference.html#form-rest-view-variables

11 Comments

Like Mike says. Alternatively disable csrf as explained in symfony.com/doc/2.0/book/forms.html#csrf-protection
Yes, disabling CSRF is an alternate solution, but be forewarned that it is not recommended to disable CSRF on any forms that might pass sensitive data.
@Mike I always go for form_rest and never disable csrf, the main reason I 'commented' rather than 'answered'. @Ben_hawk why do you have fields in your form that you don't want rendered in the twig output?
+1 @Qoop -- Ben, you shouldn't prematurely say you "hate symfony" due to lack of knowledge on how it works.
Absolutely, (not wanting to devolve into conversation) but Symfony is only as good as you make it. It's a bitch to get into but repays in dividends.
|

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.