Skip to content

Commit f762833

Browse files
bug #29926 [Form] Changed UrlType input type to text when default_protocol is not null (MatTheCat)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] Changed UrlType input type to text when default_protocol is not null | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29690 | License | MIT | Doc PR | replaces #29691 Commits ------- 2791edf1fb [Form] Changed UrlType input type to text when default_protocol is not null
2 parents 872488d + 40b58be commit f762833

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Extension/Core/Type/UrlType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener;
1616
use Symfony\Component\Form\FormBuilderInterface;
17+
use Symfony\Component\Form\FormInterface;
18+
use Symfony\Component\Form\FormView;
1719
use Symfony\Component\OptionsResolver\OptionsResolver;
1820

1921
class UrlType extends AbstractType
@@ -28,6 +30,17 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2830
}
2931
}
3032

33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function buildView(FormView $view, FormInterface $form, array $options)
37+
{
38+
if ($options['default_protocol']) {
39+
$view->vars['attr']['inputmode'] = 'url';
40+
$view->vars['type'] = 'text';
41+
}
42+
}
43+
3144
/**
3245
* {@inheritdoc}
3346
*/

Tests/AbstractLayoutTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,10 +2170,25 @@ public function testTimezoneWithPlaceholder()
21702170
);
21712171
}
21722172

2173-
public function testUrl()
2173+
public function testUrlWithDefaultProtocol()
21742174
{
21752175
$url = 'http://www.google.com?foo1=bar1&foo2=bar2';
2176-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url);
2176+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => 'http']);
2177+
2178+
$this->assertWidgetMatchesXpath($form->createView(), [],
2179+
'/input
2180+
[@type="text"]
2181+
[@name="name"]
2182+
[@value="http://www.google.com?foo1=bar1&foo2=bar2"]
2183+
[@inputmode="url"]
2184+
'
2185+
);
2186+
}
2187+
2188+
public function testUrlWithoutDefaultProtocol()
2189+
{
2190+
$url = 'http://www.google.com?foo1=bar1&foo2=bar2';
2191+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => null]);
21772192

21782193
$this->assertWidgetMatchesXpath($form->createView(), [],
21792194
'/input

0 commit comments

Comments
 (0)