How To Do Validation Arrays In Laravel With Example

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

Required field will check whether the field is filled or not in the proper way. Most of cases we will use the * symbol for required field.Validation means check the input submitted by the user. There are two types of validation are available in Laravel

How To Do Validation Arrays In Laravel With Example

Validating array based form input fields doesn't have to be a pain. You may use "dot notation" to validate attributes within an array. For example, if the incoming HTTP request contains a photos[profile] field, you may validate it like so:
 

$validator = Validator::make($request->all(), [
'photos.profile' => 'required|image',
]);
 

You may also validate each element of an array. For example, to validate that each e-mail in a given array input field is unique, you may do the following:

$validator = Validator::make($request->all(), [
      'person.*.email' => 'email|unique:users',
      'person.*.first_name' => 'required_with:person.*.last_name',
]);
 

Likewise, you may use the * character when specifying your validation messages in your language files, making it a breeze to use a single validation message for array based fields:
 

'custom' => [
'person.*.email' => [
'unique' => 'Each person must have a unique e-mail address',
]
],


Example(1)

1.Create Route in web.php  as below created

Route::get('post/create', 'PostController@create');

Route::post('post', 'PostController@store');


2.Create PostController using below command

php artisan make:controller PostController


3.Open app\Http\Controller\PostController.php and implement below code in it

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PostController extends Controller
{
    public function Create(){

        return view('file');
    }
    public function store(Request $request)
    {      
        $validator = Validator::make($request->all(), [
            'photos.profile' => 'required|image',
        ]);

        if ($validator->fails()) {
            return redirect('post/create')
                ->withErrors($validator)
                ->withInput();
        }

        // Store the blog post...
        return 'done';
    }
}


4.Now Create and implement below code in file.blade.php  file

<!DOCTYPE html>
<html>
<head>
    <title>Validating Arrays In laravel</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>Validating Arrays In laravel</h1>
    </div>
    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    <div class="form-group col-sm-2"></div>
    <div class="form-group col-sm-8">
        <div class="well">
            <form method="post" action="{{url('post')}}" enctype="multipart/form-data">
                @csrf
                <div class="form-group col-sm-12">
                    <label>file</label>
                    <input  class="form-control" type="file" name="file" value="">
                </div>
                <div class="form-group text-center">
                    <input  class="btn btn-primary" type="submit" >
                </div>
            </form>
            <div class="form-group col-sm-2"></div>
        </div>
    </div>
    <br>
</div>
</body>
</html>


5.Pass below url to see output

http://localhost/laraveldemoproject/public/post/create

 

Related Post