Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kate-kate opened this issue Sep 18, 2017 · 9 comments

Comments

@kate-kate
Copy link

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.

@kate-kate kate-kate changed the title @Assert\Valid() works with groups only for the first level of embeddance @Assert\Valid() works with groups only for the first level of embeddance (Symfony 3.4) Sep 18, 2017
@kate-kate kate-kate changed the title @Assert\Valid() works with groups only for the first level of embeddance (Symfony 3.4) @Assert\Valid() doesn't work with groups for more than one level of embeddance (Symfony 3.4) Sep 18, 2017
@xabbuh
Copy link
Member

xabbuh commented Sep 19, 2017

Having a small example project allowing to reproduce your issue would indeed be very helpful (I guess you won't need to set up any persistence layer as the issue should be reproducible using other constraints too, right?).

@kate-kate
Copy link
Author

Hello, I've created the project
There are three simple entity classes without persistence layer and I used NotBlank and Length constraints to reproduce issue.
In /part and /system routes validation works well. Problem is seen at /contract

@xabbuh
Copy link
Member

xabbuh commented Nov 15, 2017

Status: Reviewed

@xabbuh
Copy link
Member

xabbuh commented Nov 15, 2017

@kate-kate Thanks for the great reproducer. I was able to identify the issue. Can you confirm that #24983 does fix it for you?

@KamilPesek
Copy link

I think I have same problem as @kate-kate. I have constraint defined like

/**
     * @ORM\ManyToOne(targetEntity="Address", cascade={"persist"})
     * @Assert\Valid(
     *     groups={"extendedPersonMailingAddressNotSame"}
     * )
     */
    private $mailingAddress = null;

and then, I have group sequence validator


    /**
     *
     * Set conditional validation groups
     *
     * @return array
     */
    public function getGroupSequence()
    {

        if (!$this->mailingAddressSameAsPermanent) {
            $groups[] = 'extendedPersonMailingAddressNotSame';
        }

        return [$groups];  //this returns   ['extendedPersonMailingAddressNotSame']
    }

If I remove 'groups={"extendedPersonMailingAddressNotSame"}', everything works.
It also works when used with NotBlank

@Assert\NotBlank(
  groups={"extendedPersonMailingAddressNotSame"}
)

Unfortunatelly #24983 doesn't work for me.

@xabbuh
Copy link
Member

xabbuh commented Nov 16, 2017

@KamilPesek I pushed another update to the PR. Can you check that too?

stof added a commit that referenced this issue Nov 17, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] enter the context in which to validate

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24246
| License       | MIT
| Doc PR        |

Commits
-------

7359cbe [Validator] enter the context in which to validate
@xabbuh xabbuh closed this as completed Nov 17, 2017
@KamilPesek
Copy link

Unfortunately no change for me. It still doesn't work in 3.4.3.
And I am pretty sure that I have everything right, because:

  1. when i remove groups from
@Assert\Valid(
     groups={"extendedPersonMailingAddressNotSame"}
  )

it works
2) it also works with any other validation constraint, eg. NotBlank

Any suggestions?

@xabbuh
Copy link
Member

xabbuh commented Jan 8, 2018

@KamilPesek Please open a new issue and best also provide an example application that allows to reproduce your issue.

@KamilPesek
Copy link

@xabbuh oppened here #25726

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants