Skip to content

Commit

Permalink
[Form] Changed UrlType input type to text when default_protocol is no…
Browse files Browse the repository at this point in the history
…t null
  • Loading branch information
Mathieu Lechat authored and xabbuh committed Jan 18, 2019
1 parent 5646f5b commit 40b58be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Extension/Core/Type/UrlType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
*/
Expand Down
19 changes: 17 additions & 2 deletions Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 40b58be

Please sign in to comment.