I'm trying to use symfony2 UrlValidator to validate an url parameters in a Service that I created. I tried this :
$urlToValidate = 'http://www.google.ch/';
$validator = new UrlValidator();
$validator->validate($urlToValidate, new Url(array('value' => $urlToValidate)));
But I get error :
[Symfony\Component\Validator\Exception\InvalidOptionsException] The options "" do not exist in constraint Symfony\Component\Validator\Constraints\Url
Is there a way ?
Edit 1 :
I used :
$validator = new UrlValidator();
$validator->validate($url, new Url());
But I get :
[Error] Call to a member function buildViolation() on null
I found the answer :
$url = 'http://malformedurl/';
$validator = Validation::createValidator();
$violations = $validator->validate($url, new Url());
if (0 !== count($violations)) {
// there is an error !
foreach ($violations as $violation) {
throw new Exception($violation->getMessage());
}
}
As described here : http://symfony.com/doc/current/components/validator.html