Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Deprecate not configuring the default_protocol option of the UrlType #50922

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 1 commit into from
Nov 18, 2023
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
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
=========

7.1
---

* Deprecate not configuring the `default_protocol` option of the `UrlType`, it will default to `null` in 8.0

7.0
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UrlType extends AbstractType
Expand All @@ -38,7 +39,11 @@ public function buildView(FormView $view, FormInterface $form, array $options):
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'default_protocol' => 'http',
'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';
},
'invalid_message' => 'Please enter a valid URL.',
]);

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 static function provideZeros(): array
*/
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 @@ -11,14 +11,21 @@

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

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

class UrlTypeTest extends TextTypeTest
{
use ExpectDeprecationTrait;

public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\UrlType';

/**
* @group legacy
*/
public function testSubmitAddsDefaultProtocolIfNoneIsIncluded()
{
$this->expectDeprecation('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');
Expand Down Expand Up @@ -86,6 +93,7 @@ public function testThrowExceptionIfDefaultProtocolIsInvalid()
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = 'http://empty')
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'default_protocol' => 'http',
'empty_data' => $emptyData,
]);
$form->submit(null);
Expand All @@ -95,4 +103,9 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expect
$this->assertSame($expectedData, $form->getNormData());
$this->assertSame($expectedData, $form->getData());
}

protected function getTestOptions(): array
{
return ['default_protocol' => 'http'];
}
}
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);
return $this->factory->create(UrlType::class, null, $options + ['default_protocol' => 'http']);
}

public function testInvalidMessage()
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"require": {
"php": ">=8.2",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/event-dispatcher": "^6.4|^7.0",
"symfony/options-resolver": "^6.4|^7.0",
"symfony/polyfill-ctype": "~1.8",
Expand Down