Skip to content

[Validator] Unique constraint value comparison #37721

Closed
@artyuum

Description

@artyuum

Description

Right now, the Unique constraint sees the following values as different:

  • Sun
  • sun

This is correct, but what if an user needs to ensure that a value has not been passed more than once (no matter if it's in lowercase/uppercase)?

Example

My suggestion would be to add an option like:

@Assert\Unique(caseInsensitive=true)

The Unique constraint will then see both values ("Sun" & "sun") as the same and will add a violation.

To make this change, I would edit both files as follow:

UniqueValidator

Before

foreach ($value as $element) {
    ...
}

After

if ($constraint->caseInsensitive) {
    $value = array_map('strtolower', $value); // or maybe use mb_strtolower()?
}

foreach ($value as $element) {
    ...
}

Unique

Before

class Unique extends Constraint
{
    public const IS_NOT_UNIQUE = '7911c98d-b845-4da0-94b7-a8dac36bc55a';

    ...
}

After

class Unique extends Constraint
{
    public const IS_NOT_UNIQUE = '7911c98d-b845-4da0-94b7-a8dac36bc55a';

    public $caseInsensitive = false;

    ...
}

EDIT: Improved readability

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions