Validation

Getting started

Validation rules may be added to any field.

Filament includes several dedicated validation methods, but you can also use any other Laravel validation rules, including custom validation rules.

Beware that some validations rely on the field name and therefore won't work when passed via ->rule()/->rules(). Use the dedicated validation methods whenever you can.

Available rules

After (date)

The field value must be a value after a given date. See the Laravel documentation

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

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

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

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

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

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

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

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

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

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

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

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

Different

The field value must be different to another. See the Laravel documentation

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

Exists

The field value must exist in the database. See the Laravel documentation.

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

By default, the form's model will be searched, if it is registered. You may specify a custom table name or model to search:

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:

Field::make('invitation')->exists(column: 'id')

You can further customize the rule by passing a closure to the callback parameter:

use Illuminate\Validation\Rules\Exists;
 
Field::make('invitation')
->exists(callback: function (Exists $rule) {
return $rule->where('is_active', 1);
})

Greater than

The field value must be greater than another. See the Laravel documentation

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

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

Less than

The field value must be less than another. See the Laravel documentation

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

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

Field::make('name')->nullable()

Required

The field value must not be empty. See the Laravel documentation

Field::make('name')->required()

Same

The field value must be the same as another. See the Laravel documentation

Field::make('password')->same('passwordConfirmation')

Unique

The field value must not exist in the database. See the Laravel documentation

Field::make('email')->unique()

By default, the form's model will be searched, if it is registered. You may specify a custom table name or model to search:

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:

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.

Field::make('email')->unique(ignorable: $ignoredUser)

If you're using the admin panel, you can ignore the current record by passing a closure to the ignorable parameter:

Field::make('email')->unique(ignorable: fn (?Model $record): ?Model => $record)

You can further customize the rule by passing a closure to the callback parameter:

use Illuminate\Validation\Rules\Unique;
 
Field::make('email')
->unique(callback: function (Unique $rule) {
return $rule->where('is_active', 1);
})

Other rules

You may add other 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.

Custom rules

You may use any custom validation rules as you would do in Laravel:

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

You may also use closure rules:

TextInput::make('slug')->rules([
function () {
return function (string $attribute, $value, Closure $fail) {
if ($value === 'foo') {
$fail("The {$attribute} is invalid.");
}
};
},
])

Still need help? Join our Discord community or open a GitHub discussion

Enjoying Filament?

We are open source at heart. To allow us to build new features, fix bugs, and run the community, we require your financial support.

Sponsor Filament on GitHub