简体   繁体   English

ZF2:如何使用和不使用http://或https://使Zend \\ Validator \\ Hostname接受url字符串

[英]ZF2: How to make Zend\Validator\Hostname accept url strings with and without http:// or https://

I use Zend\\Validator\\Hostname to validate an input string containing a url. 我使用Zend \\ Validator \\ Hostname来验证包含url的输入字符串。 The problem is that it only accepts URLs of type mydomain.com and not with a http:// or https:// protocol prefix. 问题是它只接受mydomain.com类型的URL,而不接受http://或https://协议前缀。 What is the best way to achieve the desired behaviour? 实现理想行为的最佳方法是什么?

$inputFilter->add($factory->createInput(array(
                'name'     => 'shop_link',
                'required' => false,
                'filters'  => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                ),
                'validators' => array(
                        array(
                            'name'    => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min'      => 1,
                                'max'      => 100,
                            ),
                        ),
                        array(
                            'name'    => 'Hostname',
                            'options' => array(
                                'allow'       => \Zend\Validator\Hostname::ALLOW_DNS, // Allow these hostnames
                                'useIdnCheck' => true,  // Check IDN domains
                                'useTldCheck' => true,  // Check TLD elements
                                'ipValidator' => null,  // IP validator to use
                            ),
                        ),
                ),
        )));

Thanks 谢谢

I don't have 50 reputation to comment Sam's answer, but Hostname::ALLOW_URI doesn't work. 我没有50个评论Sam的答案的声誉,但Hostname :: ALLOW_URI不起作用。 And didn't work year ago - https://stackoverflow.com/a/13902180/822947 . 并且在一年前没有工作 - https://stackoverflow.com/a/13902180/822947 And shouldn't work, if you look at the source code. 如果你看一下源代码,就不应该工作。 Strange that it has two upvotes. 奇怪的是,它有两个upvotes。

$validator = new \Zend\Validator\Hostname();
$validator->setAllow($validator::ALLOW_DNS | $validator::ALLOW_URI);
var_dump($validator->isValid('http://domain.com')); // false!

There's two things you should try, first one will probably already be your desired result: 您应该尝试两件事,第一件可能已经是您想要的结果:

'options' => array(
    'allow' => Hostname::ALLOW_URI | Hostname::ALLOW_DNS,
    ....
)

Allowing the URI, at least going by the RFC-Specification, should include the validation of the Scheme... 允许URI,至少通过RFC规范,应该包括Scheme的验证...

In addition to that, there is also a Filter that you may want to know about: Zend\\Filter\\UriNormalize 除此之外,还有一个您可能想知道的Zend\\Filter\\UriNormalizeZend\\Filter\\UriNormalize

I've used Uri validator and disabled allowRelative (set to false) for validating URIs: 我使用了Uri验证器并禁用了allowRelative (设置为false)来验证URI:

$this->add(array(
        'name' => 'url',
        'validators' => array(
            array(
                'name' => 'Uri',
                'options' => array(
                    'allowRelative' => false
                ),
            ),
        ),
    ));

Try this: 尝试这个:

public function isValidUrl($url) {
    $uri = new \Zend\Validator\Uri();
    if (!$uri->isValid($url)) {
        return FALSE;
    }
    $parseUrl = parse_url($url);
    if(!isset($parseUrl['host']) || empty($parseUrl['host'])) {
        return FALSE;
    }
    $validator = new \Zend\Validator\Hostname(\Zend\Validator\Hostname::ALLOW_DNS);
    if (!$validator->isValid($parseUrl['host'])) {
        return FALSE;
    }
    if (!filter_var($parseUrl['host'], FILTER_VALIDATE_URL) === false) {
        return FALSE;
    }
    return TRUE;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM