prototype field is empty after form(form) but set when set before the form(form) symfony - forms

when I follow this tutorial: http://symfony.com/doc/current/cookbook/form/form_collections.html
and I render the
<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
...
</ul>
the prototype stay empty if I put it after the {{ form(form)}} but gets filled If I put it i before the {{ form(form) }} tag. any one an idea why this is and how to solve it.
thanks

The tag {{ form(form) }} is supposed to output all your form, so there is nothing to output after this tag.
If the tag {{ form(form) }} does not output the prototype, then it was not configured right in the form type class.
But if you chose to output prototype by using form_widget, you should not use form(form) and should output the form by parts:
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_row(form.another_form_property) }}
</div>
<div>
<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
...
</ul>
</div>
{{ form_end(form) }}

Related

easyadmin action buttons in custom template

I'm creating my own template for the edit page the thing is the actions buttons don't show so I tried to add them in configureActions function:
public function configureActions(Actions $actions): Actions
{
return parent::configureActions($actions)->add(Crud::PAGE_EDIT, Action::SAVE_AND_CONTINUE);
}
but i get this error
The "saveAndContinue" action already exists in the "edit" page, so you can't add it again. Instead, you can use the "updateAction()" method to update any options of an existing action+
This is my edit twig:
{% extends '#!EasyAdmin/layout.html.twig' %}<div class="tab-pane fade show active" id="client">
{% block edit_form %}
<div class="form-group">
{{ form_label(edit_form.name) }}
{{ form_widget(edit_form.name, {'attr': {'class': 'form-control' }}) }}
</div>
<div class="form-group">
{{ form_label(edit_form.surname) }}
{{ form_widget(edit_form.surname, {'attr': {'class': 'form-control' }}) }}
</div>{% endblock %}

Symfony 6 - Access CollectionType fields in Twig Template

I have a Symfony 6 project, where I us a CollectionType in a Form.
This CollectionType is called "variants" and has three fields. Lets call them:
Field_1
Field_2
Field_3
If I render this CollectionType in my Twig template with {{form_row(form.variants)}} all three fields of the CollectionType are rendered underneith.
However, I would like to to separate each of the fields of the CollectionType in a own column next to each other. But how dow I access the fields?
I have tried to access the fields via {{form_row(form.variants.Field_1)}} but obviously that did not work.
I am happy about any ideas :)
you were close.
CollectionType is an array - as it can hold multiple sets of the type.
If you only have one set try
{{ form_row(form.variants[0].Field_1) }}
{{ form_row(form.variants[0].Field_2) }}
{{ form_row(form.variants[0].Field_3) }}
The for method:
{% for variant in form.variants %}
{{ form_row(variant.Field_1) }}
{{ form_row(variant.Field_2) }}
{{ form_row(variant.Field_3) }}
{% endfor %}
Thanks to Rufinus again.
I used that information and the remark from vinceAmstoutz and did the creation in a foor loop:
{% for variant in form.variants %}
<div class="row">
<div class="col">
{{ form_row(variant.Field_1) }}
</div>
<div class="col">
{{ form_row(variant.Field_2) }}
</div>
<div class="col">
{{ form_row(variant.Field_3) }}
</div>
</div>
{% endfor %}

symfony2 and twig: get properties of form field

I am trying to create a form with sub-fields with symfony2.
In twig I render the form as
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_label(form) }}
{{ form_errors(form) }}
{% for field in form %}
{{ form_widget(field) }}
{% endfor %}
</div>
{{ form_end(form) }}
However, I want to add some customization depending on the field I am rendering.
What I want to achieve is something like this:
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_label(form) }}
{{ form_errors(form) }}
{% for field in form %}
{% if field.label == "myvalue" %} <-- this code is not working
{# do something here #}
{{ form_widget(field) }}
{% endif %}
{% endfor %}
</div>
{{ form_end(form) }}
I am not able to access the label of each of my sub-fields in twig.
I think it is possible with something like
{{ field.vars.something }}
, but I did not manage to find any clear documentation about this.
Can someone please help?
Thank you!
Edit:
I actually found the answer to my question:
It was indeed just
{{ field.vars.label }}
and
{% if field.vars.label == "myvalue" %}
{# do something here #}
{{ form_widget(field) }}
{% endif %}
did the trick.
However, I am still looking for some good documentation about this "vars" attribute in twig, and what can be retrieved with it.
Thanks!
You'll find more information at http://symfony.com/doc/current/reference/forms/twig_reference.html#more-about-form-variables
On that page you'll find a list of common form vars. You might also create custom vars by implementing the buildView method of a FormType. You can read an example at http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html#adding-the-extension-business-logic
Hope it'll help

multiple form in symfony2

I have a form for adding comments, and i want to know how can i multiply this form on the same page.
I tried this code but it does not work :
{% for statut in statuts %}
<div>
{{ statut.sujet }}
<form action="" method="post" {{ form_enctype(formC) }}>
{{ form_errors(formC) }}
{{ form_errors(formC.commentaire) }}
{{ form_widget(formC.commentaire) }}
<div><input type="submit" class="btn btn-info"/><div>
{{ form_rest(formC) }}
</form>
</div>
{% endfor %}
Does somebody know how to do that ? or any suggestions
You need to do this in the controller like get all statuts then run a for loop for each statuts and get comment form for each statut field store them in array and return the array.
In your twig template then run a for loop for the same.

FOSUserBundle issues with plainPassword on twig form

I am defining my own twig layout for new user registration and I have everything laid out the way I want it with the exception of the plainPassword field from the FOSUserBundle.
<p class="left">
{{ form_widget(form.plainPassword) }}
</p>
<div class="clearfix"></div>
The code above displays both the password and verification block. I would like to break this up into the 4 elements of form.plainPassword.label, form.plainPassword.field, form.plainPassword2.label, and form.plainPassword2.field. I cannot figure out what to put in the form_label() and form_widget() calls.
<p class="left">
{{ form_label( ??? ) }}
{{ form_widget( ??? ) }}
</p>
<p class="left">
{{ form_label( ??? ) }}
{{ form_widget( ??? ) }}
</p>
<div class="clearfix"></div>
I am assuming this can be done.
I had the same problem. My solution (seems to be official :) :
{{ form_label (form.plainPassword.first) }}
{{ form_widget (form.plainPassword.first) }}
{{ form_label (form.plainPassword.second) }}
{{ form_widget (form.plainPassword.second) }}
Hope it can helps !
This blog post shows how to output a repeated field in twig.
http://blogsh.de/2011/10/19/how-to-use-the-repeated-field-type-in-symfony/
But in short this worked for me:
{{ form_label (form.plainPassword.children['New Password']) }}
{{ form_widget (form.plainPassword.children['New Password']) }}
{{ form_label (form.plainPassword.children['Confirm Password']) }}
{{ form_widget (form.plainPassword.children['Confirm Password']) }}
I have to say I'm sure using .children isnt the best/official way of doing it, but it works!