Skip to content
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

Add ExcludeWith validation rule #584

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
11 changes: 11 additions & 0 deletions docs/advanced-usage/validation-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,17 @@ public string $closure;
public string $closure;
```

### ExcludeWith

*At the moment the data is not yet excluded due to technical reasons, v4 should fix this*

[Docs](https://laravel.com/docs/9.x/validation#rule-exclude-with)

```php
#[ExcludeWith('other_field')]
public string $closure;
```

### ExcludeWithout

*At the moment the data is not yet excluded due to technical reasons, v4 should fix this*
Expand Down
28 changes: 28 additions & 0 deletions src/Attributes/Validation/ExcludeWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;
use Spatie\LaravelData\Support\Validation\References\FieldReference;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class ExcludeWith extends StringValidationAttribute
{
protected FieldReference $field;

public function __construct(
string|FieldReference $field,
) {
$this->field = $this->parseFieldReference($field);
}

public static function keyword(): string
{
return 'exclude_with';
}

public function parameters(): array
{
return [$this->field];
}
}
2 changes: 2 additions & 0 deletions src/Support/Validation/ValidationRuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Spatie\LaravelData\Attributes\Validation\Enum;
use Spatie\LaravelData\Attributes\Validation\ExcludeIf;
use Spatie\LaravelData\Attributes\Validation\ExcludeUnless;
use Spatie\LaravelData\Attributes\Validation\ExcludeWith;
use Spatie\LaravelData\Attributes\Validation\ExcludeWithout;
use Spatie\LaravelData\Attributes\Validation\Exists;
use Spatie\LaravelData\Attributes\Validation\File;
Expand Down Expand Up @@ -132,6 +133,7 @@ protected function mapping(): array
Enum::keyword() => Enum::class,
ExcludeIf::keyword() => ExcludeIf::class,
ExcludeUnless::keyword() => ExcludeUnless::class,
ExcludeWith::keyword() => ExcludeWith::class,
ExcludeWithout::keyword() => ExcludeWithout::class,
Exists::keyword() => Exists::class,
File::keyword() => File::class,
Expand Down
6 changes: 6 additions & 0 deletions tests/Datasets/Attributes/RulesDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Spatie\LaravelData\Attributes\Validation\Enum;
use Spatie\LaravelData\Attributes\Validation\ExcludeIf;
use Spatie\LaravelData\Attributes\Validation\ExcludeUnless;
use Spatie\LaravelData\Attributes\Validation\ExcludeWith;
use Spatie\LaravelData\Attributes\Validation\ExcludeWithout;
use Spatie\LaravelData\Attributes\Validation\Exists;
use Spatie\LaravelData\Attributes\Validation\File;
Expand Down Expand Up @@ -218,6 +219,11 @@ function fixature(
expected: 'exclude_unless:field,42',
);

yield fixature(
attribute: new ExcludeWith('field'),
expected: 'exclude_with:field',
);

yield fixature(
attribute: new ExcludeWithout('field'),
expected: 'exclude_without:field',
Expand Down
Loading