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

[2.x] Add MultiSelect table filter #785

Merged
merged 9 commits into from
Dec 11, 2021
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
132 changes: 1 addition & 131 deletions packages/forms/docs/02-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,142 +227,12 @@ class CreatePost extends Component implements Forms\Contracts\HasForms

When `getState()` is run:

1) [Validation](#validation) rules are checked, and if errors are present, the form is not submitted.
1) [Validation](validation) rules are checked, and if errors are present, the form is not submitted.
2) Any pending file uploads are stored permanently in the filesystem.
3) [Field relationships](#field-relationships), if they are defined, are saved.

> You may transform the value that is dehydrated from a field [using the `dehydrateStateUsing()` method](advanced#dehydration).

## Validation

You may add validation rules to any field using the `rules()` method:

```php
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.

We recommend that you use dedicated validation methods wherever possible.

#### Different

The field value must be different to another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-different)

```php
Field::make('backupEmail')->different('email')
```

#### Exists

The field value must exist in the database. [See the Laravel documentation](https://laravel.com/docs/validation#rule-exists).

```php
Field::make('invitation')->exists()
```

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')
```

#### Greater than

The field value must be greater than another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-gt)

```php
Field::make('newNumber')->gt('oldNumber')
```

#### Greater than or equal to

The field value must be greater than or equal to another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-gte)

```php
Field::make('newNumber')->gte('oldNumber')
```

#### Less than

The field value must be less than another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-lt)

```php
Field::make('newNumber')->lt('oldNumber')
```

#### Less than or equal to

The field value must be less than or equal to another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-lte)

```php
Field::make('newNumber')->lte('oldNumber')
```

#### Nullable

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()
```

#### Required

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)
```

## Registering a model

You may register a model to a form. The form builder is able to use this model to unlock DX features, such as:
Expand Down
215 changes: 215 additions & 0 deletions packages/forms/docs/05-validation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
---
title: Validation
---

## Getting started

Validation rules may be added to any [field](fields).

Filament includes several [dedicated validation methods](#available-rules), but you can also use any [other Laravel validation rules](#other-rules), including [custom validation rules](#custom-rules).

## Available rules

### After (date)

The field value must be a value after a given date. [See the Laravel documentation](https://laravel.com/docs/validation#rule-after)

```php
Field::make('startDate')->after('tomorrow')
```

Alternatively, you may pass the name of another field to compare against:

```php
Field::make('startDate')
Field::make('endDate')->after('startDate')
```

### After or equal to (date)

The field value must be a date after or equal to the given date. [See the Laravel documentation](https://laravel.com/docs/validation#rule-after-or-equal)

```php
Field::make('startDate')->afterOrEqual('tomorrow')
```

Alternatively, you may pass the name of another field to compare against:

```php
Field::make('startDate')
Field::make('endDate')->afterOrEqual('startDate')
```

### Before (date)

The field value must be a date before a given date. [See the Laravel documentation](https://laravel.com/docs/validation#rule-before)

```php
Field::make('startDate')->before('first day of next month')
```

Alternatively, you may pass the name of another field to compare against:

```php
Field::make('startDate')->before('endDate')
Field::make('endDate')
```

### Before or equal to (date)

The field value must be a date before or equal to the given date. [See the Laravel documentation](https://laravel.com/docs/validation#rule-before-or-equal)

```php
Field::make('startDate')->beforeOrEqual('end of this month')
```

Alternatively, you may pass the name of another field to compare against:

```php
Field::make('startDate')->beforeOrEqual('endDate')
Field::make('endDate')
```

### Different

The field value must be different to another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-different)

```php
Field::make('backupEmail')->different('email')
```

### Exists

The field value must exist in the database. [See the Laravel documentation](https://laravel.com/docs/validation#rule-exists).

```php
Field::make('invitation')->exists()
```

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')
```

### Greater than

The field value must be greater than another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-gt)

```php
Field::make('newNumber')->gt('oldNumber')
```

### Greater than or equal to

The field value must be greater than or equal to another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-gte)

```php
Field::make('newNumber')->gte('oldNumber')
```

### Less than

The field value must be less than another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-lt)

```php
Field::make('newNumber')->lt('oldNumber')
```

### Less than or equal to

The field value must be less than or equal to another. [See the Laravel documentation](https://laravel.com/docs/validation#rule-lte)

```php
Field::make('newNumber')->lte('oldNumber')
```

### Nullable

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()
```

### Required

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)
```

## Other rules

You may add other validation rules to any field using the `rules()` method:

```php
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).

## Custom rules

You may use any custom validation rules as you would do in [Laravel](https://laravel.com/docs/validation#custom-validation-rules):

```php
TextInput::make('slug')->rules([new Uppercase()])
```

You may also use [closure rules](https://laravel.com/docs/validation#using-closures):

```php
TextInput::make('slug')->rules([
function (string $attribute, $value, Closure $fail) {
if ($value === 'foo') {
$fail("The {$attribute} is invalid.");
}
},
])
```
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ class="px-3 py-2 text-sm text-gray-700 cursor-default select-none"
x-show="state.length"
class="overflow-hidden rtl:space-x-reverse relative w-full px-1 py-1"
>
<div class="flex gap-1">
<div class="flex flex-wrap gap-1">
<template class="inline" x-for="option in state" x-bind:key="option">
<button
@unless ($isDisabled())
x-on:click="deselectOption(option)"
x-on:click.stop="deselectOption(option)"
@endunless
type="button"
@class([
Expand Down
Loading