Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aacafe8

Browse files
committed
[Form] Add option widget to ChoiceType
1 parent 86e77eb commit aacafe8

File tree

4 files changed

+225
-69
lines changed

4 files changed

+225
-69
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@
3535
{%- endblock textarea_widget -%}
3636

3737
{%- block choice_widget -%}
38-
{% if expanded %}
38+
{% if 'expanded' == widget %}
3939
{{- block('choice_widget_expanded') -}}
40-
{% else %}
40+
{% elseif 'select' == widget %}
4141
{{- block('choice_widget_collapsed') -}}
42+
{% elseif 'hidden' == widget %}
43+
{{- block('hidden_widget') -}}
44+
{% else %}
45+
{{- block('form_widget_simple') -}}
4246
{% endif %}
4347
{%- endblock choice_widget -%}
4448

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
13+
14+
use Symfony\Component\Form\DataTransformerInterface;
15+
use Symfony\Component\Form\Exception\UnexpectedTypeException;
16+
17+
/**
18+
* Converts an array of values to a string with multiple values separated by a delimiter.
19+
*
20+
* @author Bilal Amarni <[email protected]>
21+
*/
22+
class ValuesToStringTransformer implements DataTransformerInterface
23+
{
24+
private $delimiter;
25+
private $trim;
26+
27+
public function __construct($delimiter, $trim)
28+
{
29+
$this->delimiter = $delimiter;
30+
$this->trim = $trim;
31+
}
32+
33+
/**
34+
* @param array $array
35+
*
36+
* @return string
37+
*
38+
* @throws UnexpectedTypeException if the given value is not an array
39+
*/
40+
public function transform($array)
41+
{
42+
if (null === $array) {
43+
return array();
44+
}
45+
46+
if (!is_array($array)) {
47+
throw new UnexpectedTypeException($array, 'array');
48+
}
49+
50+
return implode($this->delimiter, $array);
51+
}
52+
53+
/**
54+
* @param string $string
55+
*
56+
* @return array
57+
*
58+
* @throws UnexpectedTypeException if the given value is not a string
59+
*/
60+
public function reverseTransform($string)
61+
{
62+
if (empty($string)) {
63+
return array();
64+
}
65+
66+
if (!is_string($string)) {
67+
throw new UnexpectedTypeException($string, 'string');
68+
}
69+
70+
$values = explode($this->delimiter, $string);
71+
72+
if ($this->trim) {
73+
$values = array_map('trim', $values);
74+
}
75+
76+
return $values;
77+
}
78+
}

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
3333
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer;
3434
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer;
35+
use Symfony\Component\Form\Extension\Core\DataTransformer\ValuesToStringTransformer;
3536
use Symfony\Component\OptionsResolver\Options;
3637
use Symfony\Component\OptionsResolver\OptionsResolver;
3738

@@ -54,7 +55,7 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
5455
*/
5556
public function buildForm(FormBuilderInterface $builder, array $options)
5657
{
57-
if ($options['expanded']) {
58+
if ('expanded' == $options['widget']) {
5859
$builder->setDataMapper($options['multiple']
5960
? new CheckboxListMapper($options['choice_list'])
6061
: new RadioListMapper($options['choice_list']));
@@ -140,10 +141,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
140141
});
141142
}
142143
} elseif ($options['multiple']) {
143-
// <select> tag with "multiple" option
144+
// "select", "text" or "hidden" widget with "multiple" option
144145
$builder->addViewTransformer(new ChoicesToValuesTransformer($options['choice_list']));
146+
147+
// for "text" / "hidden" widget, view data uses a delimiter
148+
if (in_array($options['widget'], array('text', 'hidden'))) {
149+
$builder->addViewTransformer(new ValuesToStringTransformer($options['delimiter'], $options['trim']));
150+
}
145151
} else {
146-
// <select> tag without "multiple" option
152+
// "select", "text" or "hidden" tag without "multiple" option
147153
$builder->addViewTransformer(new ChoiceToValueTransformer($options['choice_list']));
148154
}
149155

@@ -170,8 +176,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
170176
: $this->createChoiceListView($options['choice_list'], $options);
171177

172178
$view->vars = array_replace($view->vars, array(
179+
'widget' => $options['widget'],
173180
'multiple' => $options['multiple'],
174-
'expanded' => $options['expanded'],
181+
'expanded' => $options['expanded'], // BC
175182
'preferred_choices' => $choiceListView->preferredChoices,
176183
'choices' => $choiceListView->choices,
177184
'separator' => '-------------------',
@@ -205,7 +212,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
205212
$view->vars['empty_value'] = $view->vars['placeholder'];
206213
$view->vars['empty_value_in_choices'] = $view->vars['placeholder_in_choices'];
207214

208-
if ($options['multiple'] && !$options['expanded']) {
215+
if ('select' == $options['widget'] && $options['multiple']) {
209216
// Add "[]" to the name in case a select tag with multiple options is
210217
// displayed. Otherwise only one of the selected options is sent in the
211218
// POST request.
@@ -218,7 +225,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
218225
*/
219226
public function finishView(FormView $view, FormInterface $form, array $options)
220227
{
221-
if ($options['expanded']) {
228+
if ('expanded' == $options['widget']) {
222229
// Radio buttons should have the same name as the parent
223230
$childName = $view->vars['full_name'];
224231

@@ -240,6 +247,10 @@ public function configureOptions(OptionsResolver $resolver)
240247
{
241248
$choiceListFactory = $this->choiceListFactory;
242249

250+
$widget = function (Options $options) {
251+
return $options['expanded'] ? 'expanded' : 'select';
252+
};
253+
243254
$emptyData = function (Options $options) {
244255
if ($options['multiple'] || $options['expanded']) {
245256
return array();
@@ -303,7 +314,7 @@ public function configureOptions(OptionsResolver $resolver)
303314
};
304315

305316
$compound = function (Options $options) {
306-
return $options['expanded'];
317+
return 'expanded' == $options['widget'];
307318
};
308319

309320
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
@@ -315,8 +326,10 @@ public function configureOptions(OptionsResolver $resolver)
315326
};
316327

317328
$resolver->setDefaults(array(
329+
'widget' => $widget,
318330
'multiple' => false,
319-
'expanded' => false,
331+
'delimiter' => ',',
332+
'expanded' => false, // deprecated
320333
'choice_list' => null, // deprecated
321334
'choices' => array(),
322335
'choices_as_values' => false,

0 commit comments

Comments
 (0)