10.
Form Rendering and Form Variables
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
20 Comments
Hmmmm.... No auto complete here?
Never mind. It was wrong file.
How Do I render a form without a label.. I tried {'label' : ''} but it will give me an error! saying "put some value inside idiot".
Hey Jian!
Hmm, I haven't got that error message before, but I get the idea ;)
If you don't want to render a label you only have to set to "false" the label attribute, you can do it from the "FormType" class using the "FormBuilder" or inside the template: {'label' : false}
Cheers!
great! thank you
Hi! Congratulations, your tutorials are really impresive, easy to follow and understand. Thank you very much!
That being said, i am facing a problem and i can't seem to find a proper solution for it.
Basically,
i would like to display a search form that would filter the list of
elements being displayed by several entity fields.
This i think i did
it correctly, but when i add a paginator, i can't find a way to
properly pass the filters selected in the paginator.
For example, in
the genus project, add a search form by genus name, creation date, etc
in the genus list controller with pagination.
Any guidance will be really appreciated, or maybe a tutorial about this point will be great also.
Thanks in advanced, and excuse my poor english
Hey Jacobo,
May I know what paginator do you use? Is it an Open Source lib like WhiteOctoberPagerfantaBundle? Generally, you need to create a method in entity repository, build the query which applies filtering. Then you need to pass this query to the paginator and it did the trick.
Cheers!
Hey Victor, thanks for your reply. Yes, that's kind of what i do. I have a method in the repository that filters and sets the limit and the offset. Then returns a native orm paginator object. Then in the controller, if user click the search button i get the $request->request var, or if used the paginator i get the $request->query var. Then in the template i send the params as get parameters in each paginator link. It looks a bit messy, was wondering if there was any workaround.
Thank you for the help!
Hey Jacobo,
First of all, generally the GET method is used for search. Actually, Google and GitHub use GET method as well. Then you don't need t worry about POST data, you always will work with $request->query. And this way you can send someone a link to your search results which is impossible with the POST. IIRC, paginators like WhiteOctoberPagerfantaBundle or KnpPaginatorBundle fix this out of the box. So I recommend you to take a look at them, they will make your life a bit easier by rendering the paginator's links with other query parameters you have on the page :)
Cheers!
Thank you Victor! I will definetly check them out.
In Web Profiler `View Variables` list `label` variable is null. Shouldn't it be 'Number of species'?
Hey Serge,
We overwrite label on template level, i.e. setting "Number of species" in admin/genus/new.html.twig - probably that's the key. I think if you set the label on Form Type level - it would be there. Probably this is a bug, or maybe just a some kind of limitations, Im not sure.
Cheers!
How are you getting code completion for stuff like {{form_row(form.name)}} ? In my twig template it doesn't actually seems to now the form is representing the Person entity (in my data model). Is it down to consistent naming conventions?
I answered it myself by adding {# personForm \AppBundle\Entity\Person #} but your example doesn't have the equivalent for the tutorial's data mode. So how is it working in your code? Does location of the form make a huge difference perhaps?
Hey Richard
I suppose you are using "PhpStorm", if so I think you already know about the symfony plugin that you can install. I don't know how it works internally but you can configure it in the settings window -> plugins -> symfony. and there is an option for Twig.
I believe it checks your controller actions, and checks which template you are using and which variables you are passing into it, so it can give you autocomplete
They have a documentation if you want to go deeper http://symfony2-plugin.espe...
I hope it may helped you :)
You say in the video that when using the form_widget you can't change the order of the fields. However according to my tests it does appear that the order in which you add the fields to the form builder is the order they appear on the form.
Question Title displays First on the Form
VS
Question Title displays 2nd on the Form
Yo Terry!
You're totally right as usual :). You *can* change the order... you just can't do it from inside of the template (only from inside the form class like you added here). This is an "accidental" feature... meaning it's not in purpose that it renders in the same way that you build your fields. That being said, it's not going to change ever, so this *is* a reliable way to control the order of your fields.
The next question is, should you do this? It depends: if you're a one-(wo)man army, maybe! But if you may ever work with frontend people, then the more display logic you can put into the template, the better: we want to simply be able to tell them to open "form.html.twig" to make changes, not also to need to hunt down a form class or controller :).
Cheers!
Got it... As always, thanks for the informative response!
You can actually change the order of the fields by changing the order of the properties in GenusFormType's buildForm() function
Hey Helene Shaikh
You are totally right, that's another way to change the order of the fields, but if you are going to reuse that FormType, you can't change it without affecting others
Have a nice day :)
"Houston: no signs of life"
Start the conversation!