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

[Form] Add constraints_from_* options#50459

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
tasiot wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromtasiot:feature/constraints-from

Conversation

tasiot
Copy link

@tasiottasiot commentedMay 28, 2023
edited by nicolas-grekas
Loading

Apply constraints to field from an entity not mapped by the form data_class.

QA
Branch?6.4
Bug fix?no
New feature?yes
Deprecations?no
Ticketsno
LicenseMIT
Doc PRsymfony/symfony-docs#...

Use-cases:

  • DTO form like user registration with many entities concerned (user / address)

Current usage:
You set constraints in your entities then create a DTO but you must duplicate your constraints in your DTO object to valide your fields.

New usage:
You set constraints in your entities then create a DTO and link your DTO form fields to your entities properties.

Usage:

Addconstraints_from_entity option on any form field (child ofFormType::class).
If the entity related property name is different that the field name, you must provideconstraints_from_property to declare the property name.

Works on (un)mapped field and (un)setdata_class form.

<?php// App/Form/UserRegistrationType.phpnamespaceApp\Form;useApp\Entity\User;useApp\Entity\Address;useApp\Form\Model\UserRegistration;useSymfony\Component\Form\AbstractType;useSymfony\Component\Form\Extension\Core\Type\TextType;useSymfony\Component\Form\FormBuilderInterface;useSymfony\Component\OptionsResolver\OptionsResolver;class UserRegistrationTypeextends AbstractType{publicfunctionbuildForm(FormBuilderInterface$builder,array$options):void    {$builder            ->add('username', TextType::class, ['label' =>'Your username','constraints_from_entity' => User::class,// Not required if property name is same as the field name// 'constraints_from_property' => 'username' // Constraints from User::username            ])            ->add('zipCode', TextType::class, ['label' =>'Your zip/postal code','constraints_from_entity' => Address::class,'constraints_from_property' =>'postalCode'// Constraints from Address::postalCode            ]);    }publicfunctionconfigureOptions(OptionsResolver$resolver):void    {$resolver->setDefaults(['data_class' => UserRegistration::class        ]);    }}

Notes:
It's my first pull request, I hope I done all required things ;)

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has acontribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (seehttps://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (seehttps://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "6.3" but it seems your PR description refers to branch "6.3 for features".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@tasiot
Copy link
Author

I think the target branch is 6.4 but she's behind the 6.3 so I didn't know which branch I must to target

@tasiottasiotforce-pushed thefeature/constraints-from branch 2 times, most recently froma16660d to017d72dCompareMay 28, 2023 23:41
@tasiot
Copy link
Author

I don't understand why the high-deps and low-deps unit tests fail…

Can anyone help me?

@nicolas-grekas
Copy link
Member

6.4 is the right target, we merge 6.3 into 6.4 regularly so it will catch up eventually.

@nicolas-grekasnicolas-grekas modified the milestones:6.3,6.4May 29, 2023
@nicolas-grekasnicolas-grekas changed the base branch from6.3 to6.4May 29, 2023 11:12
@nicolas-grekasnicolas-grekas changed the base branch from6.4 to6.3May 29, 2023 11:12
@tasiottasiotforce-pushed thefeature/constraints-from branch from017d72d to55cb5feCompareJune 7, 2023 09:52
@tasiottasiot marked this pull request as draftJune 7, 2023 10:02
@tasiottasiot changed the base branch from6.3 to6.4June 7, 2023 10:02
@tasiottasiot marked this pull request as ready for reviewJune 7, 2023 10:03
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has acontribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (seehttps://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (seehttps://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@@ -7,6 +7,7 @@ CHANGELOG
* Don't render seconds for HTML5 date pickers unless "with_seconds" is explicitly set
* Add a `placeholder_attr` option to `ChoiceType`
* Deprecate not configuring the "widget" option of date/time form types, it will default to "single_text" in v7
* Add `constraints_from_entity` and `constraints_from_property` option to `FormType`

Choose a reason for hiding this comment

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

for a new 6.4 section

tasiot reacted with thumbs up emoji
Apply constraints to field from an entity not mapped by the form data_class.Fix fabbot reviewFix changelog to 6.4
@tasiottasiotforce-pushed thefeature/constraints-from branch from55cb5fe tofbe76e9CompareJune 7, 2023 10:18
@tasiot
Copy link
Author

tasiot commentedJun 7, 2023
edited
Loading

The job low-deps fails because it cannot find the traitSymfony\Component\Form\Tests\VersionAwareTest inSymfony\Component\Form\Test\FormPerformanceTestCase.
Can anyone help me? I don't understand how to pass this test because I don't think I have any relation with these files…

@tasiot
Copy link
Author

Maybe the term "DTO" is not right, a form model will be more appropriate? An object who contains some properties to create some entities…

Sometimes, you can have many forms… A form with'data_class' => YourEntity::class and another one with'data_class' => YourDTO::class.

When you make a form to create/edit an entity, it's recommanded to set constraints in this entity instead of each form field, isn't it?
Why duplicate these constraints in your second form (with DTO object)?

With the optionconstraints_from_entity another form (DTO) or an unmapped field can also apply these constraints without needs to duplicate the entity field constraints in each form field.

Consider these entities (simplified):

Topic
name(string)
category(TopicCategory)
TopicCategory
name(string)
TopicMessage
topic(Topic)
message(text)

You can easily imagine a form to create the first message which asks all theses entity (a category, a name and a message) (=a DTO with these properties) and another form to create the next messages which only asks a message (=TopicMessage Entity).
Instead of duplicate your entities constraints to your DTO form, you only need to add option constraints_from_entity as below:

// With a DTO (data_class => App\Form\Model\FirstTopicMessage::class)$builder->add('category', EntityType::class, ['class' => TopicCategory::class])->add('name', TextType::class, ['constraints_from_entity' => Topic::class])->add('message', TextareaType::class, ['constraints_from_entity' => TopicMessage::class])

OR

// With unmapped fields (data_class => App\Entity\TopicMessage::class)$builder->add('category', EntityType::class, ['class' => TopicCategory::class,'mapped' =>false,])->add('name', TextType::class, ['constraints_from_entity' => Topic::class,'mapped' =>false,])->add('message', TextareaType::class)

@nicolas-grekasnicolas-grekas modified the milestones:6.4,7.1Nov 15, 2023
@xabbuhxabbuh modified the milestones:7.1,7.2May 15, 2024
@fabpotfabpot modified the milestones:7.2,7.3Nov 20, 2024
@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-grekasnicolas-grekas left review comments

@xabbuhxabbuhAwaiting requested review from xabbuhxabbuh is a code owner

@ycerutoycerutoAwaiting requested review from ycerutoyceruto is a code owner

@wouterjwouterjAwaiting requested review from wouterj

@chalasrchalasrAwaiting requested review from chalasr

@lyrixxlyrixxAwaiting requested review from lyrixx

@dunglasdunglasAwaiting requested review from dunglas

@kbondkbondAwaiting requested review from kbond

@OskarStarkOskarStarkAwaiting requested review from OskarStark

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

5 participants
@tasiot@carsonbot@nicolas-grekas@fabpot@xabbuh

[8]ページ先頭

©2009-2025 Movatter.jp