I'm using a collectionType that render a multiple select inputs, I want to add the select2 css class to my form but it just doesn't works.
This is the Form that has the collection.
->add('arrayDiagnosticos', 'collection', [
'type' => 'entity',
'label' => 'Diagnósticos dinámicos',
'allow_add' => true,
'allow_delete' => true,
'attr' => [
'class' => 'select2',
],
'options' => [
'empty_value' => 'Seleccionar Diagnóstico',
'class' => 'AppBundle:Diagnostico',
'required' => true,
'label' => 'Buscador de Diagnósticos',
'attr' => [
'class' => 'select2',
],
],
])
The twig is
<ul class="tags" data-prototype="{{ form_widget(form.arrayDiagnosticos.vars.prototype)|e }}">
{# iterate over each existing tag and render its only field: name #}
{% for diagnostico in form.arrayDiagnosticos %}
<li>
{{ form_row(diagnostico.nombreDiagnostico) }}
</li>
{% endfor %}
</ul>
It should render a select input like this:
1
But it renders like a regular select input
2
This is the output html
<div class="form-group"><label class="control-label required">Diagnósticos dinámicos</label>
<div id="paciente_form_arrayDiagnosticos" class="select2" data-prototype="<div class="row"><div class="col-xs-9"><select id="paciente_form_arrayDiagnosticos___name__" name="paciente_form[arrayDiagnosticos][__name__]" required="required" class="select2 form-control"><option value="" selected="selected">Seleccionar Diagnóstico</option> <option value="1" >se siente mal</option> <option value="2" >asfd</option> <option value="3" >YOLO</option></select></div><div class="col-xs-3"><a href="#" class="btn btn-danger btn-sm" data-removefield="collection" data-field="__id__">Eliminar Diagnóstico</a></div></div>" data-prototype-name="__name__"><ul class="bc-collection list-unstyled"></ul>Agregar Diagnóstico</div></div>
I also tried to do it via jQuery and no luck
<script>
$(function(){
$('#paciente_form_arrayDiagnosticos').addClass('select2')});
</script>
How do I properly attach the select2 css class?
Try this:
->add('arrayDiagnosticos', 'collection', [
'type' => new PreDiagnosticoType(),
'label' => ' ',
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'attr' => array(
'class' => 'select2',
),
])
Since your <selects /> are created dinamically, you have to set up a listener to apply the select2 function when each select element is created.
Somthing like this might work:
$('body').on('DOMNodeInserted', '.select2', function() {
$(this).select2();
});
Related
I'm trying to style the dropdown options (label and checkbox) of a symfony form but am running into problems. I can style the rendered group of checkboxes and labels, but not each item (paired label + checkbox) individually.
I've attempted to style them by:
{{ form_widget(form.qualifications, {'attr': {'class':'d-block'} }) }}
But as detailed in the documentation, this only applies the styles to the parent element (the rendered group of options to select), not each individual option.
Here's the symfony form builder part which creates the widget
->add('qualifications', EntityType::class, [
'class' => Tag::class,
'multiple' => true,
'expanded' => true,
'required' => false,
'placeholder' => 'Select...',
'choices' => $this->tagsService->getTagsQualificationLevels(),
'attr' => [
'class' => 'form-control-ajax-submit-on-change w-20',
]
])
I expect to be able to apply styles to the choices array, but am really stuck on how to do so.
You can use the choice_attr option:
->add('qualifications', EntityType::class, [
'class' => Tag::class,
'multiple' => true,
'expanded' => true,
'required' => false,
'placeholder' => 'Select...',
'choices' => $this->tagsService->getTagsQualificationLevels(),
'attr' => [
'class' => 'form-control-ajax-submit-on-change w-20',
],
'choice_attr' => function($choiceValue, $key, $value) {
return ['class' => 'my_custom_choice_class'];
},
])
May be you should try for loop in template?
I had similar problems with radiobutton, take a look: How to make RadioButton in Symfony Form?
Hi,
i have created a search form using GET method, to my indexAction, the form contains virtual fields.
i created the logic for searching inside the indexAction.
My problem is when i submit the form, all inputs values become empty.
What i want is to keep the values passed as arguments in form inputs
here is my FormType :
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('title', TextType::class, array(
'required' => false,
'attr' => array(
'placeholder' => 'Chercher',
)
))
->add('client', EntityType::class, array(
'required' => false,
'class' => 'AppBundle:Person',
'placeholder' => 'Choisir client',
'attr' => array(
'class' => 'select2',
)
))
->add('date_start', DateTimeType::class, array(
'required' => false,
'input' => 'datetime',
'widget' => 'single_text',
'format' => 'dd-MM-yyyy',
'inherit_data' => true,
'attr' => array(
'name' => 'date_start',
'class' => 'datepicker',
'placeholder' => 'Date début',
'value' => ''
)
))
->add('date_end', DateTimeType::class, array(
'required' => false,
'input' => 'datetime',
'widget' => 'single_text',
'format' => 'dd-MM-yyyy',
'inherit_data' => true,
'attr' => array(
'name' => 'date_end',
'class' => 'datepicker',
'placeholder' => 'Date fin',
'value' => ''
)
))
;
}
The indexAction :
public function indexAction(Request $request)
{
$getArgs = $request->query->get('order');
dump($getArgs);
$form = $this->createForm('AppBundle\Form\Search\OrderType', new Order(), array('get' => $getArgs));
$form->handleRequest($request);
$repo = $this->getDoctrine()->getRepository('AppBundle:Order');
$query = $getArgs ? $repo->findAllMatched($q) : $repo->createQueryBuilder('o');
$paginator = $this->get('knp_paginator');
$orders = $paginator->paginate($query);
return $this->render('AppBundle:Order:index.html.twig', array(
'orders' => $orders,
'form' => $form->createView(),
));
}
Any suggestions are wellcome.
For now, i resolved the problem in this way :
<div class="nav box-search">
<form method="GET" class="form-inline">
<div class="pull-right">
<button type="submit" class="btn btn-default">{{ 'actions.search' | trans }}</button>
</div>
<div class="pull-left">
{% set params = app.request.query.all %}
{{- form_widget(form.title, { 'attr': {'value': params.order.title| default('')} }) -}}
{{ form_widget(form.client, {value: params.order.client| default('') }) }}
{{- form_widget(form.date_start, { 'attr': {'value': params.order.date_start| default('')} }) -}}
<i class="fa fa-exchange"></i>
{{- form_row(form.date_end, { 'attr': {'value': params.order.date_end| default('')} }) -}}
</div>
</form>
</div>
It's not that bad solution, but what i would is to have this parameters configuration in the FormType.
Issue: Multiple dropdown just hands over a string not an array.
I tried to use a multiple dropdown in the formbuilder:
->add('options', 'choice', array(
'choices' => $printerOptionsDropdown,
'empty_value' => 'Optionen wählen',
'label' => 'Optionen',
'attr' => array(
'class' => 'form-control selectpicker',
'data-live-search' => true,
'multiple' => true),
'required' => false
))
With this twig template:
<form action="{{ path('<form>_create', { 'id' : entity.id }) }}" name="<formForm>" id="<formForm>" method="POST" class="form-horizontal" role="form" >
<div class="form-group">
<label for="<formbuildertag>_options" class="col-sm-2 control-label">{{ form_label(form.options) }}</label>
<div class="col-sm-4">
{{ form_widget(form.options) }}{{ form_errors(form.options) }}
</div>
</div>
And everything looks fine. I can select multiple options.
But when I submit the form it only hands over a string not an array.
<formbuildertag>[options]:"Value1"
<formbuildertag>[options]:"Value2"
The output of the post request is just a string of Value2. It gets overwritten because it's not an array. I got that. But why does the formbuilder not even create an array for the form.
I already tried to overwrite the full_name
form_widget(form.options, `enter code here`'full_name' => '<formbuldertag>[options][]')
but it didn't work.
Any ideas?
You must have multiple option defined as true. You have it in attr. change it as below :
->add('options', 'choice', array(
'choices' => $printerOptionsDropdown,
'empty_value' => 'Optionen wählen',
'label' => 'Optionen',
'attr' => array(
'class' => 'form-control selectpicker',
'data-live-search' => true,
'required' => false,
'multiple' => true
))
Hope this helps!
My form in php:
class MyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('configFiles', CollectionType::class, array(
'entry_type' => TextareaType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true
))
}
...
}
My form in twig:
...
<div class="form-group">
{{ form_label(create_lab_form.configFiles) }}
<div class="configFiles" data-prototype="{{ form_widget(create_lab_form.configFiles.vars.prototype)|e('html_attr') }}">
</div>
{{ form_errors(create_lab_form.configFiles) }}
</div>
{% do create_lab_form.configFiles.setRendered %}
</div>
...
How to add attribute (e.g. "rows") for Textarea collection element in example above?
I tried something like this but it does not work:
->add('configFiles', CollectionType::class, array(
'entry_type' => TextareaType::class, array(
'attr' => array('rows' => 10
)),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true
))
Thank you in advance!
Everything is right but you are using the wrong key:
'entry_type' => TextareaType::class,
'entry_options' => array(
'attr' => array(
'rows' => 10
)),
should do the yob
I have a form with 3 entity fields displaying radios input.
->add(
'membership',
'entity',
array(
'class' => 'Comiti\UserBundle\Entity\Membership',
'expanded' => true,
'multiple' => false,
'label' => false,
'empty_value' => 'Aucune adhésion',
'query_builder' => function (MembershipRepository $er) {
return $er->createQueryBuilder('membership')
->where('membership.club = :club')
->setParameter('club', $this->authentication_service->getCurrentClub())
->orderBy('membership.name', 'ASC')
;
},
)
)->add(
'federal_license',
'entity',
array(
'class' => 'Comiti\UserBundle\Entity\FederalLicense',
'expanded' => true,
'multiple' => false,
'label' => false,
'empty_value' => 'Aucune licence',
'query_builder' => function (FederalLicenseRepository $er) {
return $er->createQueryBuilder('federal_license')
->where('federal_license.club = :club')
->setParameter('club', $this->authentication_service->getCurrentClub())
->orderBy('federal_license.name', 'ASC')
;
}
)
)->add(
'insurance',
'entity',
array(
'class' => 'Comiti\UserBundle\Entity\Insurance',
'expanded' => true,
'multiple' => false,
'label' => false,
'empty_value' => 'Aucune assurance',
'query_builder' => function (InsuranceRepository $er) {
return $er->createQueryBuilder('insurance')
->where('insurance.club = :club')
->setParameter('club', $this->authentication_service->getCurrentClub())
->orderBy('insurance.name', 'ASC')
;
}
)
);
I need to define a custom template for those radios input that put in each input an attr whith "data-price".
i made this:
{%- block radio_widget -%}
<input type="radio" data-price="{{Myprivcevar}}" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
Is there any possibility to access to {{MypriceVar }}
I am on Symfony 2.6
You can add attributes to an input without creating a custom template:
{{ form_widget(yourRow, {'attr': {'data-price':'yourValue'}}) }}
Example:
{{ form_widget(choiceFormView, {'attr': {'data-price':'2'}}) }}
result in
<input type="radio" id="form_choice_0" name="form[choice]" required="required" data-price="2" value="1" checked="checked">