Updated April 12, 2023
Introduction to Laravel Validate Array
In Laravel, we can get a lot of opportunities to validate the incoming data of the application. Validatesrequeststrait is used by default by Laravel with the help of its base controller to process the validation. It is convenient as it is an easier way to validate any incoming HTTP requests. It also holds a good variety of powerful and useful rules of validation.
The two ways of validation in Laravel are:
- Inside the controller action
- With the help of a dedicated form request
When the validation fails due to any technical error, it assists by submitting to the user a clear error report with detailed specifications about the error.
What is Laravel Validate Array?
Laravel’s validation system is a powerful one. You can easily complete the task of validation with the help of Laravel’s technical support. Laravel framework has everything inside itself to complete the process of validation. Though on a usual note, other frameworks provide validation there is always the complexity that tends to complicate the whole process and make things tedious. Meanwhile, in Laravel framework validation is easier and it is less messy compared to other frameworks where applications are created.
Pre-defined validation rules exist in Laravel. It helps the user to make things happen at the click of a button. Most of the web applications on the internet require validation of data. For instance, if there is a particular registration form that needs to be filled in by the users along with their email IDs and password, it needs validation to provide further access. Laravel has standard rules in place within the validator. Also, the benefit of creating a new rule and customizing it as per the needs is possible in ‘validation’ in Laravel.
There are array-based forms that can be generated in Laravel and they are not a pain to be generated. The ‘dot notification’ can be used by the user to validate the attributes that are present within an array. Even each element of the array can be validated. The * character must be used by the user when the user wishes to validate the messages inside it in a specific language of interest. The user can use a single validation message for the fields that are array-based.
1. If there would be an incoming HTTP request with photos field in it, you can validate the data with the help of the following:
$validator = Validator::make ($request->all(), [
'photos.profile' => 'required|image',
]);
2. If there is a need for validation of each of the emails that are available in the array, then the following can be used:
$validator = Validator::make ($request->all(), [
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);
3. When using the character ‘*’ in the process of validation the following can be used:
'custom' => [
'person.*.email' => [
'unique' => 'Each person must have a unique e-mail address',
]
],
How does Laravel Validate Array exactly work?
We have already understood the meaning of Laravel Validate Array and come to understand what exactly the process is and what the benefits of this validation process are. Now let us look into the concern of ‘how’ does this validation actually process and work.
To validate a set of objects or the data in an array, Laravel avails of validation options of various kinds. You can consider all the options that are made available before finally sticking on to one of the options. Form array validation is one such process. It simplifies the validation process by helping to validate data that is unclear which are passed by the HTML forms with the use of array syntax. Let us consider a user who needs to add a ‘company’ and inside that section can add the names of the employees into the company as per the needs. Each employee will surely be having a name and also a particular designation or title.
Considering this, let’s look at the code:
Code:
<form>
<label>Company Name</label>
<input type="text" name="name">
<h3>Employees</h3>
<div class="add-employee">
<label>Employee Name</label>
<input type="text" name="employee[1][name]">
<label>Employee Title</label>
<input type="text" name="employee[1][title]">
</div>
<div class="add-employee">
<label>Employee Name</label>
<input type="text" name="employee[2][name]">
<label>Employee Title</label>
<input type="text" name="employee[2][title]">
</div>
<a href="#" class="js-create-new-add-employee-box">Add another employee</a>
<input type="submit">
</form>
Output:
*With the help of this code, you can create an option to continuously add employees constantly in the validation process to validate the entries.
Similarly, you can validate forms in the Laravel validate Array where there is an array of forms that are available within Laravel.
Example of Laravel Validate Array
When you wish to create an array form and validate it, we can use this example as a reference to understand its working and process.
Here we shall be using dynamic fields to work on the data to be validated. When you wish for the user to add more fields later on so that there can be an addition of data we can try this. An example in the form of an image:
In this above image, there is an option to ‘Add New’ data. This would assist in loading a new input field for entering data and the field name will be the array. With the help of JavaScript, this is possible. A PHP example follows:
Code:
<input type="text" class="form-control" name="name" value="name">
@for ($i=0; $i < 2; $i++)
<input type="text" name="items [{{$i}}]" value="{{ $i }}">
@endforeach
In Laravel you will find the option of Validation:: façade, new form requests, and also Controller Validation. Utilizing Artisan let us create a request for a new form. The system is an automated manner that will generate the file. Run the following code in the terminal:
Code:
$ php artisan make:requestOrderRequest
You will get the message as a request created successfully. The following content will be available at the app/Http/Requests/OrderRequest.php.
Code:
<?php namespace AppHttpRequests;
useAppHttpRequestsRequest;
classOrderRequest extends Request {
public function authorize()
{
return false;
}
public function rules()
{
return [
//
];
}
}
From here on you can then add custom validation rules according to the need and validate the form.
Conclusion
Laravel Validate Array is a very good feature that is available for data validation in Laravel. With its help, users can validate all the necessary array fields which are usually used for forms in the application processing. It is a very good and effective and tension-free process as it is simplified and also has no complications in processing.
Recommended Articles
We hope that this EDUCBA information on “Laravel Validate Array” was beneficial to you. You can view EDUCBA’s recommended articles for more information.