Skip to content

PHPORM-68 Fix partial value un exist validator #2568

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 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Validation/DatabasePresenceVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ public function getCount($collection, $column, $value, $excludeId = null, $idCol
*/
public function getMultiCount($collection, $column, array $values, array $extra = [])
{
// Generates a regex like '/(a|b|c)/i' which can query multiple values
$regex = '/('.implode('|', $values).')/i';
// Nothing can match an empty array. Return early to avoid matching an empty string.
if ($values === []) {
return 0;
}

// Generates a regex like '/^(a|b|c)$/i' which can query multiple values
$regex = new Regex('^('.implode('|', array_map(preg_quote(...), $values)).')$', 'i');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first-class callable syntax is just beautiful...


$query = $this->table($collection)->where($column, 'regex', $regex);

Expand Down
26 changes: 26 additions & 0 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,31 @@ public function testExists(): void
['name' => 'required|exists:users']
);
$this->assertFalse($validator->fails());

$validator = Validator::make(
['name' => ['test name', 'john']], // Part of an existing value
['name' => 'required|exists:users']
);
$this->assertTrue($validator->fails());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, $validator->fails() is expected to return true here because we're checking that a user with that exact name exists?

Copy link
Member Author

@GromNaN GromNaN Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, according to the previous tests, the validation must fail when the value is NOT found in the collection.


$validator = Validator::make(
['name' => '(invalid regex{'],
['name' => 'required|exists:users']
);
$this->assertTrue($validator->fails());

$validator = Validator::make(
['name' => ['foo', '(invalid regex{']],
['name' => 'required|exists:users']
);
$this->assertTrue($validator->fails());

User::create(['name' => '']);

$validator = Validator::make(
['name' => []],
['name' => 'exists:users']
);
$this->assertFalse($validator->fails());
}
}