Validation
Introduction
Validation rules may be added to any field.
In Laravel, validation rules are usually defined in arrays like ['required', 'max:255']
or a combined string like required|max:255
. This is fine if you're exclusively working in the backend with simple form requests. But Filament is also able to give your users frontend validation, so they can fix their mistakes before any backend requests are made.
Filament includes many dedicated validation methods, but you can also use any other Laravel validation rules, including custom validation rules.
NOTE
Some default Laravel validation rules rely on the correct attribute names and won't work when passed via rule()
/rules()
. Use the dedicated validation methods whenever you can.
Available rules
Active URL
The field must have a valid A or AAAA record according to the dns_get_record()
PHP function. See the Laravel documentation.
Field::make('name')->activeUrl()
After (date)
The field value must be a value after a given date. See the Laravel documentation.
Field::make('start_date')->after('tomorrow')
Alternatively, you may pass the name of another field to compare against:
Field::make('start_date')
Field::make('end_date')->after('start_date')
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('start_date')->afterOrEqual('tomorrow')
Alternatively, you may pass the name of another field to compare against:
Field::make('start_date')
Field::make('end_date')->afterOrEqual('start_date')
Alpha
The field must be entirely alphabetic characters. See the Laravel documentation.
Field::make('name')->alpha()
Alpha Dash
The field may have alphanumeric characters, as well as dashes and underscores. See the Laravel documentation.
Field::make('name')->alphaDash()
Alpha Numeric
The field must be entirely alphanumeric characters. See the Laravel documentation.
Field::make('name')->alphaNum()
ASCII
The field must be entirely 7-bit ASCII characters. See the Laravel documentation.
Field::make('name')->ascii()
Before (date)
The field value must be a date before a given date. See the Laravel documentation.
Field::make('start_date')->before('first day of next month')
Alternatively, you may pass the name of another field to compare against:
Field::make('start_date')->before('end_date')
Field::make('end_date')
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('start_date')->beforeOrEqual('end of this month')
Alternatively, you may pass the name of another field to compare against:
Field::make('start_date')->beforeOrEqual('end_date')
Field::make('end_date')
Confirmed
The field must have a matching field of {field}_confirmation
. See the Laravel documentation.
Field::make('password')->confirmed()
Field::make('password_confirmation')
Different
The field value must be different to another. See the Laravel documentation.
Field::make('backup_email')->different('email')
Doesnt Start With
The field must not start with one of the given values. See the Laravel documentation.
Field::make('name')->doesntStartWith(['admin'])
Doesnt End With
The field must not end with one of the given values. See the Laravel documentation.
Field::make('name')->doesntEndWith(['admin'])
Ends With
The field must end with one of the given values. See the Laravel documentation.
Field::make('name')->endsWith(['bot'])