diff --git a/UPGRADE-5.4.md b/UPGRADE-5.4.md index c5542c4dcf632..87b34cd35fd7f 100644 --- a/UPGRADE-5.4.md +++ b/UPGRADE-5.4.md @@ -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 --------------- diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 9d7e76445500e..bbb90c0b40ff0 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -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 --- diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php index 53dd4ee8711d2..0dd42350bbc2e 100644 --- a/src/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/FixUrlProtocolListener.php @@ -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); } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php index e00cb9e9e1978..113b4bb6afa89 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/EventListener/FixUrlProtocolListenerTest.php @@ -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; @@ -20,6 +21,8 @@ class FixUrlProtocolListenerTest extends TestCase { + use ExpectDeprecationTrait; + public function testFixHttpUrl() { $data = 'www.symfony.com'; @@ -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'], ]; } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TextTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TextTypeTest.php index 3f8fbe7725ffc..709798f712510 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TextTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TextTypeTest.php @@ -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()); @@ -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()); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php index b9387d01a45e6..3e51d56432a71 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/UrlTypeTest.php @@ -19,7 +19,7 @@ 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'); @@ -27,11 +27,21 @@ public function testSubmitAddsDefaultProtocolIfNoneIsIncluded() $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'); @@ -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(''); @@ -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); @@ -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'); @@ -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