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][Doctrine] Add docs for automatic validation#11132

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

Closed
dunglas wants to merge5 commits intosymfony:4.3fromdunglas:pr_27735

Conversation

@dunglas
Copy link
Member

Copy link
Contributor

@teohhanhuiteohhanhui left a comment

Choose a reason for hiding this comment

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

versionadded is missing?

dunglas reacted with thumbs up emoji
@wouterjwouterj added the Waiting Code MergeDocs for features pending to be merged labelMar 16, 2019
@xabbuhxabbuh added this to thenext milestoneMar 22, 2019
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull requestMar 31, 2019
…alidation (dunglas)This PR was merged into the 4.3-dev branch.Discussion----------[Validator][DoctrineBridge][FWBundle] Automatic data validation| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes<!-- don't forget to update src/**/CHANGELOG.md files -->| BC breaks?    | no     <!-- seehttps://symfony.com/bc -->| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->| Fixed tickets | n/a   <!-- #-prefixed issue number(s), if any -->| License       | MIT| Doc PR        |symfony/symfony-docs#11132This feature automatically adds some validation constraints by inferring existing metadata. To do so, it uses the PropertyInfo component and Doctrine metadata, but it has been designed to be easily extendable.Example:```phpuse Doctrine\ORM\Mapping as ORM;/** *@Orm\Entity */class Dummy{    /**     *@Orm\Id     *@Orm\GeneratedValue(strategy="AUTO")     *@Orm\Column(type="integer")     */    public $id;    /**     *@Orm\Column(nullable=true)     */    public $columnNullable;    /**     *@Orm\Column(length=20)     */    public $columnLength;    /**     *@Orm\Column(unique=true)     */    public $columnUnique;}$manager = $this->managerRegistry->getManager();$manager->getRepository(Dummy::class);$firstOne = new Dummy();$firstOne->columnUnique = 'unique';$firstOne->columnLength = '0';$manager->persist($firstOne);$manager->flush();$dummy = new Dummy();$dummy->columnNullable = 1; // type mistmatch$dummy->columnLength = '012345678901234567890'; // too long$dummy->columnUnique = 'unique'; // not unique$res = $this->validator->validate($dummy);dump((string) $res);/*Object(App\Entity\Dummy).columnUnique:\n    This value is already used. (code 23bd9dbf-6b9b-41cd-a99e-4844bcf3077f)\nObject(App\Entity\Dummy).columnLength:\n    This value is too long. It should have 20 characters or less. (code d94b19cc-114f-4f44-9cc4-4138e80a87b9)\nObject(App\Entity\Dummy).id:\n    This value should not be null. (code ad32d13f-c3d4-423b-909a-857b961eb720)\nObject(App\Entity\Dummy).columnNullable:\n    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n*/```It also works for DTOs:```phpclass MyDto{    /**@var string */    public $name;}$dto = new MyDto();$dto->name = 1; // type errordump($validator->validate($dto));/*Object(MyDto).name:\n    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n*/```Supported constraints currently are:* `@NotNull` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)* `@Type` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)* `@UniqueEntity` (using Doctrine's `unique` metadata)* `@Length` (using Doctrine's `length` metadata)Many users don't understand that the Doctrine mapping doesn't validate anything (it's just a hint for the schema generator). It leads to usability and security issues (that are not entirely fixed by this PR!!).Even the ones who add constraints often omit important ones like `@Length`, or `@Type` (important when building web APIs).This PR aims to improve things a bit, and ease the development process in RAD and when prototyping. It provides an upgrade path to use proper validation constraints.I plan to make it opt-in, disabled by default, but enabled in the default Flex recipe. (= off by default when using components, on by default when using the full stack framework)TODO:* [x] Add configuration flags* [x] Move the Doctrine-related DI logic from the extension to DoctrineBundle:doctrine/DoctrineBundle#831* [x] Commit the testsCommits-------2d64e703c2 [Validator][DoctrineBridge][FWBundle] Automatic data validation
symfony-splitter pushed a commit to symfony/doctrine-bridge that referenced this pull requestMar 31, 2019
…alidation (dunglas)This PR was merged into the 4.3-dev branch.Discussion----------[Validator][DoctrineBridge][FWBundle] Automatic data validation| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes<!-- don't forget to update src/**/CHANGELOG.md files -->| BC breaks?    | no     <!-- seehttps://symfony.com/bc -->| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->| Fixed tickets | n/a   <!-- #-prefixed issue number(s), if any -->| License       | MIT| Doc PR        |symfony/symfony-docs#11132This feature automatically adds some validation constraints by inferring existing metadata. To do so, it uses the PropertyInfo component and Doctrine metadata, but it has been designed to be easily extendable.Example:```phpuse Doctrine\ORM\Mapping as ORM;/** *@Orm\Entity */class Dummy{    /**     *@Orm\Id     *@Orm\GeneratedValue(strategy="AUTO")     *@Orm\Column(type="integer")     */    public $id;    /**     *@Orm\Column(nullable=true)     */    public $columnNullable;    /**     *@Orm\Column(length=20)     */    public $columnLength;    /**     *@Orm\Column(unique=true)     */    public $columnUnique;}$manager = $this->managerRegistry->getManager();$manager->getRepository(Dummy::class);$firstOne = new Dummy();$firstOne->columnUnique = 'unique';$firstOne->columnLength = '0';$manager->persist($firstOne);$manager->flush();$dummy = new Dummy();$dummy->columnNullable = 1; // type mistmatch$dummy->columnLength = '012345678901234567890'; // too long$dummy->columnUnique = 'unique'; // not unique$res = $this->validator->validate($dummy);dump((string) $res);/*Object(App\Entity\Dummy).columnUnique:\n    This value is already used. (code 23bd9dbf-6b9b-41cd-a99e-4844bcf3077f)\nObject(App\Entity\Dummy).columnLength:\n    This value is too long. It should have 20 characters or less. (code d94b19cc-114f-4f44-9cc4-4138e80a87b9)\nObject(App\Entity\Dummy).id:\n    This value should not be null. (code ad32d13f-c3d4-423b-909a-857b961eb720)\nObject(App\Entity\Dummy).columnNullable:\n    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n*/```It also works for DTOs:```phpclass MyDto{    /**@var string */    public $name;}$dto = new MyDto();$dto->name = 1; // type errordump($validator->validate($dto));/*Object(MyDto).name:\n    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n*/```Supported constraints currently are:* `@NotNull` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)* `@Type` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)* `@UniqueEntity` (using Doctrine's `unique` metadata)* `@Length` (using Doctrine's `length` metadata)Many users don't understand that the Doctrine mapping doesn't validate anything (it's just a hint for the schema generator). It leads to usability and security issues (that are not entirely fixed by this PR!!).Even the ones who add constraints often omit important ones like `@Length`, or `@Type` (important when building web APIs).This PR aims to improve things a bit, and ease the development process in RAD and when prototyping. It provides an upgrade path to use proper validation constraints.I plan to make it opt-in, disabled by default, but enabled in the default Flex recipe. (= off by default when using components, on by default when using the full stack framework)TODO:* [x] Add configuration flags* [x] Move the Doctrine-related DI logic from the extension to DoctrineBundle:doctrine/DoctrineBundle#831* [x] Commit the testsCommits-------2d64e703c2 [Validator][DoctrineBridge][FWBundle] Automatic data validation
symfony-splitter pushed a commit to symfony/validator that referenced this pull requestMar 31, 2019
…alidation (dunglas)This PR was merged into the 4.3-dev branch.Discussion----------[Validator][DoctrineBridge][FWBundle] Automatic data validation| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes<!-- don't forget to update src/**/CHANGELOG.md files -->| BC breaks?    | no     <!-- seehttps://symfony.com/bc -->| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->| Fixed tickets | n/a   <!-- #-prefixed issue number(s), if any -->| License       | MIT| Doc PR        |symfony/symfony-docs#11132This feature automatically adds some validation constraints by inferring existing metadata. To do so, it uses the PropertyInfo component and Doctrine metadata, but it has been designed to be easily extendable.Example:```phpuse Doctrine\ORM\Mapping as ORM;/** *@Orm\Entity */class Dummy{    /**     *@Orm\Id     *@Orm\GeneratedValue(strategy="AUTO")     *@Orm\Column(type="integer")     */    public $id;    /**     *@Orm\Column(nullable=true)     */    public $columnNullable;    /**     *@Orm\Column(length=20)     */    public $columnLength;    /**     *@Orm\Column(unique=true)     */    public $columnUnique;}$manager = $this->managerRegistry->getManager();$manager->getRepository(Dummy::class);$firstOne = new Dummy();$firstOne->columnUnique = 'unique';$firstOne->columnLength = '0';$manager->persist($firstOne);$manager->flush();$dummy = new Dummy();$dummy->columnNullable = 1; // type mistmatch$dummy->columnLength = '012345678901234567890'; // too long$dummy->columnUnique = 'unique'; // not unique$res = $this->validator->validate($dummy);dump((string) $res);/*Object(App\Entity\Dummy).columnUnique:\n    This value is already used. (code 23bd9dbf-6b9b-41cd-a99e-4844bcf3077f)\nObject(App\Entity\Dummy).columnLength:\n    This value is too long. It should have 20 characters or less. (code d94b19cc-114f-4f44-9cc4-4138e80a87b9)\nObject(App\Entity\Dummy).id:\n    This value should not be null. (code ad32d13f-c3d4-423b-909a-857b961eb720)\nObject(App\Entity\Dummy).columnNullable:\n    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n*/```It also works for DTOs:```phpclass MyDto{    /**@var string */    public $name;}$dto = new MyDto();$dto->name = 1; // type errordump($validator->validate($dto));/*Object(MyDto).name:\n    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n*/```Supported constraints currently are:* `@NotNull` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)* `@Type` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)* `@UniqueEntity` (using Doctrine's `unique` metadata)* `@Length` (using Doctrine's `length` metadata)Many users don't understand that the Doctrine mapping doesn't validate anything (it's just a hint for the schema generator). It leads to usability and security issues (that are not entirely fixed by this PR!!).Even the ones who add constraints often omit important ones like `@Length`, or `@Type` (important when building web APIs).This PR aims to improve things a bit, and ease the development process in RAD and when prototyping. It provides an upgrade path to use proper validation constraints.I plan to make it opt-in, disabled by default, but enabled in the default Flex recipe. (= off by default when using components, on by default when using the full stack framework)TODO:* [x] Add configuration flags* [x] Move the Doctrine-related DI logic from the extension to DoctrineBundle:doctrine/DoctrineBundle#831* [x] Commit the testsCommits-------2d64e703c2 [Validator][DoctrineBridge][FWBundle] Automatic data validation
fabpot added a commit to symfony/symfony that referenced this pull requestMar 31, 2019
…alidation (dunglas)This PR was merged into the 4.3-dev branch.Discussion----------[Validator][DoctrineBridge][FWBundle] Automatic data validation| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes<!-- don't forget to update src/**/CHANGELOG.md files -->| BC breaks?    | no     <!-- seehttps://symfony.com/bc -->| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->| Fixed tickets | n/a   <!-- #-prefixed issue number(s), if any -->| License       | MIT| Doc PR        |symfony/symfony-docs#11132This feature automatically adds some validation constraints by inferring existing metadata. To do so, it uses the PropertyInfo component and Doctrine metadata, but it has been designed to be easily extendable.Example:```phpuse Doctrine\ORM\Mapping as ORM;/** *@Orm\Entity */class Dummy{    /**     *@Orm\Id     *@Orm\GeneratedValue(strategy="AUTO")     *@Orm\Column(type="integer")     */    public $id;    /**     *@Orm\Column(nullable=true)     */    public $columnNullable;    /**     *@Orm\Column(length=20)     */    public $columnLength;    /**     *@Orm\Column(unique=true)     */    public $columnUnique;}$manager = $this->managerRegistry->getManager();$manager->getRepository(Dummy::class);$firstOne = new Dummy();$firstOne->columnUnique = 'unique';$firstOne->columnLength = '0';$manager->persist($firstOne);$manager->flush();$dummy = new Dummy();$dummy->columnNullable = 1; // type mistmatch$dummy->columnLength = '012345678901234567890'; // too long$dummy->columnUnique = 'unique'; // not unique$res = $this->validator->validate($dummy);dump((string) $res);/*Object(App\Entity\Dummy).columnUnique:\n    This value is already used. (code 23bd9dbf-6b9b-41cd-a99e-4844bcf3077f)\nObject(App\Entity\Dummy).columnLength:\n    This value is too long. It should have 20 characters or less. (code d94b19cc-114f-4f44-9cc4-4138e80a87b9)\nObject(App\Entity\Dummy).id:\n    This value should not be null. (code ad32d13f-c3d4-423b-909a-857b961eb720)\nObject(App\Entity\Dummy).columnNullable:\n    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n*/```It also works for DTOs:```phpclass MyDto{    /**@var string */    public $name;}$dto = new MyDto();$dto->name = 1; // type errordump($validator->validate($dto));/*Object(MyDto).name:\n    This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n*/```Supported constraints currently are:* `@NotNull` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)* `@Type` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc)* `@UniqueEntity` (using Doctrine's `unique` metadata)* `@Length` (using Doctrine's `length` metadata)Many users don't understand that the Doctrine mapping doesn't validate anything (it's just a hint for the schema generator). It leads to usability and security issues (that are not entirely fixed by this PR!!).Even the ones who add constraints often omit important ones like `@Length`, or `@Type` (important when building web APIs).This PR aims to improve things a bit, and ease the development process in RAD and when prototyping. It provides an upgrade path to use proper validation constraints.I plan to make it opt-in, disabled by default, but enabled in the default Flex recipe. (= off by default when using components, on by default when using the full stack framework)TODO:* [x] Add configuration flags* [x] Move the Doctrine-related DI logic from the extension to DoctrineBundle:doctrine/DoctrineBundle#831* [x] Commit the testsCommits-------2d64e70 [Validator][DoctrineBridge][FWBundle] Automatic data validation
@OskarStarkOskarStark removed the Waiting Code MergeDocs for features pending to be merged labelMar 31, 2019
@OskarStark
Copy link
Contributor

