Reputation: 53
$this->setValidator('website', new sfValidatorAnd(array(
$this->validatorSchema['website'],
new sfValidatorUrl(array(), array(
'invalid' => 'This is not website',
)),
)));
this validate http://google.com, but google.com no. How can i this edit for validate without http:// ?
Upvotes: 5
Views: 6337
Reputation: 1888
I am afraid you will need to create your own custom validator:
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')));
}
}
Here ((%s)://)?
mean that now protocol is optional.
See sfValidatorUrl for an original pattern (REGEX_URL_FORMAT const).
Upvotes: 4
Reputation: 20045
For validating you could use the native PHP function filter_var with the flag FILTER_VALIDATE_URL.
Upvotes: 1
Reputation: 715
just set the "required" option to false (by default, it is true).
$this->setValidator('url',
new sfValidatorUrl(array('required' => false), array(
'invalid' => 'invalid url')));
Upvotes: -1