我正在使用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会返回错误
约束Symfony \ Component \ Validator \ Constraints \ Ip
中不存在选项“type”
我不明白我的约束有什么问题。
为什么这个错误说我的ip约束没有type
选项?
我该如何解决这个问题?
我可以不在验证器中使用ip
约束吗?
由于
答案 0 :(得分:1)
约束http://api.symfony.com/2.3/Symfony/Component/Validator/Constraints/Ip.html没有type
属性。
如果您需要,您应该扩展Symfony\Component\Validator\Constraints\Ip