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

[Form] UrlType should not add protocol to emails #43707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 UPGRADE-5.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Form
------

* Deprecate calling `FormErrorIterator::children()` if the current element is not iterable.
* `UrlType` does not add the default protocol to urls that looks like emails or does not contain a dot or a slash.

FrameworkBundle
---------------
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Deprecate calling `FormErrorIterator::children()` if the current element is not iterable.
* Allow to pass `TranslatableMessage` objects to the `help` option
* Add the `EnumType`
* `UrlType` does not add the default protocol to urls that looks like emails or does not contain a dot or a slash.

5.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public function onSubmit(FormEvent $event)
{
$data = $event->getData();

if ($this->defaultProtocol && $data && \is_string($data) && !preg_match('~^[\w+.-]+://~', $data)) {
if ($this->defaultProtocol && $data && \is_string($data)
&& !preg_match('~^[\w+.-]+://~', $data)
// skip email & non-url values
&& !preg_match('~^([^:/?@]++@|[^./]+$)~', $data)
) {
$event->setData($this->defaultProtocol.'://'.$data);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests\Extension\Core\EventListener;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener;
use Symfony\Component\Form\Form;
Expand All @@ -20,6 +21,8 @@

class FixUrlProtocolListenerTest extends TestCase
{
use ExpectDeprecationTrait;

public function testFixHttpUrl()
{
$data = 'www.symfony.com';
Expand All @@ -32,26 +35,17 @@ public function testFixHttpUrl()
$this->assertEquals('http://www.symfony.com', $event->getData());
}

public function testSkipKnownUrl()
{
$data = 'http://www.symfony.com';
$form = new Form(new FormConfigBuilder('name', null, new EventDispatcher()));
$event = new FormEvent($form, $data);

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

$this->assertEquals('http://www.symfony.com', $event->getData());
}

public function provideUrlsWithSupportedProtocols()
{
return [
['http://www.symfony.com'],
['ftp://www.symfony.com'],
['chrome-extension://foo'],
['h323://foo'],
['iris.beep://foo'],
['foo+bar://foo'],
['fabien@symfony.com'],
['foo'],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)

public function testSubmitNullReturnsNullWithEmptyDataAsString()
{
$form = $this->factory->create(static::TESTED_TYPE, 'name', [
$form = $this->factory->create(static::TESTED_TYPE, 'name', array_merge($this->getTestOptions(), [
'empty_data' => '',
]);
]));

$form->submit(null);
$this->assertSame('', $form->getData());
Expand All @@ -48,9 +48,9 @@ public function provideZeros()
*/
public function testSetDataThroughParamsWithZero($data, $dataAsString)
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
$form = $this->factory->create(static::TESTED_TYPE, null, array_merge($this->getTestOptions(), [
'data' => $data,
]);
]));
$view = $form->createView();

$this->assertFalse($form->isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ class UrlTypeTest extends TextTypeTest

public function testSubmitAddsDefaultProtocolIfNoneIsIncluded()
{
$form = $this->factory->create(static::TESTED_TYPE, 'name');
$form = $this->factory->create(static::TESTED_TYPE, 'name', $this->getTestOptions());

$form->submit('www.domain.com');

$this->assertSame('http://www.domain.com', $form->getData());
$this->assertSame('http://www.domain.com', $form->getViewData());
}

public function testSubmitAddsNoDefaultProtocolToEmail()
{
$form = $this->factory->create(static::TESTED_TYPE, 'name', $this->getTestOptions());

$form->submit('contact@domain.com');

$this->assertSame('contact@domain.com', $form->getData());
$this->assertSame('contact@domain.com', $form->getViewData());
}

public function testSubmitAddsNoDefaultProtocolIfAlreadyIncluded()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
$form = $this->factory->create(static::TESTED_TYPE, null, array_merge($this->getTestOptions(), [
'default_protocol' => 'http',
]);
]));

$form->submit('ftp://www.domain.com');

Expand All @@ -41,9 +51,9 @@ public function testSubmitAddsNoDefaultProtocolIfAlreadyIncluded()

public function testSubmitAddsNoDefaultProtocolIfEmpty()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
$form = $this->factory->create(static::TESTED_TYPE, null, array_merge($this->getTestOptions(), [
'default_protocol' => 'http',
]);
]));

$form->submit('');

Expand All @@ -53,9 +63,9 @@ public function testSubmitAddsNoDefaultProtocolIfEmpty()

public function testSubmitAddsNoDefaultProtocolIfNull()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
$form = $this->factory->create(static::TESTED_TYPE, null, array_merge($this->getTestOptions(), [
'default_protocol' => 'http',
]);
]));

$form->submit(null);

Expand All @@ -65,9 +75,9 @@ public function testSubmitAddsNoDefaultProtocolIfNull()

public function testSubmitAddsNoDefaultProtocolIfSetToNull()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
$form = $this->factory->create(static::TESTED_TYPE, null, array_merge($this->getTestOptions(), [
'default_protocol' => null,
]);
]));

$form->submit('www.domain.com');

Expand All @@ -83,11 +93,11 @@ public function testThrowExceptionIfDefaultProtocolIsInvalid()
]);
}

public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = 'http://empty')
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty.com', $expectedData = 'http://empty.com')
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
$form = $this->factory->create(static::TESTED_TYPE, null, array_merge($this->getTestOptions(), [
'empty_data' => $emptyData,
]);
]));
$form->submit(null);

// listener normalizes data on submit
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.