|
Revision 22149, 1.7 kB
(checked in by Kris.Wallsmith, 4 years ago)
|
[1.3] added a check for sfCallable in regex validator, added protocols option to url validator, moved email pattern to class constant
|
- Property svn:mime-type set to
text/x-php
- Property svn:eol-style set to
native
- Property svn:keywords set to
Id
|
| Line | |
|---|
| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfValidatorUrl extends sfValidatorRegex |
|---|
| 20 |
{ |
|---|
| 21 |
const REGEX_URL_FORMAT = '~^ |
|---|
| 22 |
(%s):// # protocol |
|---|
| 23 |
( |
|---|
| 24 |
([a-z0-9-]+\.)+[a-z]{2,6} # a domain name |
|---|
| 25 |
| # or |
|---|
| 26 |
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # a IP address |
|---|
| 27 |
) |
|---|
| 28 |
(:[0-9]+)? # a port (optional) |
|---|
| 29 |
(/?|/\S+) # a /, nothing or a / with something |
|---|
| 30 |
$~ix'; |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
* Available options: |
|---|
| 34 |
* |
|---|
| 35 |
* * protocols: An array of acceptable URL protocols (http, https, ftp and ftps by default) |
|---|
| 36 |
* |
|---|
| 37 |
* @param array $options An array of options |
|---|
| 38 |
* @param array $messages An array of error messages |
|---|
| 39 |
* |
|---|
| 40 |
* @see sfValidatorRegex |
|---|
| 41 |
*/ |
|---|
| 42 |
protected function configure($options = array(), $messages = array()) |
|---|
| 43 |
{ |
|---|
| 44 |
parent::configure($options, $messages); |
|---|
| 45 |
|
|---|
| 46 |
$this->addOption('protocols', array('http', 'https', 'ftp', 'ftps')); |
|---|
| 47 |
$this->setOption('pattern', new sfCallable(array($this, 'generateRegex'))); |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
* Generates the current validator's regular expression. |
|---|
| 52 |
* |
|---|
| 53 |
* @return string |
|---|
| 54 |
*/ |
|---|
| 55 |
public function generateRegex() |
|---|
| 56 |
{ |
|---|
| 57 |
return sprintf(self::REGEX_URL_FORMAT, implode('|', $this->getOption('protocols'))); |
|---|
| 58 |
} |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|