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

Appearance settings

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

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
wants to merge 12 commits into from
Next Next commit
Add argument: fields
  • Loading branch information
wkania committed Apr 3, 2022
commit 374683e0f1cc44beddae071830e5f90602e7c29f
26 changes: 25 additions & 1 deletion 26 src/Symfony/Component/Validator/Constraints/Unique.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Unique extends Constraint
{
public const IS_NOT_UNIQUE = '7911c98d-b845-4da0-94b7-a8dac36bc55a';

public $fields = [];

protected const ERROR_NAMES = [
self::IS_NOT_UNIQUE => 'IS_NOT_UNIQUE',
];
Expand All @@ -37,13 +39,25 @@ class Unique extends Constraint
public $message = 'This collection should contain only unique elements.';
public $normalizer;

/**
* {@inheritdoc}
*
* @param array|string $fields the combination of fields that must contain unique values or a set of options
*/
public function __construct(
array $options = null,
string $message = null,
callable $normalizer = null,
array $groups = null,
mixed $payload = null
mixed $payload = null,
$fields = null,
) {
if (\is_array($fields) && \is_string(key($fields))) {
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
$options = array_merge($fields, $options);
} elseif (null !== $fields) {
$options['fields'] = $fields;
}

parent::__construct($options, $groups, $payload);

$this->message = $message ?? $this->message;
Expand All @@ -53,4 +67,14 @@ public function __construct(
throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
}
}

public function getOptions()
{
return ['fields'];
}

public function getDefaultOption()
{
return 'fields';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public function validate(mixed $value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, Unique::class);
}

if (!\is_array($constraint->fields) && !\is_string($constraint->fields)) {
throw new UnexpectedTypeException($constraint->fields, 'array');
}

$fields = (array) $constraint->fields;

if (null === $value) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,35 @@ public function testExpectsValidCaseInsensitiveComparison()

$this->assertNoViolation();
}

/**
* @dataProvider getValidCollectionValues
*/
public function testValidCollectionValues($value, $fields)
{
$this->validator->validate($value, new Unique($fields));

$this->assertNoViolation();
}

public function getValidCollectionValues()
{
return [
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
yield 'single string' => [[['letter' => 'a']], 'letter'],
yield 'unique strings' => [[['language' => 'eng'], ['language' => 'fra'], ['language' => 'pol']], 'language'],
yield 'unique floats' => [[
['latitude' => 51.509865, 'longitude' => -0.118092],
['latitude' => 48.864716, 'longitude' => 2.349014],
['latitude' => 52.520008, 'longitude' => 13.404954],
], ['latitude', 'longitude']],
yield 'unique int and string' => [[
['id' => 1, 'email' => 'bar@email.com'], ['id' => 2, 'email' => 'foo@email.com'],
], ['id', 'name']],
yield 'unique arrays' => [[['vector' => [1, 2]], ['vector' => [2, 4]], ['vector' => [4, 6]]
], ['vector']],
yield 'unique objects' => [[['object' => new \stdClass()], ['object' => new \stdClass()]], ['object']],
];
}
}

class CallableClass
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.