The code is merged, so I removed the label.

@dunglas
Copy link
MemberAuthor

Still needsdoctrine/DoctrineBundle#938 before merge.

@wouterj
Copy link
Member

I think it would be nicer to move this new section to a complete new article, linked at the bottom of the guide. It's not really a base feature we should explain in Doctrine imo. Ideally, we should make the main guides as short as possible.

@dunglas
Copy link
MemberAuthor

@wouterj But it will be enabled by default. Shouldn't we at least reference it in this guide?
@OskarStark I'll do the change as soon as I'll be back from vacations, thanks for the review!!

OskarStark reacted with thumbs up emoji

@sylfabre
Copy link
Contributor

@dunglas thank you for this new feature.
Do you plan to add more documentation on how to extend it?

My use-case:

  • we have developed at AssoConnect a similar logic to valid our DTOs and entities
  • we have custom Doctrine types (like email, iban, ...) to enforce consistency in the database structure
  • we use these custom types for validation as well (likeemail =>EmailValidator)
  • we would like to switch to your feature as soon as Symfony 4.3 is released

@javiereguiluzjaviereguiluz modified the milestones:next,4.3May 9, 2019
@xabbuhxabbuh changed the base branch frommaster to4.3May 9, 2019 07:43
@dunglas
Copy link
MemberAuthor

@sylfabre thanks! Unfortunately I don't think I'll have the time to add such docs anytime soon, but I'll be glad to help someone writing it.
In a nutshell, you have to implementtheLoaderInterface interface and to register this loader with thevalidator.auto_mapper tag.

You can see how it's done for Doctrine:

I hope this helps.

@javiereguiluz
Copy link
Member

This was finally merged! Thanks Kévin and reviewers! I made all the changes requested by reviewers while merging.

@dunglas
Copy link
MemberAuthor

Thank you very much Javier!

@nspyke
Copy link
Contributor

@dunglas@javiereguiluz I'm looking for some info on how to configure this feature.
Theframework.validation.auto_mapping key is yet to be documented here:https://symfony.com/doc/current/reference/configuration/framework.html#validation

I can see in the feature PR there is mentions of being able to configure it on a class level as well as a class property level (i think?), but again I'm not too sure how to implement this

I'd be happy to make a PR to add this provided I had some instruction on where to find the information about the configuration available

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

Reviewers

@OskarStarkOskarStarkOskarStark approved these changes

@javiereguiluzjaviereguiluzjaviereguiluz approved these changes

+4 more reviewers

@apfelboxapfelboxapfelbox left review comments

@noniagriconomienoniagriconomienoniagriconomie left review comments

@teohhanhuiteohhanhuiteohhanhui requested changes

@dkarlovidkarlovidkarlovi approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.3

Development

Successfully merging this pull request may close these issues.

12 participants

@dunglas@OskarStark@wouterj@sylfabre@javiereguiluz@nspyke@dkarlovi@teohhanhui@apfelbox@noniagriconomie@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp