I need an URL validator inside a form. Now I'm using:
$url = new Zend_Form_Element_Text('url');
$url->addValidator(new Zend_Validate_Hostname());
The code above will validate URL's like: www.domain.com
but will fail validation for http://www.domain.com
.
What validator should I use in order to validate both, with and without protocol URL's.
This will validate both domain and uri:
$form->addElement ('text', 'url',array( 'validators' => array ( array( 'Callback', true, ['callback' => function($value) { if (Zend_Uri::check($value)) { $hostnameValidator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_DNS); //do not allow local hostnames, this is the default $uriHttp = Zend_Uri_Http::fromString($value); return $hostnameValidator->isValid($uriHttp->getHost()); } return false; }], 'messages' => [Zend_Validate_Callback::INVALID_VALUE => 'Please enter a valid URL'] ) ) ));
You cant do it with Zend_Validate_Hostname. The way you could do it, is to write your own validator that will split sting into 2 parts - and validate them using Zend_Validate_Hostname for hostname and Zend_Validate_InArray for protocol
您需要删除方案http,或实施新的可重复使用的验证器http://www.rondobley.com/2011/09/24/how-to-validate-a-url-with-a-scheme-and-主机名在-Zend的框架/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.