Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlValidator Symfony "the options "" do not exist"

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

like image 473
Xero Avatar asked Jan 25 '26 11:01

Xero


1 Answers

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

like image 87
Xero Avatar answered Jan 27 '26 01:01

Xero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!