vendor/symfony/form/Extension/Core/Type/UrlType.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\EventListener\FixUrlProtocolListener;
  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 UrlType extends AbstractType
  19. {
  20.     /**
  21.      * {@inheritdoc}
  22.      */
  23.     public function buildForm(FormBuilderInterface $builder, array $options)
  24.     {
  25.         if (null !== $options['default_protocol']) {
  26.             $builder->addEventSubscriber(new FixUrlProtocolListener($options['default_protocol']));
  27.         }
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function buildView(FormView $viewFormInterface $form, array $options)
  33.     {
  34.         if ($options['default_protocol']) {
  35.             $view->vars['attr']['inputmode'] = 'url';
  36.             $view->vars['type'] = 'text';
  37.         }
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function configureOptions(OptionsResolver $resolver)
  43.     {
  44.         $resolver->setDefaults([
  45.             'default_protocol' => 'http',
  46.             'invalid_message' => function (Options $options$previousValue) {
  47.                 return ($options['legacy_error_messages'] ?? true)
  48.                     ? $previousValue
  49.                     'Please enter a valid URL.';
  50.             },
  51.         ]);
  52.         $resolver->setAllowedTypes('default_protocol', ['null''string']);
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function getParent()
  58.     {
  59.         return TextType::class;
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function getBlockPrefix()
  65.     {
  66.         return 'url';
  67.     }
  68. }