我正在使用symfony2验证器来验证表单和API POST请求。
我把我的验证器做了这样:
public function validate($content, Constraint $constraint)
{
if (in_array($constraint->type, self::DNS_TYPE, true) === false) {
$this->context->buildViolation('This DNS type is not valid.')
->atPath($constraint->type)
->addViolation();
}
switch ($constraint->type) {
case 'A':
$contentConstraint = new Assert'Ip(['version' => '4']);
break;
case 'AAAA':
$contentConstraint = new Assert'Ip(['version' => '6']);
break;
case 'CNAME':
case 'NS':
case 'MX':
$contentConstraint = new Assert'Regex(['pattern' => '/^[[:alnum:]-'._]+$/u']);
break;
default:
$contentConstraint = false;
break;
}
// This part don't work
if ($this->validate('1.1.1.1', $contentConstraint)) {
$this->context->buildViolation('his value is not correct.')
->atPath($content)
->addViolation();
}
}
第一个回调和开关适用于我的表单,但是当使用第二个回调时,symfony返回错误
选项"type"在约束Symfony''Component''Validator''Constraint''Ip 中不存在
我不明白我的约束有什么问题。为什么此错误说我的 ip 约束没有type
选项?
我该如何解决这个问题?我也许不能在验证器中使用ip
约束?
谢谢
约束
http://api.symfony.com/2.3/Symfony/Component/Validator/Constraints/Ip.html 没有 type
属性。如果需要,应扩展Symfony'Component'Validator'Constraints'Ip