In Laravel, the popular PHP framework, data validation plays a crucial role in ensuring the integrity and reliability of your application. While validating simple input fields is straightforward, dealing with arrays and nested values can be a bit more complex. In this tutorial, we’ll explore how to validate arrays and nested values in Laravel, using practical examples to help you grasp the concepts effectively.
In many scenarios, we have encountered form submissions that include dynamic fields or nested data structures. For instance, consider an online shopping cart where a user can add multiple products with varying quantities. Validating such an array of products and their respective quantities becomes essential to maintain data consistency and prevent any potential issues.
To begin, we need to define the validation rules that our array or nested values should adhere to. In Laravel, you can accomplish this by creating a new validation rule set using the `Validator` facade or by utilizing form requests. Let’s take a practical example to illustrate this.
Let’s say we have an e-commerce platform where users can submit an order with multiple products. Each product consists of a name, price, and quantity. We want to ensure that all the products have a valid name, a positive price, and a non-zero quantity.
Creating Request for Validation
Let’s start by creating a validation request and adding logic to it. Open the terminal and enter the below command in it:
php artisan make:request ProductStoreRequest
This will create a new file in the app/Http/Requests directory. Let’s modify it as per the example:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreOrderRequest extends FormRequest
{
public function rules()
{
return [
'products' => 'required|array',
'products.*.name' => 'required|string',
'products.*.price' => 'required|numeric|min:0',
'products.*.quantity' => 'required|integer|min:1',
];
}
}
Here, we have defined rules for our validation. First of all, we have checked the request has a product key and it must be an array. After that, we will validate sub-elements of the array like the name’s value is required and the data type for the name will be a string. Price and quantity need to be numeric and we have also set minimum values for both.
Implementing the Request Class in the Controller
Next, we will utilize the StoreOrderRequest class in our controller to handle the validation and processing of the submitted data.
Let’s assume we have an OrderController with a store method to handle the order submission. Let’s modify it for storing further:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreOrderRequest;
class OrderController extends Controller
{
public function store(StoreOrderRequest $request)
{
$validatedData = $request->validated();
foreach ($validatedData as $key => $value) {
Order::create($value);
}
return back();
}
}
Here, we have bound request class into our store method. The request will handle validation and if validation fails it will return with validation errors and if everything goes well then execute the code further.
We have taken a loop through validated data and inserted it into the Order model.
When validation fails, Laravel automatically redirects back to the previous page with the errors available for display. You can access these errors in your views or controllers to provide meaningful feedback to the user, highlighting the specific fields that need attention.
Conclusion
Validating arrays and nested values in Laravel is crucial for maintaining data integrity in complex scenarios. By following the practical example, you can ensure that your application’s array-based input data is validated according to your defined rules.