Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[MapRequestPayload] Allow usage of expressions for defining validation groups#58273

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Brajk19 wants to merge10 commits intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromBrajk19:request-payload-value-expression-groups

Conversation

Brajk19
Copy link
Contributor

@Brajk19Brajk19 commentedSep 16, 2024
edited
Loading

QA
Branch?7.3
Bug fix?no
New feature?yes
Deprecations?no
LicenseMIT

This PR adds option to use Expression for defining validation groups when using#[MapRequestPayload] and
#[MapQueryString]

Inspired by expression options when using#[IsGranted], request and args are available.

Example:
We have route which is used to update user info. EntityUser has propertytype which can beadmin orregular.
If user is admin, his password must be very strong and they must have image while regular user can have weaker password and image is not required.
Payload used in request:

finalreadonlyclass UpdateUserDTO{publicfunction__construct(        #[Assert\NotNull(message:'Admin user must have image', groups: ['admin'])]public ?string$image,        #[Assert\PasswordStrength(minScore:Assert\PasswordStrength::STRENGTH_MEDIUM, groups: ['regular'])]        #[Assert\PasswordStrength(minScore:Assert\PasswordStrength::STRENGTH_VERY_STRONG, groups: ['admin'])]publicstring$password,    ) {    }}

We need to dynamically determine which validation group to use based on context (User that is being updated).
Current way of doing this is to manually callvalidate method:

$violations =$this->validator->validate($dto, groups: [$user->getType()]);// handle violations...

This PR allows usage of Expression with access to arguments so above code can be replaced with#[MapRequestPayload]

#[Route('/user/{id}', methods: ['PUT'])]publicfunction updateUserAction(User$user,    #[MapRequestPayload(        validationGroups: [newExpression('args["user"].getType()')],    )]UpdateUserDTO$dto)

HypeMC and OskarStark reacted with thumbs up emoji
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM, here are some minor comments

->set('controller.expression_language', ExpressionLanguage::class)
->args([service('cache.request_payload_value_resolver_expression_language')->nullOnInvalid()])

->set('cache.request_payload_value_resolver_expression_language')

Choose a reason for hiding this comment

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

either the EL service is named like proposed and this needs to ba aligned, or we change the name of the EL service, but the current mix looks inconsistent to me

Suggested change
->set('cache.request_payload_value_resolver_expression_language')
->set('cache.controller_expression_language')

Copy link
ContributorAuthor

@Brajk19Brajk19Oct 9, 2024
edited
Loading

Choose a reason for hiding this comment

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

@nicolas-grekas true. i prefer suggested change.
fixed550c8a4

@Brajk19Brajk19force-pushed therequest-payload-value-expression-groups branch from34d63e6 to4af5a37CompareOctober 9, 2024 18:41
@fabpotfabpot modified the milestones:7.2,7.3Nov 20, 2024
@Brajk19Brajk19force-pushed therequest-payload-value-expression-groups branch from4af5a37 to14189d2CompareJanuary 3, 2025 10:24
@Brajk19Brajk19force-pushed therequest-payload-value-expression-groups branch from5ebb49b to1f9de26CompareJanuary 10, 2025 11:17
@Brajk19Brajk19force-pushed therequest-payload-value-expression-groups branch from1f9de26 to29d53f3CompareJanuary 23, 2025 14:25
@Brajk19Brajk19force-pushed therequest-payload-value-expression-groups branch from29d53f3 to85204a5CompareMarch 7, 2025 21:14
@OskarStark
Copy link
Contributor

It looks like your committer email is not associated with your GitHub account, you may want to change that for credibility 🖖

@Brajk19
Copy link
ContributorAuthor

It looks like your committer email is not associated with your GitHub account, you may want to change that for credibility 🖖

@OskarStark thanks for advice, i fixed that 😄

@Brajk19Brajk19force-pushed therequest-payload-value-expression-groups branch from85204a5 to67eb8ecCompareMarch 23, 2025 08:35
@fabpotfabpot modified the milestones:7.3,7.4May 26, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasAwaiting requested review from nicolas-grekas

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

5 participants
@Brajk19@OskarStark@nicolas-grekas@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp