$this->setValidator('website', new sfValidatorAnd(array(
$this->validatorSchema['website'],
new sfValidatorUrl(array(), array(
'invalid' => 'This is not website',
)),
)));
这验证了 http://google.com,但是 google.com 没有。我如何在没有 http:// 的情况下进行此编辑以进行验证?
最佳答案
恐怕您需要创建自己的自定义验证器:
class myCustomValidatorUrl extends sfValidatorRegex
{
const REGEX_URL_FORMAT = '~^
((%s)://)? # protocol
(
([a-z0-9-]+\.)+[a-z]{2,6} # a domain name
| # or
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # a IP address
)
(:[0-9]+)? # a port (optional)
(/?|/\S+) # a /, nothing or a / with something
$~ix';
protected function configure($options = array(), $messages = array())
{
parent::configure($options, $messages);
$this->addOption('protocols', array('http', 'https', 'ftp', 'ftps'));
$this->setOption('pattern', new sfCallable(array($this, 'generateRegex')));
}
public function generateRegex()
{
return sprintf(self::REGEX_URL_FORMAT, implode('|', $this->getOption('protocols')));
}
}
这里 ((%s)://)?
表示现在协议(protocol)是可选的。
参见 sfValidatorUrl对于原始模式(REGEX_URL_FORMAT 常量)。
关于php - 在没有 http ://in symfony 的情况下验证 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6690596/