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

[Validator] add groups support to the Valid constraint#21111

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

Merged
fabpot merged 1 commit intosymfony:3.4fromxabbuh:issue-3622
Aug 5, 2017

Conversation

xabbuh
Copy link
Member

@xabbuhxabbuh commentedDec 31, 2016
edited
Loading

QA
Branch?3.4
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#3622,#17622
LicenseMIT
Doc PRTODO

tigitz, Koc, yceruto, and samnela reacted with hooray emojifancyweb, rvanlaak, wesleylancel, DavidBadura, liverbool, jlamur, ricardosaracino, NoResponseMate, yceruto, and stollr reacted with heart emoji
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
}

$violations = $this->context->getValidator()->validate($value, null, array($this->context->getGroup()));
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I am not completely sure if handling the traversal really should be done here or if we should better add some special handling for theValid constraint to theRecursiveContextualValidator instead. Any opinions are warmly welcome.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks clean to me like this.

@kgilden
Copy link
Contributor

kgilden commentedDec 31, 2016
edited
Loading

@xabbuh awesome! I confirmed it works as would validation groups work for every other constraint, i.e. invalidation.xml you add groups toValid and now pass thevalidation_groups option when creating the form.Valid will only be applied if any of the groups invalidation_groups equals those specified invalidation.xml.

However, I do think there is one super useful use case not covered. Consider aProfile object havingprofilePicture andcoverPhoto properties - both of typeImage, which internally has afile property of typeFile. Let's say we have different maximum file sizes for both of them, so we add 2File constraints to theImage object. And both properties should be valid at all times forProfile.

<classname="Profile">  <propertyname="profilePicture">    <constraintname="Valid" />  </property>  <propertyname="coverPhoto">    <constraintname="Valid" />  </property></class><classname="Image">  <propertyname="file">    <constraintname="File">      <optionname="maxSize">100k</option>      <optionname="groups">        <value>profile-picture</value>      </option>    </constraint>    <constraintname="File">      <optionname="maxSize">1000k</option>      <optionname="groups">        <value>cover-photo</value>      </option>    </constraint>  </property></class>

InProfileType we'd have something like this probably.

$builder->add('profilePicture', ImageType::class, ['validation_groups' => ['profile-picture']]);$builder->add('coverPhoto', ImageType::class, ['validation_groups' => ['cover-photo']);

But try as you might, it seems to me that validation for theseFile objects does not kick in.

$form =$factory->create(ProfileType::class);$form->handleRequest($request);$form->isValid();// returns true for files larger than what was specified

Of course this is a bit nonsensical example, but I tried to simplify as much as possible. In one of my projects we for example haveDocument objects which wrap all files and in one of the entities one property can contain only XML files and the other one can accept a different whitelist of types.

@xabbuh
Copy link
MemberAuthor

@kgilden This seems logical to me as the root form itself is validated in theDefault group. Configurationvalidation_groups for the root form to be, for example,['Default', 'profile-picture', 'cover-photo'] should do the trick.

@kgilden
Copy link
Contributor

@xabbuh ahh yes, sorry for the confusion. I should note that this patch isstill required to make my example above work. The same groups must also be added toValid constraints and then it works as expected.

@xabbuh
Copy link
MemberAuthor

@kgilden You are right. But IMO this behaviour is straight-forward with how validation groups in Symfony work in general. Do you disagree with that?

@xabbuhxabbuh added this to the3.x milestoneDec 31, 2016
@kgilden
Copy link
Contributor

kgilden commentedDec 31, 2016
edited
Loading

@xabbuh it's straight-forward if for example theFile constraint is applied to two distinct properties on the root object, i.e.

<classname="Profile">  <propertyname="profilePicture">    <constraintname="File"><!-- distinct options-->    </constraint>  </property>  <propertyname="coverPhoto">    <constraintname="File"><!-- distinct options-->    </constraint>  </property></class>

What I think is currently not possible is when you want to validate the same objects deeper. After this PR it's possible to do as follows.

<classname="Profile">  <propertyname="profilePicture">    <constraintname="Valid">      <optionname="groups">        <value>profile-picture</value>      </option>    </constraint>  </property>  <propertyname="coverPhoto">    <constraintname="Valid">      <optionname="groups">        <value>cover-photo</value>      </option>    </constraint>  </property></class><classname="Image">  <propertyname="file">    <constraintname="File">      <optionname="maxSize">100k</option>      <optionname="groups">        <value>profile-picture</value>      </option>    </constraint>    <constraintname="File">      <optionname="maxSize">1000k</option>      <optionname="groups">        <value>cover-photo</value>      </option>    </constraint>  </property></class>
class ProfileTypeextends AbstractType{publicfunctionbuildForm(FormBuilderInterface$builder,array$options)    {$builder->add('profilePicture', ImageType::class);$builder->add('coverPhoto', ImageType::class);    }publicfunctionconfigureOptions(OptionsResolver$resolver)    {$resolver->setDefaults(['validation_groups' => ['Default','cover-photo','profile-picture']]);    }}

Correct me if I'm wrong, but I don't think there was a way to do it prior to this PR.

@xabbuh
Copy link
MemberAuthor

Indeed, that's not possible right now.

kgilden reacted with thumbs up emoji

@pourquoi
Copy link

+1 for this feature. The use case described by kgilden happens regularly for me (deep cascade validation groups)

@fabpot
Copy link
Member

@xabbuh Can you add a note in the CHANGELOG before I can merge?

@xabbuh
Copy link
MemberAuthor

@fabpot done, but are we sure that this is the right way to implement it?

ricardosaracino reacted with thumbs up emoji

@xabbuhxabbuh changed the base branch frommaster to3.4May 18, 2017 07:16
@xabbuh
Copy link
MemberAuthor

ping @symfony/deciders

throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
}

$violations = $this->context->getValidator()->validate($value, null, array($this->context->getGroup()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks clean to me like this.

@fabpot
Copy link
Member

Thank you@xabbuh.

@fabpotfabpot merged commit0ca27cc intosymfony:3.4Aug 5, 2017
fabpot added a commit that referenced this pull requestAug 5, 2017
… (xabbuh)This PR was merged into the 3.4 branch.Discussion----------[Validator] add groups support to the Valid constraint| Q             | A| ------------- | ---| Branch?       | 3.4| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#3622,#17622| License       | MIT| Doc PR        | TODOCommits-------0ca27cc add groups support to the Valid constraint
@xabbuhxabbuh deleted the issue-3622 branchAugust 5, 2017 17:42
This was referencedOct 18, 2017
@Atiragram
Copy link

Hi,
What if value is null? i need@Assert\Valid only after and when@Assert\NotNull is valid, currently i get 500 error because of that empty value

@ogizanagi
Copy link
Contributor

@Atiragram : This is already supposed to be fixed in#25297, available since 3.4.1. If you still encounter an issue, please open a new issue with a reproducer or a failing test case. Thanks!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@HeahDudeHeahDudeHeahDude approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
3.4
Development

Successfully merging this pull request may close these issues.

9 participants
@xabbuh@kgilden@pourquoi@fabpot@Atiragram@ogizanagi@nicolas-grekas@HeahDude@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp