我正在尝试使用symfony2 UrlValidator来验证我创建的服务中的url参数。我试过这个:
$urlToValidate = 'http://www.google.ch/';
$validator = new UrlValidator();
$validator->validate($urlToValidate, new Url(array('value' => $urlToValidate)));
但我得到错误:
[Symfony \ Component \ Validator \ Exception \ InvalidOptionsException] 选项“”不存在于约束中 的Symfony \组件\验证\约束\地址
有办法吗?
修改1:
我用过:
$validator = new UrlValidator();
$validator->validate($url, new Url());
但我明白了:
[错误]在null
上调用成员函数buildViolation()
答案 0 :(得分:0)
我找到了答案:
$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