Validation
Nova provides a variety of ways to validate your resource fields.
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.
Rules
Attaching Rules
When defining a field on a resource, you may use the rules
method to attach validation rules to the field:
Of course, if you are leveraging Laravel’s support for validation rule objects, you may attach those to resources as well:
You may also provide rules to the rules
method via an array or Closure:
Additionally, you may use custom closure rules to validate your resource fields:
Creation Rules
If you would like to define rules that only apply when a resource is being created, you may use the creationRules
method:
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:
After Validation Hooks
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:
The 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
:
The afterCreationValidation
Method
The afterCreationValidation
method will be called after a resource that is being created has been validated:
The afterUpdateValidation
Method
The afterUpdateValidation
method will be called after a resource that is being updated has been validated:
Was this page helpful?