Skip to content

Unique entity custom message #16345

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
wants to merge 1 commit into from
Closed

Unique entity custom message #16345

wants to merge 1 commit into from

Conversation

jamyouss
Copy link

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

Add the possibility to have custom message for unique entity constraint.
For example if we have a collection of entities in a form, the message can be more expressive.

Example:

/**
 * Town
 *
 * @ORM\Entity()
 * @UniqueEntity(fields={"city"}, message="The city '{{ city }}' is already used.")
 */
class Town
{

Reopen PR #15201 based now on branch 2.8

@jamyouss
Copy link
Author

ping @stof @xabbuh

@fabpot
Copy link
Member

fabpot commented Oct 29, 2015

Looks like there is a problem here as many commits are not related to the changes made here.

@jamyouss
Copy link
Author

@fabpot Yes my PR was based on 2.6 but had some travis errors not related to my changes.
That why i merge the 2.8 branch into it and open a new PR.

Do i need to do a new PR ? (a clean PR)

@xabbuh
Copy link
Member

xabbuh commented Oct 29, 2015

@Razmo You can rebase your changes on the current 2.8 branch and do a forced push to your branch afterwards. Your pull request will be automatically synced.

@jamyouss
Copy link
Author

@xabbuh Thanks, it works 👍

@nicolas-grekas
Copy link
Member

Please do not merge but rebase instead. In fact, can you squash your commits in one please?
(or in two in your case, one for each author)

@jamyouss
Copy link
Author

jamyouss commented Dec 9, 2015

Ok going to squash my commits in one (there is only one author, just done a mistake on my config for first commits) and i'll rebase on 2.8.

@jamyouss
Copy link
Author

jamyouss commented Dec 9, 2015

@nicolas-grekas done

@@ -135,13 +136,29 @@ public function validate($entity, Constraint $constraint)
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]];

$vars = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not necessary, is it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, it is useless

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to remove it

@xabbuh
Copy link
Member

xabbuh commented Dec 10, 2015

👍 LGTM

if (preg_match_all('/{{ ([a-zA-Z0-9_]+) }}/', $constraint->message, $vars) > 0) {

if (!class_exists('Symfony\\Component\\PropertyAccess\\PropertyAccess')) {
throw new \RuntimeException('Unable to access entity property as the Symfony PropertyAccess is not installed.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably explain why it is necessary

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PropertyAccess component should be ship with standard edition right ? Why this check ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dupuchba You can use the Doctrine bridge without using the full framework.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I am not sure if it's widespread but fair enough ! Thx @xabbuh

@fabpot
Copy link
Member

fabpot commented Jan 27, 2016

Any news on this one?

@jamyouss
Copy link
Author

@fabpot : I'm waiting for @stof explanations of his point (with some examples) because i didn't understand:

this check is a bad idea: when using translation keys, the params are needed, but they are not part of the key

May be you can example me his point ?

@jamyouss
Copy link
Author

Ping @stof

@fabpot
Copy link
Member

fabpot commented Mar 2, 2016

@Razmo $constraint->message can be two different things, the message itself or a key that can be used to retrieve the real message. For the latter, the key never contains the parameters (blog.title vs Blog {{ title }}).

@fabpot
Copy link
Member

fabpot commented Jun 13, 2016

@jamyouss Do you need help to finish this one?

@jamyouss
Copy link
Author

@fabpot Yes please.
I only see one solution. Inject the translator in UniqueEntityValidator class, translate the message and make the replacement after. By this way we make the replacement on the final message. The drawback is that ConstraintViolationBuilder already do the translation.

Do you have another idea ?

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 6, 2016
@nicolas-grekas
Copy link
Member

@jamyouss would you mind rebasing on branch 3.4 and changing the target branch on github also to 3.4?

@jamyouss jamyouss changed the base branch from 2.8 to 3.4 August 6, 2017 14:36
@nicolas-grekas nicolas-grekas modified the milestones: 3.4, 4.1 Sep 28, 2017
@nicolas-grekas nicolas-grekas modified the milestones: 4.1, next Apr 20, 2018
@fabpot
Copy link
Member

fabpot commented Feb 3, 2020

Closing as this PR is stale and it cannot be merged as is anyway.

@fabpot fabpot closed this Feb 3, 2020
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
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.

8 participants