Skip to content

[Validator] Choices constraint improvement #29658

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

Merged
merged 1 commit into from
Jan 2, 2019
Merged

[Validator] Choices constraint improvement #29658

merged 1 commit into from
Jan 2, 2019

Conversation

nikophil
Copy link
Contributor

@nikophil nikophil commented Dec 20, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT

Hi,

here is a little improvement for the choice constraint: expose a new choices wildcard for the messages, in order to provide a way to display easily the valid choices to the user.

thanks.

@HeahDude
Copy link
Contributor

HeahDude commented Dec 20, 2018

This can be closed as this feature is supported by the annotation reader already, just remove the quotes :).

 /**
     * @var string
     *
     * @Assert\Choice(choices=App\Constants::AVAILABLE_STATES)
     */
    private $state;

@yceruto
Copy link
Member

yceruto commented Dec 20, 2018

@HeahDude Do you meant @Assert\Choice(choices=App\Constants::AVAILABLE_STATES)?

@HeahDude
Copy link
Contributor

yes x).

@@ -100,6 +116,7 @@ public function validate($value, Constraint $constraint)
} elseif (!\in_array($value, $choices, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ choices }}', implode(', ', $choices))
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah this line is still a nice addition indeed!

@nikophil
Copy link
Contributor Author

yeah indeed, you're right, and i 've learnt something new :)
will we keep the new wildcard choices i've added, or should i close the PR ?

@HeahDude
Copy link
Contributor

I'm 👍 for supporting a default choices implode, as long as we don't change the default message for both BC and let it opt-in performance wise.

@nikophil
Copy link
Contributor Author

i've updated the PR

@@ -100,6 +100,7 @@ public function validate($value, Constraint $constraint)
} elseif (!\in_array($value, $choices, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ choices }}', implode(', ', $choices))
Copy link
Contributor

Choose a reason for hiding this comment

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

should also be added to the multiple=true case

choices can be any value, i think we need to map all choices using formatValue() also.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, you're totally right - i've added to the multiple=true case and formatted the value

@nicolas-grekas nicolas-grekas added this to the next milestone Dec 24, 2018
@xabbuh
Copy link
Member

xabbuh commented Dec 27, 2018

@nikophil Can you please update the PR title and description to match the recent changes (they will be part of the git history)?

@nikophil
Copy link
Contributor Author

done 👍

@fabpot
Copy link
Member

fabpot commented Jan 2, 2019

Thank you @nikophil.

@fabpot fabpot merged commit 71dfa35 into symfony:master Jan 2, 2019
fabpot added a commit that referenced this pull request Jan 2, 2019
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Validator] Choices constraint improvement

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

Hi,

here is a little improvement for the choice constraint:  expose a new `choices` wildcard for the messages, in order to provide a way to display easily the valid choices to the user.

thanks.

Commits
-------

71dfa35 add new `choices` wildcard in message
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Jan 3, 2019
…int (nikophil)

This PR was submitted for the 3.4 branch but it was merged into the master branch instead (closes #10836).

Discussion
----------

document "choices" wildcard in Choice validation constraint

Hello,

here is a little doc update in order to document the `choices` wildcard introduced by symfony/symfony#29658

Commits
-------

7764519 document "choices" wildcard in Choice validation constraint
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Mar 7, 2019
…ation (nikophil)

This PR was squashed before being merged into the 3.4 branch (closes #10810).

Discussion
----------

[Validator] Explained how to use array constant in annotation

Hi,

few days ago i tried to add a new feature to the `choice` validator constraint, by adding a new `choicesFromConstant` option.
symfony/symfony#29658
But then, i realized this was already supported.

I think this should be documented, as i think a lot of people don't know this feature.

thanks.

Commits
-------

7774aab [Validator] Explained how to use array constant in annotation
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
@fabpot fabpot mentioned this pull request May 9, 2019
przemyslaw-bogusz added a commit to przemyslaw-bogusz/symfony-docs that referenced this pull request Feb 8, 2020
javiereguiluz pushed a commit to przemyslaw-bogusz/symfony-docs that referenced this pull request Feb 10, 2020
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Feb 10, 2020
…s (przemyslaw-bogusz)

This PR was submitted for the 5.0 branch but it was merged into the 4.4 branch instead (closes #13081).

Discussion
----------

[Constraints][Choice] Added choices to message parameters

It was added in this PR:
symfony/symfony#29658

<!--

If your pull request fixes a BUG, use the oldest maintained branch that contains
the bug (see https://symfony.com/roadmap for the list of maintained branches).

If your pull request documents a NEW FEATURE, use the same Symfony branch where
the feature was introduced (and `master` for features of unreleased versions).

-->

Commits
-------

3ba67f7 Added choices to message parameters
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