How to validate an array in Laravel?


There are various ways to validate the input data in Laravel. One of those is the Validator class. The first step is that you need to include this class as shown below −

use Illuminate\Support\Facades\Validator;

When you work with an array you need to mention the keys and the rules to be used in the array. For example, consider the following array. Here, we are adding the keys, firstname, lastname, and address along with the values. Using rules we define all these elements are mandatory and of the data type String.

$formData = array(
   'firstname' => 'Siya',
   'lastname' => 'Nadkarni',
   'address' => 'xyz'
);
$rules['firstname'] = 'required|string';
$rules['lastname'] = 'required|string';
$rules['address'] = 'required|string';

Example 1

Let us now test the array using the validator as shown below -

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class testuserip extends Controller { public function index() { $formData = array( 'firstname' => 'Siya', 'lastname' => 'Nadkarni', 'address' => 'xyz', ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; $rules['address'] = 'required|string'; $validator = Validator::make($formData , $rules); if ($validator->fails()) { echo "Invalid Array"; } else { echo "Array Validation is Successful"; } } }

Output

On execution, the above code generates the following output.

Array Validation is Successful

Example 2

Now, let us check another example where we will remove the address field and test it −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class testuserip extends Controller { public function index() { $formData = array( 'firstname' => 'Siya', 'lastname' => 'Nadkarni' ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; $rules['address'] = 'required|string'; $validator = Validator::make($formData , $rules); if ($validator->fails()) { echo "Invalid Array"; } else { echo "Array Validation is Successfull"; } } }

In the above case, the array does not have an address field but the rules say an address field is required, so when you test it using a validator it will fail and this program prints a message saying “Invalid Array” as shown below.

Output

Invalid Array

Example 3

If the keys are mentioned and there is no rule defined, the validator will ignore the key and get it passed.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class testuserip extends Controller { public function index() { $formData = array( 'firstname' => 'Siya', 'lastname' => 'Nadkarni', 'address' => 'xyz', ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; // $rules['address'] = 'required|string'; $validator = Validator::make($formData , $rules); if ($validator->fails()) { echo "Invalid Array"; } else { echo "Array Validation is Successfull"; } } }

Output

Here the rule for address is removed and after testing The output of the above code is as follows −

Array Validation is Successful

Example 4

The following shows how to validate a numeric array. The rules for it are as follows −

$formData = array(10,20,30,40); $validator = \Validator::make(compact('formData'), [ 'formData' => 'required|array', 'formData.*' => 'integer' ]);

The rule applied is to have elements for the array and they have to be integer values.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class testuserip extends Controller { public function index() { $formData = array(10,20,30,40); $validator = \Validator::make(compact('formData'), [ 'formData' => 'required|array', 'formData.*' => 'integer' ]); if ($validator->fails()) { echo "Invalid Array"; } else { echo "Array Validation is Successfull"; } } }

Output

On execution, the above program generates the following output −

Array Validation is Successful

Updated on: 29-Aug-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements