Skip to content

Commit

Permalink
[4.x] Allow overwriting the column for unique_user_value validation (
Browse files Browse the repository at this point in the history
…#8852)

Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
marcorieser and jasonvarga authored Oct 16, 2023
1 parent 323fa4e commit eb1a1fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Validation/UniqueUserValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ class UniqueUserValue
{
public function validate($attribute, $value, $parameters, $validator)
{
[$except] = array_pad($parameters, 1, null);
[$except, $column] = array_pad($parameters, 2, null);

$column ??= $attribute;

$existing = User::query()
->where($attribute, $value)
->where($column, $value)
->first();

if (! $existing) {
Expand Down
11 changes: 11 additions & 0 deletions tests/Validation/UniqueUserValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@ public function it_passes_when_updating()
['email' => 'unique_user_value:123']
)->passes());
}

/** @test */
public function it_supports_overwriting_the_column()
{
User::make()->email('[email protected]')->save();

$this->assertTrue(Validator::make(
['baz' => '[email protected]'],
['baz' => 'unique_user_value:null,email']
)->fails());
}
}

0 comments on commit eb1a1fc

Please sign in to comment.