我可以将töst.tv
注册为域名,换句话说,它是一个有效的域名。
Zend 2 Hostname Validator将在以下示例中返回false:
// create hostname validator
$oHostnameValidator = new \Zend\Validator\Hostname(array(
'allow' => \Zend\Validator\Hostname::ALLOW_DNS,
'useIdnCheck' => true,
'useTldCheck' => false,
));
if(!$oHostnameValidator->isValid('töst.tv')) // isValid returns false
{
print_r($oHostnameValidator->getMessages());
}
getMessages
将返回:
Array
(
[hostnameInvalidHostnameSchema] => Die Eingabe scheint ein DNS Hostname zu sein, passt aber ...
[hostnameInvalidLocalName] => Die Eingabe scheint kein gültiger lokaler Netzerkname zu...
)
我发现protected $validIdns
不包含tld tv
(在课程Zend\Validator\Hostname
中)
有没有办法(update-safe)将当前有效的idn检查注入zend hostname validator中的某些tld?
或者这是一个应该报告的错误?
修改
我刚刚扩展了主机名验证器(感谢 Wilt )
<?php
namespace yourNamespace;
class Hostname extends \Zend\Validator\Hostname
{
/**
* Sets validator options.
*
* @param int $allow OPTIONAL Set what types of hostname to allow (default ALLOW_DNS)
* @param bool $useIdnCheck OPTIONAL Set whether IDN domains are validated (default true)
* @param bool $useTldCheck Set whether the TLD element of a hostname is validated (default true)
* @param Ip $ipValidator OPTIONAL
* @see http://www.iana.org/cctld/specifications-policies-cctlds-01apr02.htm Technical Specifications for ccTLDs
*/
public function __construct($options = array())
{
// call parent construct
parent::__construct($options);
// inject valid idns
$this->_injectValidIDNs();
}
/**
* inject new valid idns - use first DE validation as default (until we get the specified correct ones ...)
*/
protected function _injectValidIDNs()
{
// inject TV validation
if(!isset($this->validIdns['TV']))
{
$this->validIdns['TV'] = array(
1 => array_values($this->validIdns['DE'])[0],
);
}
}
}
答案 0 :(得分:1)
您可以在GitHub上发出问题,并为Zend\Validator\Hostname
类提取拉取请求,在该类中添加根据您应该也在$validIdns
数组中的值。
否则,您还可以扩展项目中的现有类,并使用自定义的值覆盖现有的$validIdns
值:
<?php
namespace My\Validator;
class HostName extends \Zend\Validator\Hostname
{
protected $validIdns = [
//...your custom value for TV + existing ones...
]
}
现在您可以像这样使用它:
$oHostnameValidator = new \My\Validator\Hostname(array(
'allow' => \My\Validator\Hostname::ALLOW_DNS,
'useIdnCheck' => true,
'useTldCheck' => false,
));