Skip to content

Commit

Permalink
Update ignore rule to accept a closure
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Nov 2, 2021
1 parent 9e30c15 commit 912c51b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/advanced-usage/validation-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,9 @@ public string $value;

#[Unique('users', ignore: 5)]
public string $value;

#[Unique('users', ignore: fn() => request()->get('email'))]
public string $value;
```

### Url
Expand Down
4 changes: 2 additions & 2 deletions docs/as-a-data-transfer-object/request-to-data-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ class SongData extends Data
public static function fromRequest(Request $request): static
{
return new self(
{$request->input('title_of_song')},
{$request->input('artist_name')}
$request->input('title_of_song'),
$request->input('artist_name')
);
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Attributes/Validation/Unique.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(
private string $table,
private ?string $column = 'NULL',
private ?string $connection = null,
private ?string $ignore = null,
private string|Closure|null $ignore = null,
private ?string $ignoreColumn = null,
private bool $withoutTrashed = false,
private string $deletedAtColumn = 'deleted_at',
Expand All @@ -33,7 +33,10 @@ public function getRules(): array
}

if ($this->ignore) {
$rule->ignore($this->ignore, $this->ignoreColumn);
$rule->ignore(
is_callable($this->ignore) ? ($this->ignore)() : $this->ignore,
$this->ignoreColumn
);
}

if ($this->where) {
Expand Down
5 changes: 5 additions & 0 deletions tests/Attributes/Validation/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,11 @@ public function uniqueAttributesDataProvider(): Generator
'expected' => [(new BaseUnique('users', 'email'))->ignore(5, 'uuid')],
];

yield [
'attribute' => new Unique('users', 'email', ignore: fn() => 5),
'expected' => [(new BaseUnique('users', 'email'))->ignore(5)],
];

$closure = fn (Builder $builder) => $builder;

yield [
Expand Down

0 comments on commit 912c51b

Please sign in to comment.