This commit is contained in:
Dan Harrin
2021-09-05 10:47:34 +01:00
parent f877eb9cd1
commit 663d32732c
4 changed files with 71 additions and 20 deletions
+55 -5
View File
@@ -344,6 +344,8 @@ You may add validation rules to any field using the `rules()` method:
TextInput::make('slug')->rules(['alpha_dash'])
```
A full list of validation rules may be found in the [Laravel documentation](https://laravel.com/docs/validation#available-validation-rules)
#### Dedicated methods
There are also dedicated methods for some validation rules, some of which are able to add frontend validation as well as backend validation.
@@ -352,17 +354,29 @@ We recommend that you use dedicated validation methods wherever possible.
##### Exists
The field under validation must exist in the database.
The field value must exist in the database. [See the Laravel documentation](https://laravel.com/docs/validation#rule-exists).
```php
Field::make('name')->exists()
Field::make('invitation')->exists()
```
You may specify a custom table or model to search inside. If none is specified, the
By default, the form's model will be searched, [if it is registered](#registering-a-model). You may specify a custom table name or model to search:
```php
use App\Models\Invitation;
Field::make('invitation')->exists(table: Invitation::class)
```
By default, the field name will be used as the column to search. You may specify a custom column to search:
```php
Field::make('invitation')->exists(column: 'id')
```
##### Nullable
The field under validation can be empty. This rule is applied by default if the `required` rule is not present.
The field value can be empty. This rule is applied by default if the `required` rule is not present. [See the Laravel documentation](https://laravel.com/docs/validation#rule-nullable)
```php
Field::make('name')->nullable()
@@ -370,12 +384,48 @@ Field::make('name')->nullable()
##### Required
The field under validation must not be empty.
The field value must not be empty. [See the Laravel documentation](https://laravel.com/docs/validation#rule-required)
```php
Field::make('name')->required()
```
##### Same
The field value must be the same as another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-same)
```php
Field::make('password')->same('passwordConfirmation')
```
##### Unique
The field value must not exist in the database. [See the Laravel documentation](https://laravel.com/docs/validation#rule-unique)
```php
Field::make('email')->unique()
```
By default, the form's model will be searched, [if it is registered](#registering-a-model). You may specify a custom table name or model to search:
```php
use App\Models\User;
Field::make('email')->unique(table: User::class)
```
By default, the field name will be used as the column to search. You may specify a custom column to search:
```php
Field::make('email')->unique(column: 'email_address')
```
Sometimes, you may wish to ignore a given model during unique validation. For example, consider an "update profile" form that includes the user's name, email address, and location. You will probably want to verify that the email address is unique. However, if the user only changes the name field and not the email field, you do not want a validation error to be thrown because the user is already the owner of the email address in question.
```php
Field::make('email')->unique(ignorable: $ignoredUser)
```
## Field relationships
Some fields, such as the `BelongsToManyMultiSelect`, `SpatieMediaLibraryFileUpload`, or `SpatieTagsInput` are able to interact with model relationships.
@@ -14,13 +14,13 @@ trait CanBeValidated
protected $validationAttribute = null;
public function exists(string | callable | null $table = null, string | callable | null $columnName = null): static
public function exists(string | callable | null $table = null, string | callable | null $column = null): static
{
$this->rule(function () use ($columnName, $table) {
$this->rule(function () use ($column, $table) {
$table = $this->evaluate($table) ?? $this->getModelClass();
$columnName = $this->evaluate($columnName) ?? $this->getName();
$column = $this->evaluate($column) ?? $this->getName();
return Rule::exists($table, $columnName);
return Rule::exists($table, $column);
}, fn (): bool => (bool) $this->evaluate($table));
return $this;
@@ -62,12 +62,13 @@ trait CanBeValidated
return $this;
}
public function same(string | callable $statePath): static
public function same(string | callable $statePath, bool $isStatePathAbsolute = false): static
{
$this->rule(function () use ($statePath): string {
$this->rule(function () use ($isStatePathAbsolute, $statePath): string {
$statePath = $this->evaluate($statePath);
if ($containerStatePath = $this->getContainer()->getStatePath()) {
$containerStatePath = $this->getContainer()->getStatePath();
if ((! $isStatePathAbsolute) && $containerStatePath) {
$statePath = "{$containerStatePath}.{$statePath}";
}
@@ -77,14 +78,14 @@ trait CanBeValidated
return $this;
}
public function unique(string | callable | null $table = null, string | callable | null $columnName = null, Model | callable $ignorable = null): static
public function unique(string | callable | null $table = null, string | callable | null $column = null, Model | callable $ignorable = null): static
{
$this->rule(function () use ($columnName, $ignorable, $table) {
$this->rule(function () use ($column, $ignorable, $table) {
$table = $this->evaluate($table) ?? $this->getModelClass();
$columnName = $this->evaluate($columnName) ?? $this->getName();
$column = $this->evaluate($column) ?? $this->getName();
$ignorable = $this->evaluate($ignorable);
return Rule::unique($table, $columnName)
return Rule::unique($table, $column)
->when(
$ignorable,
fn (Unique $rule) => $rule->ignore(
@@ -131,11 +131,11 @@ trait HasState
return data_get($this->getLivewire(), $this->getStatePath());
}
public function getStatePath(bool $absolute = true): string
public function getStatePath(bool $isAbsolute = true): string
{
$pathComponents = [];
if ($absolute && ($containerStatePath = $this->getContainer()->getStatePath())) {
if ($isAbsolute && ($containerStatePath = $this->getContainer()->getStatePath())) {
$pathComponents[] = $containerStatePath;
}
+2 -2
View File
@@ -129,11 +129,11 @@ trait HasState
return $this->dehydrateState($state);
}
public function getStatePath(bool $absolute = true): string
public function getStatePath(bool $isAbsolute = true): string
{
$pathComponents = [];
if ($absolute && $parentComponentStatePath = $this->getParentComponent()?->getStatePath()) {
if ($isAbsolute && $parentComponentStatePath = $this->getParentComponent()?->getStatePath()) {
$pathComponents[] = $parentComponentStatePath;
}