UrlValidator Symfony "the options "“不存在”

UrlValidator Symfony "the options "" do not exist"

我正在尝试使用 symfony2 UrlValidator 来验证我创建的服务中的 url 参数。我试过了:

    $urlToValidate = 'http://www.google.ch/';       
    $validator = new UrlValidator();
    $validator->validate($urlToValidate, new Url(array('value' => $urlToValidate)));

但我收到错误消息:

[Symfony\Component\Validator\Exception\InvalidOptionsException] The options "" do not exist in constraint Symfony\Component\Validator\Constraints\Url

有办法吗?

编辑 1 :

我用过:

    $validator = new UrlValidator();
    $validator->validate($url, new Url());

但我得到:

[Error] Call to a member function buildViolation() on null

我找到了答案:

$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());
    }
}

如此处所述:http://symfony.com/doc/current/components/validator.html