Skip to content

[DoctrineBridge][Validator] Expose all fields for message validation failure #38969

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

Open
wants to merge 4 commits into
base: 7.4
Choose a base branch
from

Conversation

ahmed-bhs
Copy link
Contributor

@ahmed-bhs ahmed-bhs commented Nov 2, 2020

Q A
Branch? 5.x
Bug fix? no
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR In progress

The main reason of the pull request is to improve the way UniqueEntity validator provide invalide values for us.
Unfortunately, if we use the validator with more than one field, we will only have the first value at our disposal to customize the message that’s displayed when the constraint fails, cause, according to this line:
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]]; the message is always mapped to the first field causing the violation.

Imagine we have an entity Dummy class, and we post a json data that looks like this: {"foo": "space", "bar": "github"}.

/**
 * @ORM\Entity()
 * @UniqueEntity(fields={ "foo", "bar"}, message="Registration failed, the foo *{{ foo }}* is already exist for *{{ bar }}*")
 */
class Dummy
{
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $foo;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $bar;
..
}

The result that will be displayed is: Registration failed, the foo *space* is already exist for *github*" and that's more specific and friendly.

Any way, just let me know if this requires to add some unit tests or some documentation, (if it'is an interesting idea).
Best regards,

@fabpot fabpot modified the milestones: 5.4, 6.1 Nov 16, 2021
@fabpot fabpot modified the milestones: 6.1, 6.2 May 20, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants