Header Ads

How to apply Laravel Nova Validation to submit and update form



If we develop a system without validation form is dangerous.  Nov cares about creation and Updates on the form. for example 


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



The code you provided is an example of how to define a field using the Laravel Nova `Text` field class. Let's break it down:

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

 `Text::make('Email')`: This creates a new instance of the `Text` field with the label "Email". It represents a text input field in the Laravel Nova form.

 `->sortable()`: This method enables sorting for the field. When applied, a sortable icon will appear in the table header of the corresponding column in the Nova resource index view.

 ->rules('required', 'email', 'max:255')`: This method sets validation rules for the field. In this example, the field is required, must be a valid email format, and should not exceed 255 characters in length.

 `->creationRules('unique:users,email')`: This method specifies additional validation rules that apply specifically during resource creation. In this case, it checks that the email field is unique among the `users` table in the database.

` ->updateRules('unique:users,email,{{resourceId}}')`: This method specifies additional validation rules that apply specifically during resource updating. It also checks that the email field is unique among the `users` table, excluding the current resource being updated (`{{resourceId}}` represents the ID of the current resource).

By chaining these methods together, you define a `Text` field named "Email" with various features and validation rules in a Laravel Nova resource. You can further customize the field by adding other methods or properties based on your specific requirements.


Some mothers validation use in nova are: 

The methods you mentioned (`->rules()`, `->creationRules()`, `->updateRules()`, `->afterValidation()`, `->afterCreationValidation()`, and `->afterUpdateValidation()`) are used in Laravel Nova to define validation rules and perform additional validation logic for fields in a resource's form.

Here's a brief explanation of each method:

1. `->rules('rule1', 'rule2', ...)`: This method sets the validation rules for the field. The provided rules should be strings representing the validation rules that apply to the field. Multiple rules can be specified by separating them with commas.

2. `->creationRules('rule1', 'rule2', ...)`: This method sets additional validation rules that apply specifically during resource creation. These rules will be merged with the rules set by `->rules()`. The provided rules can be specific to the creation process and can be different from the rules used for updating existing resources.

3. `->updateRules('rule1', 'rule2', ...)`: This method sets additional validation rules that apply specifically during resource updating. These rules will be merged with the rules set by `->rules()`. The provided rules can be specific to the updating process and can be different from the rules used for creating new resources.

4. `->afterValidation(callback)`: This method allows you to specify a callback function that will be executed after the field's validation rules have been applied. You can use this callback to perform additional custom validation logic or modify the field's value based on the validated data.

1. `->afterCreationValidation(callback)`: This method allows you to specify a callback function that will be executed after the field's validation rules have been applied during resource creation only. You can use this callback to perform additional custom validation logic or modify the field's value based on the validated data specific to the creation process.

5. `->afterUpdateValidation(callback)`: This method allows you to specify a callback function that will be executed after the field's validation rules have been applied during resource updating only. You can use this callback to perform additional custom validation logic or modify the field's value based on the validated data specific to the updating process.

By using these methods, you can customize the validation rules and add custom validation logic to your Laravel Nova resource fields, providing more control over the validation process.


->rules()
->creattionRules()
->updateRules()
->afterValidation()
->afterCreationValidation()
->afterUpdateValidation()

No comments:

Powered by Blogger.