symfony2 formbuilder choice multiple throws no array - php

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!

Related

How do I style child elements of a drop down list in a Symfony form

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?

Symfony, Keep Get Argumments in Search form after submit

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.

Show hidden select field on change using script in cakephp

I wrote this script to show hidden label and whole select field on change in other select field CakePHP,
script function is showing label but not select field.
Here is the htmlhelper:
<div class="form-group">
<label for="job_category" class="col-sm-5 control-label">Tutor City</label>
<div class="col-sm-7">
<?php echo $this->Form->input('city_id',
array('class' => 'user_login form-control',
'placeholder' =>'TutorCity',
'label' => false,
'div' => false,
'id' => 'city'
));
?>
</div>
</div>
<div class="form-group">
<label for="job_category" class="col-sm-5 control-label" id="area_label" hidden>Tutor Access Areas</label>
<div class="col-sm-7">
<?php echo $this->Form->input('area_id',
array('class' => 'user_login form-control',
'placeholder' => 'TutorAreas',
'id' => 'area',
'div' => false,
'label' => false,
'multiple' => true,
'type' => 'hidden'
));
?>
</div>
</div>
Script:
<script type="text/javascript">
$("#city").change(function () {
$("#area_label").show();
$("#area").show();
})
</script>
Any help is appreciated. Thanks.
The second input is hidden input <input type="hidden"> so to generate select field remove type => hidden
<?php echo $this->Form->input('area_id',
array('class' => 'user_login form-control',
'placeholder' => 'TutorAreas',
'id' => 'area',
'div' => false,
'label' => false,
'multiple' => true,
'type' => 'hidden' // remove this
));
?>
You need to remove the type hidden first. You can try using type text with display none. You can change the input field display block on change.

Customize CSS in collection type symfony

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();
});

Translation From Entity Does Not Work [Symfony2]

I just update Symfony2 from 2.4 to 2.7. Besides a lot of deprecated calls, I have found one weird problem.
I have a "select" tag within a form and I get the options from database. In database are stored the translation keys, and always has worked fine, symfony showed the correct language in the form. But not now.
Some code and screenshots:
Form:
->add('category', 'entity', array(
'empty_value' => 'Event.form.label.category_empty',
'class' => 'EventBundle:Category',
'choice_label' => 'name',
'error_bubbling' => true,
'constraints' => array(
new NotBlank(array('message' => 'Event.form.error.category.notblank'))
)
))
->add('subcategory', 'entity', array(
'class' => 'EventBundle:Subcategory',
'choice_label' => 'name',
'error_bubbling' => true,
'constraints' => array(
new NotBlank(array('message' => 'Event.form.error.subcategory.notblank'))
)
))
Template:
<div class="form-group col-sm-6 input-group">
<label for="event_category" class="input-group-addon">{{ 'Event.form.label.category' | trans }}</label>
{{ form_widget(form.category, { 'attr': { 'data-ott-subcaturl' : path('get_subcategories') , 'class' : 'form-control ev-category'} }) }}
</div>
<div class="form-group col-sm-6 input-group">
<label for="event_subcategory" class="input-group-addon">{{ 'Event.form.label.subcategory' | trans }}</label>
{{ form_widget(form.subcategory, { 'attr': { 'class' : 'form-control ev-subcategory'} }) }}
</div>
Form result with Symfony2.4:
Form result with Symfony2.7:
As you see, the empty_value key is translated in both cases. And the option values keys does not appear in debugger, as if there were. I think is because translations load before doctrine, but I don't know how fix it.
Thanks.
I found the solution:
In 2.7 was introduced the choice_translation_domain to avoid translating options.
http://symfony.com/blog/new-in-symfony-2-7-form-and-validator-updates#added-choice-translation-domain-domain-to-avoid-translating-options for more details
->add('category', 'entity', array(
'choice_translation_domain' => true,
));

Categories