Skip to content

@Assert\Valid() doesn't work with groups for more than one level of embeddance (Symfony 3.4) #24246

Closed
@kate-kate

Description

@kate-kate
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.4.*@dev

Problem description

I've updated to 3.4 version to try using @Assert\Valid() with groups and found a bug.
For example I have 3 entities: Contract, System, Part.

Contract has embedded Systems collection and "contract_valid" group. So it has systems field annotated like that:

    /**
     * @var System[]|ArrayCollection
     *
     * @Assert\Valid(groups={"contract_valid"})
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\System", mappedBy="contract", cascade={"persist", "remove"})
     */
    private $systems;

System has embedded Parts collection and validation group "system_valid". And It has unique serialNumber

/**
 * System
 *
 * @ORM\Table(name="system")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\SystemRepository")
 *
 * @UniqueEntity("serialNumber", groups={"system_valid", "contract_valid"})
 */
class System
{
    /**
     * @var string
     *
     * @Assert\NotBlank(groups={"system_valid", "contract_valid"})
     * @ORM\Column(name="serialNumber", type="string", length=255, unique=true)
     */
    private $serialNumber;

    /**
     * @var Part[]|ArrayCollection
     *
     * @Assert\Valid(groups={"system_valid", "contract_valid"})
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Part", mappedBy="system", cascade={"persist", "remove"})
     */
    private $parts;

And Part itself has no embedded entities, but it also has unique field serialNumber, which is validated for "contract_valid", "system_valid" and its own "part_valid" validation groups

/**
 * Part
 *
 * @ORM\Table(name="part")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PartRepository")
 *
 * @UniqueEntity("serialNumber", groups={"part_valid", "system_valid", "contract_valid"})
 */
class Part
{
   
    /**
     * @var string
     *
     * @Assert\NotBlank(groups={"part_valid", "system_valid", "contract_valid"})
     * @ORM\Column(name="serialNumber", type="string", length=255, unique=true)
     */
    private $serialNumber;

The problem is the following: when I try to save System with not unique Part serialNumber - validation error will be thrown successfully. But if I try to create Contract with not unique System serialNumber, I see PDO Exception instead of validation error:
2017-09-18 15 24 51

Works the same way when I try to create (or update) Contract with valid System and not unique Part serialNumber:
2017-09-18 15 35 03

Also, when I remove "contract_valid" group from Part annotations and from System parts @Assert\Valid(), it also works well for System (and obviously doesn't works for Part at all).

I can create repository with represented bug, if helps.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions