I have this form element I want to render without a label, but I can't find the way...
$builder
->add('gender', 'choice', array(
'expanded' => true,
'choices' => array(
'Male' => 'm',
'Female' => 'f',
)
))
;
some help please, this isn't working:
{% block choice_widget %}
{% spaceless %}
{% for child in form %}
<input type="radio" value="{{ child.get('value') }}">
{% endfor %}
{% endspaceless %}
{% endblock choice_widget %}
I'm getting Array to string conversion
What I wanna do is an image based gender selector, just clicking an image to make the selection.
When using the form component, do not ever render form fields yourself, always rely on the form_ helpers as described in the form documentation.
In your case, this should work:
{{ form_label(form.gender) }}
{{ form_errors(form.gender) }}
{% for choiceFormView in form.gender %}
{{ form_widget(choiceFormView) }}
{% endfor %}
Related
I'd like access to the instance of the entity when using an EntityType::class. I have a form like below:
->add('fooBars', MyNewType::class, [
'class' => FooBar::class,
'choices' => $fooBars,
'label' => 'Foo Bar',
'multiple' => true,
'expanded' => true,
'required' => false,
'by_reference' => false,
]);
I've created a new type which has the parent of entity type, I then have a custom template as well. Having followed this I have my entity type displaying but where I'm looping over the children, I don't know how to access the entity.
{% block foo_bar_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form if not child.rendered %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}
I'd like to call a method on the entity it's looping over but I'm unsure how to access it. I can't dump child as it produces too much.
This can be achieved using the following:
{% set entity = form.vars.choices[child.vars.value].data %}
So the complete block may look like:
{% block foo_bar_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form if not child.rendered %}
{% set entity = form.vars.choices[child.vars.value].data %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}
And now you can call entity methods like so:
{{ entity.name }}}
I have this code to render the value and label of fields in a form:
{% for field in filter %}
{{ field.vars.data }}
{{ field.vars.label }}
{% endfor %}
but it is not rendering the name of the labels unless I set them this way:
->add('name', 'autocomplete_q', array('label' => 'A_LABEL_NAME', 'required' => false,'class' => 'ExchangeAdminBundle:Compound'))
so is there any way render just the label name without setting it explictly?
In your template, you can add this kind of information:
{{ form_label(form.name, 'Your Name', {'label_attr': {'class': 'foo'}}) }}
How can I access a field in collection in twig
$builder
->add('name', 'text', array('required' => false))
->add('email', 'collection', array(
'type' => new UsersEmailAddressesType(),
'allow_add' => true
))
UserEmailAddressesType has two fields name and email, how can I access email field in twig ?
In the symfony cookbook there is an example on how to embed collections in forms. The solution there looks like this (adapted to your form example):
<ul>
{% for email in form.email %}
<li>{{ form_row(email.address) }}</li>
{% endfor %}
</ul>
Since you want to place the inputs next to each other you might want to check whether the loop.index is odd, even or divisibleby() for example like this:
{% for email in form.email %}
{% if loop.index is odd %}
<li class="float-left">
{% else %}
<li class="float-right">
{% endif %}
{{ form_row(email.address) }}</li>
{% endfor %}
It seems that Symfony2 Form component does not handle this common case. Below is what I want in my html
The code looks like :
->add('code', 'choice', array(
'choices' => array(
'Food' => array('pizza', 'burger', 'icecream'),
'Music' => array('poney', 'little', 'pocket'),
),
'multiple' => true,
'expanded' => true,
'required' => true
));
Which gives in reality the wrong output :
It's wierd because the case with expanded => false is correctly handled
How to handle that case please ?
Ok so here's the form_theme solution for this
{% block _user_code_widget %}
<div {{ block('widget_container_attributes') }}>
{% for name, choices in form.vars.choices %}
<ul>
<li class="checkbox_category">
<input type="checkbox" class="checkallsub" /> {{ name }}
</li>
{% for key,choice in choices %}
<li class="indent">
{{ form_widget(form[key]) }}
{{ form_label(form[key]) }}
</li>
{% endfor %}
</ul>
{% endfor %}
</div>
{% endblock %}
Of course the extra js layer is missing, but you get the idea.
I have a Entity Field Type in a form, how do I check in Twig if its returning any value?
The entity field type:
$builder->add('users', 'entity', array(
'class' => 'UserBundle:User',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
},
));
The correct variable to check was: form.users.vars.choices
So the code would be:
{% if form.users.vars.choices|length > 0 %}
{# actions #}
{% endif %}
Try this:
{% if users.options|length > 0 %}
{% for option in users.options %}
{# some action #}
{% endfor %}
{% endif %}