php - 在没有 http ://in symfony 的情况下验证 url

标签 php validation symfony1 symfony-1.4

  $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/

相关文章:

php - 禁用 "square bracket to array"表单值名称的解析

php - Doctrine2 基础;代理、存储库

php - 使用 PHP 从 Redis 中选择特定模式的所有关键数据

javascript - 验证 NoCaptcha ReCaptcha

mysql - 复制具有关系的数据库记录

javascript - 通过python脚本将php变量参数传递给onclick函数

node.js - Sails.js 中的密码确认和外部模型验证

c# - 如何强制 ValidationAttribute 将指定的对象成员标记为无效?

caching - 在 Symfony 中防止 Doctrine 的查询缓存

symfony1 - 交响乐团 : How to save record with undefined foreign key record