Validation

Statamic allows you to validate your data using Laravel's validation system.

Overview

While configuring a blueprint or fieldset field, switch to the Validation tab where you can choose from any built in Laravel rule.

Field validation
Add validation rules (with a shortcut for requiring)

In this screenshot, you can see that the field has an alpha_dash and min:4 rule which means you can only type letters and dashes, like a slug, and that it
must be at least 4 characters. You have plenty of options to be creative and confident that your data will be entered the way you need it to be.

Here's a peek at how that YAML is structured.

-
handle: your_field
field:
type: text
validate:
- alpha_dash
- 'min:4'
Hot Tip!

If you're interested in customizing user password validation, you can read about that here.

Required Fields

Being the most common type of validation rule, we give you a shortcut for that. Simply toggle it on, or add required: true to the YAML.

Validating Nestable Fields

Statamic's Grid, Replicator, and Bard fields can all contain sub-fields.

You can add validation to those sub-fields as you would with any top level fields.

However, if you want to use validation rules that target other fields, and the target field is in the same context (e.g. in the same Grid row, or same Replicator set), you may use the {this} placeholder. The path to the nested field will be expanded by Statamic for you.

-
handle: variations
field:
type: grid
fields:
-
handle: purchasable
type: toggle
-
handle: price
type: integer
validate: 'required_with:{this}.purchasable'

This would make sure that the price field is only required if the purchasable toggle is true within that same set. Without {this}, it would be checking for a purchasable field at the top level of your form.

Best Practice

Rather than reaching for {this}, you can consider using conditional fields along with the sometimes|required rules.

fields:
-
handle: purchasable
type: toggle
-
handle: price
type: integer
if:
purchasable: true
validate:
- sometimes
- required

Available Rules

All Laravel Rules

You may use any validation rule provided by Laravel. You can view the complete list on their documentation.

Unique Entry Value

-
handle: highlander
field:
type: text
validate: 'unique_entry_value:{collection},{id},{site}'

This works like Laravel's unique validation rule, but for Statamic entries. The rule should be used verbatim as shown above. Statamic will replace the collection, id, and site behind the scenes.

You can then customize the error message right in your resources/lang/{lang}/validation.php file, like so

'custom' => [
'highlander' => [
'unique_entry_value' => 'There can be only one!',
]
],

Unique Term Value

-
handle: foo
field:
type: text
validate: 'unique_term_value:{taxonomy},{id},{site}'

This works like the unique_entry_value rule, but for taxonomy terms.

Unique User Value

-
handle: login
field:
type: text
validate: 'unique_user_value:{id}'

This works like the unique_entry_value rule, but for users.

Hot Tip!

If you want to override the field that is being validated (e.g. in Livewire Form Objects), you can do so by passing a second parameter to the validation rule, such as unique_user_value:{id},username.

Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →