Strange form behaviour in Twig - php

I have a form that I display row by row in Twig. On a particular row, I try to pass HTML attributes as options, like this :
{{ form_row(form.classic_text, {'attr': {'class': 'test'}}) }}
The option is passed to the form, as I can see the class being applied to the form. However, when I put a dump right after the above code, like this :
{{ form_row(form.classic_text, {'attr': {'class': 'test'}}) }}
{{ dump(form.classic_text) }}
the attr array is empty. Any idea why ? Thanks for reading, I can provide more information in needed.

I suppose it's just because you are trying to access the attr array outside the scope where it exists

Related

Symfony 4: How to edit a given form control?

I can create a form view and even edit a submitted one.
But how can I do if I need for example to edit ONLY an user email.
Here's basically what I try:
{{ form_start(form) }}
{{ form_widget(form.id)}}
{{ form_widget(form._token) }}
{{ form_widget(form.email) }}
{{ form_widget(form.submit) }}
{{ form_end(form, {'render_rest': false}) }}
When I submit the form and dump the user after called $form->handleRequest($request), all the rest fields are reset to them default values except the ones I rendered in my twig view (id email).
So what the right way to achieve my goal?
I'm wondering what if I had differents FormType accordingly?
Dont know if this is the recommended way but it works!

Symfony 3 prevent full form rendering twig

I'm currently trying to only render specific parts of a form but for some reason, the whole form keeps getting rendered as you can see below I've used the form start and end and the whole form is still being rendered even though it should only render the start and end of the form tags.
{{ form_start(searchForm) }}
{{ form_end(searchForm) }}
Also whenever I use the code below all that happens is that the elements are being moved to the top of the form and the elements which shouldn't be rendered are rendered below.
{{ form_start(searchForm) }}
{{ form_widget(searchForm.title) }}
{{ form_widget(searchForm.title) }}
{{ form_end(searchForm) }}
As described in the doc, for don't render unrendered fields:
{{ form_end(form, {'render_rest': false}) }}
Hope this help

Save multiple instances of one entity with form submit in symfony2

Right now I'm rendering two forms ( one for company and one for it's tags ) and it looks like this:
<h3>Company</h3>
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.city) }}
{{ form_row(form.street) }}
{{ form_row(form.postalcode) }}
{{ form_row(form.buildingnumber) }}
{{ form_row(form.vatid) }}
{{ form_row(form.tags) }}
<button id="test">Test</button>
{{ form_row(tags_form.title) }}
{{ form_row(form.save) }}
{{ form_end(form) }}
I want users to be able to add another input ( after clicking test button ) {{ form_row(form.tags) }} so they can add multiple tags with one form, but right now my tags form looks like this:
$builder
->add('title',null,array(
'label' => 'tag.title',
'required' => false
));
and I don't really know how to set this up. I tried with the simplest solution:
$('#test').on('click',function(e) {
e.preventDefault();
$('#fourcreate_portalbundle_companytags_title').clone().appendTo('form');
});
but that way submitting form creates entity only from the second input.
EDIT: I forgot to add - it has to be done with two forms, because first form contains list of currently available tags and the second form is to let users add their own.
You should not use two forms, but have a collection of tags_form in form.tags. The sample in the Cookbook is about adding tags.
The browser sends a "clobbered" form where two or more name attribute of two or more input elements have the same value. Hence, the back-end gets only the last value. I can not be more specific because I am not familiar with that part of Symfony.
If you wish to clone input elements, and have their values submitted correctly, you must at least modify the name attributes before submitting the form. Also, watch out for non-unique id attributes, as that violates the HTML (DOM?) standard.
e.g
var clone = $('input[id="original"]').clone();
clone.attr('name', clone.attr('name') + '1');

How can I use custom forms fields like table for symfony form

In Symfony docs they say to use this
<div>
{{ form_label(form.task) }}
{{ form_errors(form.task) }}
{{ form_widget(form.task) }}
</div>
But this generates the label element.
But I want to have table <td> instead of <label>
And also for the input textbox, I want to mention the size of text box. Where can I do that?
You have to define your form theme.
Probably, this tutorial is what you are looking for

How to change the label appearing in symfony 2 form templates

I have this in template
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_widget(form) }}
The form is appearing as
fos_user_registration_form_username --input box
fos_user_registration_form_email
fos_user_registration_form_plainPassword_first
fos_user_registration_form_plainPassword_second
But it want to have simple labels like Username , Email etc. How to do that
You can parse the individual form fields instead of the form as a whole.
{{ form_widget(form.fos_user_registration_form_username) }}
In this way, you can parse a single form element. Make sure to end with
{{ form_rest(form) }}
to output any not yet parsed fields (such as the csrf protection token).
using the above approach you can add your own labels to the fields.
FOSUserBundle uses the translator system. You need to as seen in the docs here:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.2.0/Resources/doc/index.md
Add this:
# app/config/config.yml
framework:
translator: ~
To your config file (app/config/config.yml).
This will tell symfony to replace all the label values with the values found in the translator file (FOSUserBundle.en.yml). Then the form will always print "Username" instead of "fos_user_registration_form_username".

Categories