/
ChoiceType.php
154 lines (136 loc) · 5.59 KB
/
ChoiceType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Extension\Core\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\Extension\Core\EventListener\FixRadioInputListener;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Core\DataTransformer\ScalarToChoiceTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ScalarToBooleanChoicesTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ArrayToChoicesTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ArrayToBooleanChoicesTransformer;
class ChoiceType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{
if (!$options['choices'] && !$options['choice_list']) {
throw new FormException('Either the option "choices" or "choice_list" is required');
}
if ($options['choice_list'] && !$options['choice_list'] instanceof ChoiceListInterface) {
throw new FormException('The "choice_list" must be an instance of "Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface".');
}
if (!$options['choice_list']) {
$options['choice_list'] = new ArrayChoiceList($options['choices']);
}
if ($options['expanded']) {
// Load choices already if expanded
$options['choices'] = $options['choice_list']->getChoices();
foreach ($options['choices'] as $choice => $value) {
if ($options['multiple']) {
$builder->add((string) $choice, 'checkbox', array(
'value' => $choice,
'label' => $value,
// The user can check 0 or more checkboxes. If required
// is true, he is required to check all of them.
'required' => false,
));
} else {
$builder->add((string) $choice, 'radio', array(
'value' => $choice,
'label' => $value,
));
}
}
}
$builder
->setAttribute('choice_list', $options['choice_list'])
->setAttribute('preferred_choices', $options['preferred_choices'])
->setAttribute('multiple', $options['multiple'])
->setAttribute('expanded', $options['expanded'])
->setAttribute('required', $options['required'])
;
if ($options['expanded']) {
if ($options['multiple']) {
$builder->appendClientTransformer(new ArrayToBooleanChoicesTransformer($options['choice_list']));
} else {
$builder
->appendClientTransformer(new ScalarToBooleanChoicesTransformer($options['choice_list']))
->addEventSubscriber(new FixRadioInputListener(), 10)
;
}
} else {
if ($options['multiple']) {
$builder->appendClientTransformer(new ArrayToChoicesTransformer());
} else {
$builder->appendClientTransformer(new ScalarToChoiceTransformer());
}
}
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
{
$choices = $form->getAttribute('choice_list')->getChoices();
$preferred = array_flip($form->getAttribute('preferred_choices'));
$view
->set('multiple', $form->getAttribute('multiple'))
->set('expanded', $form->getAttribute('expanded'))
->set('preferred_choices', array_intersect_key($choices, $preferred))
->set('choices', array_diff_key($choices, $preferred))
->set('separator', '-------------------')
->set('empty_value', !$form->getAttribute('multiple') && !$form->getAttribute('required') ? '' : null)
;
if ($view->get('multiple') && !$view->get('expanded')) {
// Add "[]" to the name in case a select tag with multiple options is
// displayed. Otherwise only one of the selected options is sent in the
// POST request.
$view->set('full_name', $view->get('full_name').'[]');
}
}
/**
* {@inheritdoc}
*/
public function getDefaultOptions(array $options)
{
$multiple = isset($options['multiple']) && $options['multiple'];
$expanded = isset($options['expanded']) && $options['expanded'];
return array(
'multiple' => false,
'expanded' => false,
'choice_list' => null,
'choices' => array(),
'preferred_choices' => array(),
'empty_data' => $multiple || $expanded ? array() : '',
'error_bubbling' => false,
);
}
/**
* {@inheritdoc}
*/
public function getParent(array $options)
{
return $options['expanded'] ? 'form' : 'field';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'choice';
}
}