Skip to main content

Command Palette

Search for a command to run...

Laravel 12 Array Validation Tutorial

Laravel 12 Array Validation Example

Published
3 min read
Laravel 12 Array Validation Tutorial
I

Experienced Web Developer | Laravel | PHP | jQuery | HTML | CSS I’m a skilled web developer with extensive experience building dynamic and responsive web applications using Laravel, PHP, jQuery, HTML, and CSS. I specialize in creating clean, scalable, and maintainable code, with a focus on performance and user experience. Whether it's developing RESTful APIs, building custom CMS features, or crafting responsive front-ends, I bring a problem-solving mindset and a passion for quality to every project.

In this Laravel 12 guide, you’ll discover how to validate array-type inputs effectively using real-world examples.

Laravel’s validation system comes with robust rules that make handling array-based form fields—like multiple checkboxes, dynamic input sets, or nested structures—simple and efficient.

Table of Contents

  • Understanding Array Validation

  • Benefits of Array Validation

  • Basic Array Validation Example

  • Nested Array Validation

  • Validating Array Structures

  • Custom Validation Messages

  • Final Thoughts

Understanding Array Validation

Array validation in Laravel allows developers to verify inputs that arrive as arrays—such as multiple uploaded files, repeated form fields, or collections of data like product lists or addresses.

Laravel simplifies this process by offering concise validation syntax, eliminating the need for manual loops or custom logic.

Benefits of Array Validation

In today’s web applications, forms often contain dynamic or repeatable elements that send data as arrays.

For instance:

  • Selecting multiple tags or categories

  • Adding multiple phone numbers dynamically

  • Handling JavaScript-generated form fields

Laravel provides convenient rules such as array, min, max, and distinct, as well as support for dot notation to validate nested data easily.

Basic Array Validation Example

Step 1: Create a Blade View

Let’s start by creating a view file named form.blade.php that includes a form allowing multiple image uploads.

We’ll use the enctype="multipart/form-data" attribute for file uploads and the input name product_imgs[] with the multiple option. Each image will be validated automatically by Laravel based on the rules defined in the controller.

Here’s how your Blade file can look:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Laravel 12 Array Validation Example - ItStuffSolutiotions</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap 5.3.8 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="card mt-5 shadow-lg border-0">
        <h3 class="card-header p-3 bg-primary text-white">
            Laravel 12 Array Validation Example - ItStuffSolutiotions
        </h3>
        <div class="card-body">
           @if ($errors->any())
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            @session('success')
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                    {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endsession
            <form method="POST" action="{{ route('validate') }}" enctype="multipart/form-data">
                @csrf
                <div class="mb-3">
                    <label class="form-label">Images:</label>
                    <input 
                        type="file"
                        name="product_imgs[]"
                        class="form-control "
                        multiple
                        >                     

                </div>               

                <div class="mb-3">
                    <button class="btn btn-success btn-submit">Submit</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Bootstrap 5.3.8 JS (with Popper) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Step 2: Controller Logic for Validation

Next, let’s define the validation logic inside a controller.

Create a UserController and include the following methods:

<?php

namespace App\Http\Controllers;

use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;


class UserController extends Controller
{    
     /**
     * Show the application form.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(): View
    {
        return view('form');
    }

     /**
     * Store data.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request): RedirectResponse
    {

            $request->validate([
            'product_imgs' => 'required|array|min:2|max:5',
            'product_imgs.*' => 'required|image|mimes:jpg,jpeg,png|max:2048',
            ]);



        return back()->with('success', 'Product images saved successfully.');
    }
}

How It Works

  • product_imgs → Must be an array and contain between 2 and 5 files.

  • product_imgs.* → Validates each file in the array individually. Each file must:

    • Be present (required)

    • Be a valid image file

    • Have one of the allowed formats: jpg, jpeg, or png

    • Not exceed 2MB in size

This setup ensures users upload 2–5 valid image files, helping maintain clean and safe uploads in your Laravel application.

👉 Read full article here: Laravel 12 Array Validation Example