Unless you like to live dangerously, any Nova fields that are displayed on the Nova creation / update pages 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.
When defining a field on a resource, you may use the rules
method to attach validation rules to the field:
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
Of course, if you are leveraging Laravel's support for validation rule objects, you may attach those to resources as well:
use App\Rules\ValidState;
Text::make('State')
->sortable()
->rules('required', new ValidState),
You may also provide rules to the rules
method via an array or Closure:
// Using an array...
Text::make('State')->rules(['required', new ValidState]),
// Using a Closure...
Text::make('State')->rules(fn ($request) => [
'required',
new ValidState(),
]);
Additionally, you may use custom closure rules 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.');
}
}),
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}}'),
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}}'),
Nova also provides several methods that allow you to perform tasks after a resource has been validated, providing the opportunity to perform more custom validation before the resource is persisted to the database:
afterValidation
Method The afterValidation
method will always be called after a resource has been validated during its creation or during an update. This method will be called before calling afterCreationValidation
or afterUpdateValidation
:
/**
* Handle any post-validation processing.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
protected static function afterValidation(NovaRequest $request, $validator)
{
if (self::somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
}
afterCreationValidation
Method The afterCreationValidation
method will be called after a resource that is being created has been validated:
/**
* Handle any post-creation validation processing.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
protected static function afterCreationValidation(NovaRequest $request, $validator)
{
if (self::somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
}
afterUpdateValidation
Method The afterUpdateValidation
method will be called after a resource that is being updated has been validated:
/**
* Handle any post-update validation processing.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
protected static function afterUpdateValidation(NovaRequest $request, $validator)
{
if (self::somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
}