Skip to content
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
14 changes: 9 additions & 5 deletions src/Symfony/Component/Validator/Constraints/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ class Url extends Constraint
public $normalizer;

/**
* @param string[]|null $protocols The protocols considered to be valid for the URL (e.g. http, https, ftp, etc.) (defaults to ['http', 'https']; use ['*'] to allow any protocol or regex patterns like ['.*'] for custom matching)
* @param bool|null $relativeProtocol Whether to accept URL without the protocol (i.e. //example.com) (defaults to false)
* @param string[]|null $groups
* @param bool|null $requireTld Whether to require the URL to include a top-level domain (defaults to false)
* @param string[]|string|null $protocols The protocols considered to be valid for the URL (e.g. http, https, ftp, etc.) (defaults to ['http', 'https']; use '*' to allow any protocol)
* @param bool|null $relativeProtocol Whether to accept URL without the protocol (i.e. //example.com) (defaults to false)
* @param string[]|null $groups
* @param bool|null $requireTld Whether to require the URL to include a top-level domain (defaults to false)
*/
#[HasNamedArguments]
public function __construct(
?array $options = null,
?string $message = null,
?array $protocols = null,
array|string|null $protocols = null,
?bool $relativeProtocol = null,
?callable $normalizer = null,
?array $groups = null,
Expand All @@ -67,6 +67,10 @@ public function __construct(
trigger_deprecation('symfony/validator', '7.1', 'Not passing a value for the "requireTld" option to the Url constraint is deprecated. Its default value will change to "true".');
}

if (\is_string($protocols)) {
$protocols = (array) $protocols;
}

$this->message = $message ?? $this->message;
$this->protocols = $protocols ?? $this->protocols;
$this->relativeProtocol = $relativeProtocol ?? $this->relativeProtocol;
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Validator/Tests/Constraints/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -88,6 +89,15 @@ public function testRequireTldDefaultsToFalse()

$this->assertFalse($constraint->requireTld);
}

#[TestWith(['*'])]
#[TestWith(['http'])]
public function testProtocolsAsString(string $protocol)
{
$constraint = new Url(protocols: $protocol, requireTld: true);

$this->assertSame([$protocol], $constraint->protocols);
}
}

class UrlDummy
Expand Down
Loading