vendor/symfony/form/Extension/Core/Type/TextType.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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\DataTransformerInterface;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. class TextType extends AbstractType implements DataTransformerInterface
  16. {
  17.     public function buildForm(FormBuilderInterface $builder, array $options)
  18.     {
  19.         // When empty_data is explicitly set to an empty string,
  20.         // a string should always be returned when NULL is submitted
  21.         // This gives more control and thus helps preventing some issues
  22.         // with PHP 7 which allows type hinting strings in functions
  23.         // See https://github.com/symfony/symfony/issues/5906#issuecomment-203189375
  24.         if ('' === $options['empty_data']) {
  25.             $builder->addViewTransformer($this);
  26.         }
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function configureOptions(OptionsResolver $resolver)
  32.     {
  33.         $resolver->setDefaults([
  34.             'compound' => false,
  35.         ]);
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function getBlockPrefix()
  41.     {
  42.         return 'text';
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function transform($data)
  48.     {
  49.         // Model data should not be transformed
  50.         return $data;
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function reverseTransform($data)
  56.     {
  57.         return $data ?? '';
  58.     }
  59. }