]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
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\Type; | |
13 | ||
14 | use Symfony\Component\Form\AbstractType; | |
15 | use Symfony\Component\Form\FormBuilderInterface; | |
16 | use Symfony\Component\Form\FormInterface; | |
17 | use Symfony\Component\Form\Extension\Core\DataTransformer\BooleanToStringTransformer; | |
18 | use Symfony\Component\Form\FormView; | |
19 | use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
20 | ||
21 | class CheckboxType extends AbstractType | |
22 | { | |
23 | /** | |
24 | * {@inheritdoc} | |
25 | */ | |
26 | public function buildForm(FormBuilderInterface $builder, array $options) | |
27 | { | |
28 | $builder | |
29 | ->addViewTransformer(new BooleanToStringTransformer($options['value'])) | |
30 | ; | |
31 | } | |
32 | ||
33 | /** | |
34 | * {@inheritdoc} | |
35 | */ | |
36 | public function buildView(FormView $view, FormInterface $form, array $options) | |
37 | { | |
38 | $view->vars = array_replace($view->vars, array( | |
39 | 'value' => $options['value'], | |
40 | 'checked' => null !== $form->getViewData(), | |
41 | )); | |
42 | } | |
43 | ||
44 | /** | |
45 | * {@inheritdoc} | |
46 | */ | |
47 | public function setDefaultOptions(OptionsResolverInterface $resolver) | |
48 | { | |
49 | $emptyData = function (FormInterface $form, $clientData) { | |
50 | return $clientData; | |
51 | }; | |
52 | ||
53 | $resolver->setDefaults(array( | |
54 | 'value' => '1', | |
55 | 'empty_data' => $emptyData, | |
56 | 'compound' => false, | |
57 | )); | |
58 | } | |
59 | ||
60 | /** | |
61 | * {@inheritdoc} | |
62 | */ | |
63 | public function getName() | |
64 | { | |
65 | return 'checkbox'; | |
66 | } | |
67 | } |