Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Exclude patterns in auto mapping #32015

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

Closed
alanpoulain opened this issue Jun 12, 2019 · 2 comments
Closed

[Validator] Exclude patterns in auto mapping #32015

alanpoulain opened this issue Jun 12, 2019 · 2 comments

Comments

@alanpoulain
Copy link
Contributor

Description
Like autowiring/autoconfiguration, it should be possible to define an exclude glob patterns for the new auto mapping feature.

This way, it should be possible to add auto mapping for an entire directory (like App\Entity\) and to exclude some files in it (like App\Entity\User).

Example
I'm not sure how we could add this feature with the current way to configure it.

Today we can do:

framework:
    validation:
        auto_mapping:
            App\Entity\: ['validator.property_info_loader']

With this configuration, only the validator.property_info_loader is used to load the validation constraints.

Maybe something like this would be okay, to look like the services configuration?

framework:
    validation:
        auto_mapping:
            include:
                App\Entity\: []
            exclude:
                - App\Entity\User

It would mean the previous way of configuring it would add the mapping to the include key.

WDYT?

@dunglas
Copy link
Member

dunglas commented Jun 12, 2019

What do you think about:

framework:
    validation:
        auto_mapping:
            App\Entity\:
                services: []
                exclude:
                - App\Entity\User

Also, currently you can use the glob syntax to list only the classes you want to include (whitelist).

@alanpoulain
Copy link
Contributor Author

I like it better, yes 🙂

You can do a whitelist but I think it defeats the purpose since you have to think about it each time you add a new class.

xabbuh added a commit that referenced this issue Oct 30, 2019
…sable auto-validation (dunglas)

This PR was squashed before being merged into the 4.4 branch (closes #32107).

Discussion
----------

[Validator] Add AutoMapping constraint to enable or disable auto-validation

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #32070, #32015   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | todo

As discussed in #32070 and #32015, it's sometimes mandatory to prevent some classes or properties to be auto mapped (auto-validated). This PR introduces a new constraint, `@AutoMapping` allowing to do exactly that. Examples:

Class:

```php
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Orm\Entity
 * @Assert\AutoMapping(false)
 */
class DoctrineLoaderNoAutoMappingEntity
{
    /**
     * @Orm\Id
     * @Orm\Column
     */
    public $id;

    /**
     * @Orm\Column(length=20, unique=true)
     */
    public $maxLength;
}
```

Property:

```php
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Orm\Entity
 */
class DoctrineLoaderEntity extends DoctrineLoaderParentEntity
{
    /**
     * @Orm\Id
     * @Orm\Column
     */
    public $id;

    /**
     * @Orm\Column(length=10)
     * @Assert\AutoMapping(false)
     */
    public $noAutoMapping;
}
```

The rules are the following:

* If the constraint is present on a property, and set to true, auto-mapping is always on, regardless of the config, and of any class level annotation
* If the constraint is present on a property, and set to false, auto-mapping is always off, regardless of the config, and of any class level annotation
* If the constraint is present on a class, and set to true, auto-mapping is always on except if a the annotation has been added to a specific property, and regardless of the config
* If the constraint is present on a class, and set to false, auto-mapping is always off except if a the annotation has been added to a specific property, and regardless of the config

Commits
-------

f6519ce [Validator] Add AutoMapping constraint to enable or disable auto-validation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants