-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
class FixUrlProtocolListener implements EventSubscriberInterface | ||
{ | ||
private $defaultProtocol; | ||
private $skipEmail = false; | ||
|
||
/** | ||
* @param string|null $defaultProtocol The URL scheme to add when there is none or null to not modify the data | ||
|
@@ -32,11 +33,25 @@ public function __construct(?string $defaultProtocol = 'http') | |
$this->defaultProtocol = $defaultProtocol; | ||
} | ||
|
||
/** | ||
* @param bool $skipEmail the URL scheme is not added to values that match an email pattern | ||
*/ | ||
public function skipEmail(): void | ||
{ | ||
$this->skipEmail = true; | ||
} | ||
|
||
public function onSubmit(FormEvent $event) | ||
{ | ||
$data = $event->getData(); | ||
|
||
if ($this->defaultProtocol && $data && \is_string($data) && !preg_match('~^[\w+.-]+://~', $data)) { | ||
if (preg_match('~^[^:/]+@[A-Za-z0-9-.]+\.[A-Za-z0-9]+$~', $data)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could be even more aggressive and simply skip adding the default protocol if there is an userinfo part in the URL. Since entering an URL with userinfo is quite rare, forcing to specify the protocol in this case would be a big decrease in user experience. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean "would not be a big decrease in user experience" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be |
||
if ($this->skipEmail) { | ||
return; | ||
} | ||
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__); | ||
} | ||
$event->setData($this->defaultProtocol.'://'.$data); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Tests\Extension\Core\Type; | ||
|
||
/** | ||
* @group legacy | ||
*/ | ||
class UrlTypeLegacyTest extends UrlTypeTest | ||
{ | ||
/** | ||
* Legacy behavior. Replace test in parent class. | ||
*/ | ||
public function testSubmitAddsNoDefaultProtocolToEmail() | ||
{ | ||
$form = $this->factory->create(static::TESTED_TYPE, 'name', $this->getTestOptions()); | ||
|
||
$form->submit('contact@domain.com'); | ||
|
||
$this->assertSame('http://contact@domain.com', $form->getData()); | ||
$this->assertSame('http://contact@domain.com', $form->getViewData()); | ||
} | ||
|
||
protected function getTestOptions(): array | ||
{ | ||
return []; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to replace this setter by a constructor argument