Valid limit does not support group options in symfony2

I need to perform cascading confirmation in the form of symfony2, if only for the specified group.

here the symfony team reported that the group option is not supported in the actual restriction https://github.com/symfony/symfony/issues/4893

How to do it?

More details:

I have a User Entity property, which is a foreign key for the Address Entity. In addition, I have an Entity called a business that has a User as property, as well as an Address property. I need to check the address for the user, but without checking it when the User is a Business property ...

Scheme

Class Address {
    ...
}

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

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

Therefore, I need to check the address inside the user only for custom forms, but not for business.

thank

+3
3

(Symfony 3).

UserInfo , Place. .

.

atemt Callback . . . .

. . .

/**
 * @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();
        }
    }
}
+5

, . "groups", . .

PHP- , .

// ...
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
    $metadata->addConstraint(new Assert\Callback(array(
            'methods' => array('validAddress'),
            'groups' => array('user'),
        )));
}

public function validAddress(ExecutionContextInterface $context)
{
    $context->validate($this->address, 'address', $context->getGroup());
}

-

// ...
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
    $metadata->addConstraint(new Assert\Callback(array(
            'methods' => array('validUser'),
            'groups' => array('business'),
        )));
    $metadata->addConstraint(new Assert\Callback(array(
            'methods' => array('validAddress'),
            'groups' => array('business'),
        )));
}

public function validUser(ExecutionContextInterface $context)
{
    $context->validate($this->user, 'user', $context->getGroup());
}

public function validAddress(ExecutionContextInterface $context)
{
    $context->validate($this->address, 'address', $context->getGroup());
}

PS ,

+3

You can add your validation rules to your UserTypeType. Remember to delete your annotation.

Follow this link to learn more about validation in form type.

0
source

All Articles