10000 [Validator] Add the fields option to the Unique constraint by wkania · Pull Request #16713 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add the fields option to the Unique constraint #16713

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

Merged
merged 1 commit into from
May 1, 2022

Conversation

wkania
Copy link
Contributor
@wkania wkania commented Apr 13, 2022

Maybe I will add an example of use.

@carsonbot
Copy link
Collaborator

It looks like you unchecked the "Allow edits from maintainer" box. That is fine, but please note that if you have multiple commits, you'll need to squash your commits into one before this can be merged. Or, you can check the "Allow edits from maintainers" box and the maintainer can squash for you.

Cheers!

Carsonbot

fabpot added a commit to symfony/symfony that referenced this pull request Apr 14, 2022
…cked for uniqueness (wkania)

This PR was squashed before being merged into the 6.1 branch.

Discussion
----------

[Validator] Define which collection keys should be checked for uniqueness

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #9888
| License       | MIT
| Doc PR        | symfony/symfony-docs#16713

Currently, the validator checks each element of the collection as a whole. We already have a custom normalizer (which is great), but it would be nice to be able to check for uniqueness certain [collection](https://symfony.com/doc/current/reference/constraints/Collection.html) keys.

For example, some fields in the collection element can be identifiers. They should be unique within the collection, even when the rest of the element data are different.

Current state:
- validates that all the elements of the given collection are unique

New state:
- preserve the current state,
- all old tests pass (no changes in them),
- no breaking changes,
- define which collection fields should be checked for uniqueness (optional),
- fields are optional in each element of the collection. Use [collection constraints](https://symfony.com/doc/current/reference/constraints/Collection.html) if they are required

Examples:

1. Basic example. Each translation of the same resource must be in a different language.
```php
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\Count(min=1),
 * @Assert\Unique(fields={"language"}),
 * @Assert\Collection(
 *         fields = {
 *             "language" = {
 *                  @Assert\NotBlank,
 *                  @Assert\Length(min = 2, max = 2),
 *                  @Assert\Language
 *             },
 *             "title" = {
 *                  @Assert\NotBlank,
 *                  @Assert\Length(max = 255)
 *             },
 *             "description" = {
 *                  @Assert\NotBlank,
 *                  @Assert\Length(max = 255)
 *             }
 *         }
 * )
 */
public array $translations = [];
```
2. An example where Optional is recognizable. Items with the id are changed and without are new.
```php
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Optional;

/**
 * 
8000
@Assert\Unique(fields={"id"}),
 * @Assert\Collection(
 *         fields = {
 *             "id" = @Assert\Optional({
 *                  @Assert\Uuid
 *             }),
 *             "name" = {
 *                  @Assert\NotBlank,
 *                  @Assert\Length(max = 255)
 *             }
 *         }
 * )
 */
public array $items = [];
```
3. An example with composite uniqueness
```php
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\Unique(fields={"latitude", "longitude"}),
 * @Assert\Collection(
 *         fields = {
 *             "latitude" = {
 *                  @Assert\NotBlank
 *             },
 *             "longitude" = {
 *                  @Assert\NotBlank
 *             },
 *             "poi" = {
 *                  @Assert\Length(max = 255)
 *             }
 *         }
 * )
 */
public array $coordinates = [];
```

Commits
-------

0e8f4ce [Validator] Define which collection keys should be checked for uniqueness
symfony-splitter pushed a commit to symfony/validator that referenced this pull request Apr 14, 2022
…cked for uniqueness (wkania)

This PR was squashed before being merged into the 6.1 branch.

Discussion
----------

[Validator] Define which collection keys should be checked for uniqueness

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #9888
| License       | MIT
| Doc PR        | symfony/symfony-docs#16713

Currently, the validator checks each element of the collection as a whole. We already have a custom normalizer (which is great), but it would be nice to be able to check for uniqueness certain [collection](https://symfony.com/doc/current/reference/constraints/Collection.html) keys.

For example, some fields in the collection element can be identifiers. They should be unique within the collection, even when the rest of the element data are different.

Current state:
- validates that all the elements of the given collection are unique

New state:
- preserve the current state,
- all old tests pass (no changes in them),
- no breaking changes,
- define which collection fields should be checked for uniqueness (optional),
- fields are optional in each element of the collection. Use [collection constraints](https://symfony.com/doc/current/reference/constraints/Collection.html) if they are required

Examples:

1. Basic example. Each translation of the same resource must be in a different language.
```php
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\Count(min=1),
 * @Assert\Unique(fields={"language"}),
 * @Assert\Collection(
 *         fields = {
 *             "language" = {
 *                  @Assert\NotBlank,
 *                  @Assert\Length(min = 2, max = 2),
 *                  @Assert\Language
 *             },
 *             "title" = {
 *                  @Assert\NotBlank,
 *                  @Assert\Length(max = 255)
 *             },
 *             "description" = {
 *                  @Assert\NotBlank,
 *                  @Assert\Length(max = 255)
 *             }
 *         }
 * )
 */
public array $translations = [];
```
2. An example where Optional is recognizable. Items with the id are changed and without are new.
```php
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Optional;

/**
 * @Assert\Unique(fields={"id"}),
 * @Assert\Collection(
 *         fields = {
 *             "id" = @Assert\Optional({
 *                  @Assert\Uuid
 *             }),
 *             "name" = {
 *                  @Assert\NotBlank,
 *                  @Assert\Length(max = 255)
 *             }
 *         }
 * )
 */
public array $items = [];
```
3. An example with composite uniqueness
```php
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\Unique(fields={"latitude", "longitude"}),
 * @Assert\Collection(
 *         fields = {
 *             "latitude" = {
 *                  @Assert\NotBlank
 *             },
 *             "longitude" = {
 *                  @Assert\NotBlank
 *             },
 *             "poi" = {
 *                  @Assert\Length(max = 255)
 *             }
 *         }
 * )
 */
public array $coordinates = [];
```

Commits
-------

0e8f4cefdb [Validator] Define which collection keys should be checked for uniqueness
@wkania
Copy link
Contributor Author
wkania commented Apr 14, 2022

The feature was merged.
I could add an example like this (in all variants) or is it not needed? What do you think?

use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\Unique(fields={"latitude", "longitude"}),
 * @Assert\Collection(
 *         fields = {
 *             "latitude" = {
 *                  @Assert\NotBlank
 *             },
 *             "longitude" = {
 *                  @Assert\NotBlank
 *             },
 *             "poi" = {
 *                  @Assert\Length(max = 255)
 *             }
 *         }
 * )
 */
public array $coordinates = [];  

wouterj added a commit that referenced this pull request May 1, 2022
…aint (wkania)

This PR was merged into the 6.1 branch.

Discussion
----------

[Validator] Add the fields option to the Unique constraint

Maybe I will add an example of use.

Commits
-------

dcbad67 Added basic example
caf3e1f [Validator] Add the fields option to the Unique constraint
@wouterj wouterj merged commit caf3e1f into symfony:6.1 May 1, 2022
@wouterj
Copy link
Member
wouterj commented May 1, 2022

Cool feature. Thanks for writing the feature and providing the docs, @wkania!

I've added the example to this section after the merge in dcbad67 I think it helps to understand this option. Please have a look whether I didn't make a mistake describing the feature :)

javiereguiluz added a commit that referenced this pull request May 3, 2022
This PR was merged into the 6.1 branch.

Discussion
----------

[Validator] Fix typo in the Unique constraint

`@wouterj`  just a small fix related to the #16713 (comment)

Commits
-------

3fd53b9 Fix typo in the Unique constraint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0