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

Skip to content

[Validator] Unique constraint value comparison #37721

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
artyuum opened this issue Jul 31, 2020 · 2 comments
Closed

[Validator] Unique constraint value comparison #37721

artyuum opened this issue Jul 31, 2020 · 2 comments

Comments

@artyuum
Copy link
Contributor

artyuum commented Jul 31, 2020

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

@xabbuh
Copy link
Member

xabbuh commented Aug 1, 2020

#37507 looks like a suggestion that would solve this issue too

@xabbuh
Copy link
Member

xabbuh commented Aug 3, 2020

Let's close in favour of #37507. Having one issue to keep track of this feature request is enough.

@xabbuh xabbuh closed this as completed Aug 3, 2020
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

2 participants