Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b3c58ec

Browse filesBrowse files
committed
[Form] Change UrlType behaviour to not convert emails to valid urls
1 parent 320c757 commit b3c58ec
Copy full SHA for b3c58ec

File tree

9 files changed

+27
-81
lines changed
Filter options

9 files changed

+27
-81
lines changed

‎UPGRADE-5.4.md

Copy file name to clipboardExpand all lines: UPGRADE-5.4.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Form
2222
------
2323

2424
* Deprecate calling `FormErrorIterator::children()` if the current element is not iterable.
25-
* Add `'default_protocol_skip_email' => true` to `UrlType` options.
25+
* `UrlType` does not add the default protocol to urls that looks like emails or does not contain a dot or a slash.
2626

2727
FrameworkBundle
2828
---------------

‎src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,7 +2648,7 @@ public function testTimezoneWithPlaceholder()
26482648
public function testUrlWithDefaultProtocol()
26492649
{
26502650
$url = 'http://www.example.com?foo1=bar1&foo2=bar2';
2651-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => 'http', 'default_protocol_skip_email' => true]);
2651+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => 'http']);
26522652

26532653
$this->assertWidgetMatchesXpath($form->createView(), ['attr' => ['class' => 'my&class']],
26542654
'/input
@@ -2664,7 +2664,7 @@ public function testUrlWithDefaultProtocol()
26642664
public function testUrlWithoutDefaultProtocol()
26652665
{
26662666
$url = 'http://www.example.com?foo1=bar1&foo2=bar2';
2667-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => null, 'default_protocol_skip_email' => true]);
2667+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => null]);
26682668

26692669
$this->assertWidgetMatchesXpath($form->createView(), ['attr' => ['class' => 'my&class']],
26702670
'/input

‎src/Symfony/Component/Form/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CHANGELOG
77
* Deprecate calling `FormErrorIterator::children()` if the current element is not iterable.
88
* Allow to pass `TranslatableMessage` objects to the `help` option
99
* Add the `EnumType`
10-
* Deprecate usage of `UrlType` without option `'default_protocol_skip_email' => true`, added to prevent emails from being converted to valid URLs.
10+
* `UrlType` does not add the default protocol to urls that looks like emails or does not contain a dot or a slash.
1111

1212
5.3
1313
---

‎src/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php
+6-14Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Extension\Core\EventListener;
1313

1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\Form\Extension\Core\Type\UrlType;
1516
use Symfony\Component\Form\FormEvent;
1617
use Symfony\Component\Form\FormEvents;
1718

@@ -23,7 +24,6 @@
2324
class FixUrlProtocolListener implements EventSubscriberInterface
2425
{
2526
private $defaultProtocol;
26-
private $skipEmail = false;
2727

2828
/**
2929
* @param string|null $defaultProtocol The URL scheme to add when there is none or null to not modify the data
@@ -33,24 +33,16 @@ public function __construct(?string $defaultProtocol = 'http')
3333
$this->defaultProtocol = $defaultProtocol;
3434
}
3535

36-
/**
37-
* @param bool $skipEmail the URL scheme is not added to values that match an email pattern
38-
*/
39-
public function skipEmail(): void
40-
{
41-
$this->skipEmail = true;
42-
}
43-
4436
public function onSubmit(FormEvent $event)
4537
{
4638
$data = $event->getData();
4739

4840
if ($this->defaultProtocol && $data && \is_string($data) && !preg_match('~^[\w+.-]+://~', $data)) {
49-
if (preg_match('~^[^:/]+@[A-Za-z0-9-.]+\.[A-Za-z0-9]+$~', $data)) {
50-
if ($this->skipEmail) {
51-
return;
52-
}
53-
trigger_deprecation('symfony/form', '5.4', 'Class "%s", will add a scheme to urls that looks like emails in 6.0. Call "setIgnoreEmail(true)"', __CLASS__);
41+
// Detect email & non-url
42+
if (preg_match('~^([^:/?@]++@|[^./]+$)~', $data)) {
43+
trigger_deprecation('symfony/form', '5.4', 'Form type "url", does not add a default protocol to urls that looks like emails or does not contain a dot or slash.');
44+
45+
return;
5446
}
5547
$event->setData($this->defaultProtocol.'://'.$data);
5648
}

‎src/Symfony/Component/Form/Extension/Core/Type/UrlType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/UrlType.php
+1-9Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ class UrlType extends AbstractType
2727
public function buildForm(FormBuilderInterface $builder, array $options)
2828
{
2929
if (null !== $options['default_protocol']) {
30-
$subscriber = new FixUrlProtocolListener($options['default_protocol']);
31-
if ($options['default_protocol_skip_email']) {
32-
$subscriber->skipEmail();
33-
} else {
34-
trigger_deprecation('symfony/form', '5.4', 'Type "%s" option "default_protocol_skip_email" will be "true" in 6.0.', static::class);
35-
}
36-
$builder->addEventSubscriber($subscriber);
30+
$builder->addEventSubscriber(new FixUrlProtocolListener($options['default_protocol']));
3731
}
3832
}
3933

@@ -60,11 +54,9 @@ public function configureOptions(OptionsResolver $resolver)
6054
? $previousValue
6155
: 'Please enter a valid URL.';
6256
},
63-
'default_protocol_skip_email' => false,
6457
]);
6558

6659
$resolver->setAllowedTypes('default_protocol', ['null', 'string']);
67-
$resolver->setAllowedTypes('default_protocol_skip_email', 'bool');
6860
}
6961

7062
/**

‎src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,7 +2296,7 @@ public function testTimezoneWithPlaceholder()
22962296
public function testUrlWithDefaultProtocol()
22972297
{
22982298
$url = 'http://www.example.com?foo1=bar1&foo2=bar2';
2299-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => 'http', 'default_protocol_skip_email' => true]);
2299+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => 'http']);
23002300

23012301
$this->assertWidgetMatchesXpath($form->createView(), [],
23022302
'/input
@@ -2311,7 +2311,7 @@ public function testUrlWithDefaultProtocol()
23112311
public function testUrlWithoutDefaultProtocol()
23122312
{
23132313
$url = 'http://www.example.com?foo1=bar1&foo2=bar2';
2314-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => null, 'default_protocol_skip_email' => true]);
2314+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\UrlType', $url, ['default_protocol' => null]);
23152315

23162316
$this->assertWidgetMatchesXpath($form->createView(), [],
23172317
'/input

‎src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php
+14-9Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function testFixHttpUrl()
3030
$event = new FormEvent($form, $data);
3131

3232
$filter = new FixUrlProtocolListener('http');
33-
$filter->skipEmail();
3433
$filter->onSubmit($event);
3534

3635
$this->assertEquals('http://www.symfony.com', $event->getData());
@@ -45,8 +44,6 @@ public function provideUrlsWithSupportedProtocols()
4544
['h323://foo'],
4645
['iris.beep://foo'],
4746
['foo+bar://foo'],
48-
['fabien@symfony.com'],
49-
['Contact+42@subdomain.example.com'],
5047
];
5148
}
5249

@@ -59,26 +56,34 @@ public function testSkipOtherProtocol($url)
5956
$event = new FormEvent($form, $url);
6057

6158
$filter = new FixUrlProtocolListener('http');
62-
$filter->skipEmail();
6359
$filter->onSubmit($event);
6460

6561
$this->assertEquals($url, $event->getData());
6662
}
6763

6864
/**
6965
* @group legacy
66+
* @dataProvider provideNonUrls
7067
*/
71-
public function testDeprecatedFixEmail()
68+
public function testDeprecatedFixEmail($url)
7269
{
73-
$this->expectDeprecation('Since symfony/form 5.4: Class "Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener", will add a scheme to urls that looks like emails in 6.0. Call "setIgnoreEmail(true)"');
70+
$this->expectDeprecation('Since symfony/form 5.4: Form type "url", does not add a default protocol to urls that looks like emails or does not contain a dot or slash.');
7471

75-
$data = 'fabien@symfony.com';
7672
$form = new Form(new FormConfigBuilder('name', null, new EventDispatcher()));
77-
$event = new FormEvent($form, $data);
73+
$event = new FormEvent($form, $url);
7874

7975
$filter = new FixUrlProtocolListener('http');
8076
$filter->onSubmit($event);
8177

82-
$this->assertEquals('http://fabien@symfony.com', $event->getData());
78+
$this->assertEquals($url, $event->getData());
79+
}
80+
81+
82+
public function provideNonUrls()
83+
{
84+
return [
85+
['fabien@symfony.com'],
86+
['foo'],
87+
];
8388
}
8489
}

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeLegacyTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeLegacyTest.php
-36Lines changed: 0 additions & 36 deletions
This file was deleted.

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php
-7Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,4 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expect
105105
$this->assertSame($expectedData, $form->getNormData());
106106
$this->assertSame($expectedData, $form->getData());
107107
}
108-
109-
protected function getTestOptions(): array
110-
{
111-
return [
112-
'default_protocol_skip_email' => true,
113-
];
114-
}
115108
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.