bug #10586 Fixes URL validator to accept single part urls (merk)

This PR was merged into the 2.3 branch.

Discussion
----------

Fixes URL validator to accept single part urls

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #3207 #6817
| License       | MIT
| Doc PR        | N/A

This PR replaces #6817 and has been rebased for 2.3, making a change to the regex to be simpler for single dot matches. (changing `([\pL\pN\pS-\.])+([\.]{0,1}[\pL]+[\.]{0,1})` to `([\pL\pN\pS-\.])+(\.?[\pL]+\.?)`)

Commits
-------

91e226e Fixes URL validator to accept single part urls
This commit is contained in:
Fabien Potencier 2014-03-31 13:10:34 +02:00
commit b094ea7fe2
2 changed files with 7 additions and 4 deletions

View File

@ -25,10 +25,10 @@ class UrlValidator extends ConstraintValidator
const PATTERN = '~^
(%s):// # protocol
(
([\pL\pN\pS-]+\.)+([\pL]|xn\-\-[\pL\pN-]+)+ # a domain name
| # or
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # a IP address
| # or
([\pL\pN\pS-\.])+(\.?([\pL]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
| # or
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # a IP address
| # or
\[
(?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?: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(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
\] # a IPv6 address

View File

@ -72,6 +72,7 @@ class UrlValidatorTest extends \PHPUnit_Framework_TestCase
return array(
array('http://a.pl'),
array('http://www.google.com'),
array('http://www.google.com.'),
array('http://www.google.museum'),
array('https://google.com/'),
array('https://google.com:80/'),
@ -85,6 +86,7 @@ class UrlValidatorTest extends \PHPUnit_Framework_TestCase
array('http://symfony.com/#?'),
array('http://www.symfony.com/doc/current/book/validation.html#supported-constraints'),
array('http://very.long.domain.name.com/'),
array('http://localhost/'),
array('http://127.0.0.1/'),
array('http://127.0.0.1:80/'),
array('http://[::1]/'),
@ -152,6 +154,7 @@ class UrlValidatorTest extends \PHPUnit_Framework_TestCase
array('http://127.0.0.1:aa/'),
array('ftp://[::1]/'),
array('http://[::1'),
array('http://hello.☎/'),
);
}