http://localhost/ is not valid url for UrlValidator
Not sure it is by design or bug if bug i can create a PR.
What steps will reproduce the problem?
$val = new UrlValidator;
$result = $val->validate('http://localhost/');
What is the expected result?
true
What do you get instead?
false
Both ::1 and 127.0.0.1 must also be kept in mind.
I would say this is a feature rather than a bug :) localhost will point to different host for every user of the system so if you allow adding such URLs it could introduce flaws with leaking information from internal network. That is true for all non-fqdn domain names. Why do you want to allow this?
@resurtm 127.0.0.1 pass the tests but ::1 not
@cebe I need to communicate with other services on same host but different ports
@amincheloh you can adjust the $pattern property of the validator to make it work in your case.
In general I think the validator could be based on parse_url() function instead of a regex and allow further options like forcing https urls only for example. Or a switch to enable/disable fqdn names. I suggest to rewrite it for 2.1.0.
I ran into the same issue during functional testing with codeception where one of the params for a model was the referal Url and codeception runs everything on http://localhost.
What steps will reproduce the problem?
$urlValid = new \yii\validators\UrlValidator();
var_dump($urlValid->validate("http://www.google.com/uncheck/orderPay/ejbPaySucess.do\\"));
exit;
What is the expected result?
bool(true)
What do you get instead?
bool(false)
Additional info
| Q | A |
|---|---|
| Yii version | 2.0.11.2 |
| PHP version | 7.0.1 |
| Operating system | windows |
@phpyii what's the purpose of the backslash at the end of the URL?
@cebe Today I found the URL stored in the database. I added URL validation
As @cebe said, that's a special case and there's a way to make it work by adjusting regular expression. Not going to change it.
As @cebe said, that's a special case and there's a way to make it work by adjusting regular expression. Not going to change it.
It seems that this effects not only localhost. For me it seems the every local domain name (without a dot) seems to be declared as invalid.
@Radon8472 correct.
In fact, Url rule is now Regex rule with IDN support. We can improve rule so:
-
Rule always check by internal regex that string is correct URL according to the RFC.
-
Allow to user set constrains (optional) for host, port, path, etc.
I thin for some people, it would be good to allow local do mains for others not.
You alredy have a PR yiisoft/yii2#17965 what enables support for local domains.
I would say, you yust should add a config option (e.g. allowLocalDomains) to switch this on / off.
And having this option, each user could choose his/her desired behavior
Adding it as an option disabled by default is fine.