Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

valid constraint does not support group options in symfony2

I need to cascade validation in symfony2 form unless for specified group.

here symfony team told that group option is not supported in Valid constraint https://github.com/symfony/symfony/issues/4893

How to do it ?

Details:

I have a User Entity has address property which is a foreign key to Address Entity. Also i have Entity called business having User as property and also Address property. I need to validate address for User but, without validating it When User is a property of Business...

Schema

Class Address {
    ...
}

Class User {
     /**
     * @Assert\Valid(groups={"user"})
     */
     private $address;
}

Class Business {
    /**
     * @Assert\Valid(groups={"business"})
     */
     private $user;
    /**
     * @Assert\Valid(groups={"business"})
     */
     private $address;
}

So I need to validate The address inside User only for User Forms but not for Business.

Thank you

like image 538
skonsoft Avatar asked Dec 26 '22 13:12

skonsoft


1 Answers

I was faced with the same problem (Symfony 3).

I have a entity UserInfo with two fields linked to one entity Place. And I need to validate both fields in one case, and one field in another case.

And didn't want to move constraints into Form.

In first atemt I used a Callback constraint to check group and validate one or both fields. It was fine. But input fields in form wasn't marked as invalid. All errors was displayed at top of the form.

Then I simply created own validator. Thanks to this I can specify needed groups for each field. And all invalid input fields in form marked accordingly.

/**
 * @Annotation
 * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
 */
class ValidGroupAware extends Constraint
{
}

class ValidGroupAwareValidator extends ConstraintValidator
{

    /**
     * Checks if the passed value is valid.
     *
     * @param mixed $value The value that should be validated
     * @param Constraint $constraint The constraint for the validation
     */
    public function validate($value, Constraint $constraint)
    {
        if (!$constraint instanceof ValidGroupAware) {
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\ValidGroupAware');
        }

        $violations = $this->context->getValidator()->validate($value, [new Valid()], [$this->context->getGroup()]);
        /** @var ConstraintViolation[] $violations */
        foreach ($violations as $violation) {
            $this->context->buildViolation($violation->getMessage())
                ->setParameters($violation->getParameters())
                ->setCode($violation->getCode())
                ->setCause($violation->getCause())
                ->setPlural($violation->getPlural())
                ->setInvalidValue($violation->getInvalidValue())
                ->atPath($violation->getPropertyPath())
                ->addViolation();
        }
    }
}
like image 129
Gregsparrow Avatar answered Dec 29 '22 12:12

Gregsparrow