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

  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\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.     public function configureOptions(OptionsResolver $resolver)
  29.     {
  30.         $resolver->setDefaults([
  31.             'compound' => false,
  32.         ]);
  33.     }
  34.     public function getBlockPrefix(): string
  35.     {
  36.         return 'text';
  37.     }
  38.     public function transform(mixed $data): mixed
  39.     {
  40.         // Model data should not be transformed
  41.         return $data;
  42.     }
  43.     public function reverseTransform(mixed $data): mixed
  44.     {
  45.         return $data ?? '';
  46.     }
  47. }