vendor/symfony/form/Extension/Core/Type/CheckboxType.php line 22

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\Extension\Core\Type;
  11. use Symfony\Component\Form\AbstractType;
  12. use Symfony\Component\Form\Extension\Core\DataTransformer\BooleanToStringTransformer;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\Form\FormView;
  16. use Symfony\Component\OptionsResolver\Options;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. class CheckboxType extends AbstractType
  19. {
  20.     /**
  21.      * {@inheritdoc}
  22.      */
  23.     public function buildForm(FormBuilderInterface $builder, array $options)
  24.     {
  25.         // Unlike in other types, where the data is NULL by default, it
  26.         // needs to be a Boolean here. setData(null) is not acceptable
  27.         // for checkboxes and radio buttons (unless a custom model
  28.         // transformer handles this case).
  29.         // We cannot solve this case via overriding the "data" option, because
  30.         // doing so also calls setDataLocked(true).
  31.         $builder->setData($options['data'] ?? false);
  32.         $builder->addViewTransformer(new BooleanToStringTransformer($options['value'], $options['false_values']));
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function buildView(FormView $viewFormInterface $form, array $options)
  38.     {
  39.         $view->vars array_replace($view->vars, [
  40.             'value' => $options['value'],
  41.             'checked' => null !== $form->getViewData(),
  42.         ]);
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function configureOptions(OptionsResolver $resolver)
  48.     {
  49.         $emptyData = function (FormInterface $form$viewData) {
  50.             return $viewData;
  51.         };
  52.         $resolver->setDefaults([
  53.             'value' => '1',
  54.             'empty_data' => $emptyData,
  55.             'compound' => false,
  56.             'false_values' => [null],
  57.             'invalid_message' => function (Options $options$previousValue) {
  58.                 return ($options['legacy_error_messages'] ?? true)
  59.                     ? $previousValue
  60.                     'The checkbox has an invalid value.';
  61.             },
  62.             'is_empty_callback' => static function ($modelData): bool {
  63.                 return false === $modelData;
  64.             },
  65.         ]);
  66.         $resolver->setAllowedTypes('false_values''array');
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function getBlockPrefix()
  72.     {
  73.         return 'checkbox';
  74.     }
  75. }