From 40b58be7b00f973daf1f9b6ea8cc700c95194d5d Mon Sep 17 00:00:00 2001 From: Mathieu Lechat Date: Wed, 26 Dec 2018 17:27:02 +0100 Subject: [PATCH] [Form] Changed UrlType input type to text when default_protocol is not null --- Extension/Core/Type/UrlType.php | 13 +++++++++++++ Tests/AbstractLayoutTest.php | 19 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Extension/Core/Type/UrlType.php b/Extension/Core/Type/UrlType.php index 17460688b7..39b9d2d208 100644 --- a/Extension/Core/Type/UrlType.php +++ b/Extension/Core/Type/UrlType.php @@ -14,6 +14,8 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; class UrlType extends AbstractType @@ -28,6 +30,17 @@ public function buildForm(FormBuilderInterface $builder, array $options) } } + /** + * {@inheritdoc} + */ + public function buildView(FormView $view, FormInterface $form, array $options) + { + if ($options['default_protocol']) { + $view->vars['attr']['inputmode'] = 'url'; + $view->vars['type'] = 'text'; + } + } + /** * {@inheritdoc} */ diff --git a/Tests/AbstractLayoutTest.php b/Tests/AbstractLayoutTest.php index bb7fc197ab..0e749b780c 100644 --- a/Tests/AbstractLayoutTest.php +++ b/Tests/AbstractLayoutTest.php @@ -2170,10 +2170,25 @@ public function testTimezoneWithPlaceholder() ); } - public function testUrl() + public function testUrlWithDefaultProtocol() { $url = 'http://www.google.com?foo1=bar1&foo2=bar2'; - $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url); + $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => 'http']); + + $this->assertWidgetMatchesXpath($form->createView(), [], +'/input + [@type="text"] + [@name="name"] + [@value="http://www.google.com?foo1=bar1&foo2=bar2"] + [@inputmode="url"] +' + ); + } + + public function testUrlWithoutDefaultProtocol() + { + $url = 'http://www.google.com?foo1=bar1&foo2=bar2'; + $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => null]); $this->assertWidgetMatchesXpath($form->createView(), [], '/input