Php 在验证器中使用约束
我正在使用symfony2验证程序验证表单和API POST请求 我的验证器是这样做的:Php 在验证器中使用约束,php,validation,symfony,Php,Validation,Symfony,我正在使用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 val
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();
}
}
第一个回调和开关与my form一起工作,但是当使用第二个回调时,symfony返回一个错误
约束Symfony\Component\Validator\Constraints\Ip中不存在选项“type”
我不明白我的约束有什么问题。
为什么这个错误会说我的ip约束没有类型
选项
我怎样才能解决这个问题?
我可能无法在验证器中使用ip
约束
谢谢约束没有类型
属性。
如果需要,应该扩展
Symfony\Component\Validator\Constraints\Ip
我不需要这个属性,我甚至不知道这个属性来自哪里。当我在另一个课堂上使用它时,它工作得很好。我不明白为什么只有在这门课上我才会犯这个错误