views:

30

answers:

2

A question about symfony form validators. For example I have an url field. If the user leave this field empty, it is OK. But if the user provide an url, he should provide a valid one.

I tried $this->setValidator('url', new sfValidatorUrl(array(), array('invalid' => 'invalid url')));. But it doesn't permit empty values. (which is not the desired behavior)

In another word. I need a Null-Or-Valid validator. I prefer not to write a new validator myself. I think I should be able to combine some validators to achieve my goal.

+1  A: 

Hello, just set the "required" option to false (by default, it is true).

$this->setValidator('url', 
  new sfValidatorUrl(array('required' => false), array(
    'invalid'  => 'invalid url')));
greg0ire
Required should be in the first array. It is an option not a message.
jeremy
My bad. fixed, thank you
greg0ire
A: 

Try the URL validator I wrote. It not only validates but sanitizes.

http://jasonswett.net/how-to-validate-and-sanitize-a-url-in-symfony/

Jason Swett