Skip to content

[Form] Change UrlType default protocol #60922

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

Merged
Merged
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
24 changes: 24 additions & 0 deletions UPGRADE-8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ DoctrineBridge
$type = $extractor->getType(Foo::class, 'property');
```

Form
----

* The `default_protocol` option in `UrlType` now defaults to `null` instead of `'http'`

*Before*
```php
// URLs without protocol were automatically prefixed with 'http://'
$builder->add('website', UrlType::class);
// Input: 'example.com' → Value: 'http://example.com'
```

*After*
```php
// URLs without protocol are now kept as-is
$builder->add('website', UrlType::class);
// Input: 'example.com' → Value: 'example.com'

// To restore the previous behavior, explicitly set the option:
$builder->add('website', UrlType::class, [
'default_protocol' => 'http',
]);
```

FrameworkBundle
---------------

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

8.0
---

* Change default value of `default_protocol` option in `UrlType` from `'http'` to `null`

7.4
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ public function buildView(FormView $view, FormInterface $form, array $options):
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'default_protocol' => static function (Options $options) {
trigger_deprecation('symfony/form', '7.1', 'Not configuring the "default_protocol" option when using the UrlType is deprecated. It will default to "null" in 8.0.');

return 'http';
},
'default_protocol' => null,
'invalid_message' => 'Please enter a valid URL.',
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,13 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

class UrlTypeTest extends TextTypeTest
{
use ExpectUserDeprecationMessageTrait;

public const TESTED_TYPE = UrlType::class;

/**
* @group legacy
*/
public function testSubmitAddsDefaultProtocolIfNoneIsIncluded()
{
$this->expectUserDeprecationMessage('Since symfony/form 7.1: Not configuring the "default_protocol" option when using the UrlType is deprecated. It will default to "null" in 8.0.');
$form = $this->factory->create(static::TESTED_TYPE, 'name');

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

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

public function testSubmitAddsNoDefaultProtocolIfAlreadyIncluded()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
Expand Down Expand Up @@ -107,6 +90,6 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expect

protected function getTestOptions(): array
{
return ['default_protocol' => 'http'];
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UrlTypeValidatorExtensionTest extends BaseValidatorExtensionTestCase

protected function createForm(array $options = [])
{
return $this->factory->create(UrlType::class, null, $options + ['default_protocol' => 'http']);
return $this->factory->create(UrlType::class, null, $options);
}

public function testInvalidMessage()
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy