I'm using symfony2 validator to validate a form and an API POST request.
I have made my validator like this :
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();
}
}
The first callback and the switch work with my form but when the second callback is used, symfony return an error
The option "type" does not exist in constraint Symfony\Component\Validator\Constraints\Ip
I don't understand what is wrong with my constraint.
Why this error say that my ip constraint don't have the type
option ?
How can I fix this ?
I can maybe not use ip
constraint in a validator ?
Thanks