# Validation

Unless you like to live dangerously, any Nova fields that are displayed on the Nova creation / update screens will need some validation. Thankfully, it's a cinch to attach all of the Laravel validation rules you're familiar with to your Nova resource fields. Let's get started.

# Attaching Rules

When defining a field on a resource, you may use the rules method to attach validation rules (opens new window) to the field:

Text::make('Name')
    ->sortable()
    ->rules('required', 'max:255'),

Of course, if you are leveraging Laravel's support for validation rule objects (opens new window), you may attach those to resources as well:

use App\Rules\ValidState;

Text::make('State')
    ->sortable()
    ->rules('required', new ValidState),

Additionally, you may use custom Closure rules (opens new window) to validate your resource fields:

Text::make('State')
    ->sortable()
    ->rules('required', function($attribute, $value, $fail) {
        if (strtoupper($value) !== $value) {
            return $fail('The '.$attribute.' field must be uppercase.');
        }
    });

# Creation Rules

If you would like to define rules that only apply when a resource is being created, you may use the creationRules method:

Text::make('Email')
    ->sortable()
    ->rules('required', 'email', 'max:255')
    ->creationRules('unique:users,email')
    ->updateRules('unique:users,email,{{resourceId}}'),

# Update Rules

Likewise, if you would like to define rules that only apply when a resource is being updated, you may use the updateRules method. If necessary, you may use resourceId place-holder within your rule definition. This place-holder will automatically be replaced with the primary key of the resource being updated:

Text::make('Email')
    ->sortable()
    ->rules('required', 'email', 'max:255')
    ->creationRules('unique:users,email')
    ->updateRules('unique:users,email,{{resourceId}}'),
Last Updated: 10/26/2020, 6:59:37 PM