Laravel Http request के साथ आने वाले data को validate करने के लिए हमें कई सारी approaches provide कराता है। By Default Laravel Base Controller , Http Request validate करने के लिए ValidatesRequests Trait use करता है।

Laravel Validation Example

माना हमारे पास routes/web.php में एक Post create करने के लिए दो routes define हैं।

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

Next - Post actions handle करने के लिए एक controller PostController बनाते हैं।

File : <>app/Http/Controllers/PostController.php

Copy Fullscreen Close Fullscreen
<?php 
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PostController extends Controller
{
    /**
     * Show the form to create a new blog post.
     *
     * @return Response
     */
    public function create()
    {
        return view('post/create');
    }

    /**
     * Store a new blog post.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
       /* Validate and store the blog post... */
    }
}

Laravel Writing Validation

कुछ इस तरह से आपका Controller होगा। @store method में आपको validation की जरूरत पड़ेगी और अगर input सही है तो इसे database में Save कर सकते हैं।


Laravel में Input fields को validate करने के लिए Illuminate\Http\Request object द्वारा provide किया गया validate() method use करते हैं। अगर validation pass हो जाता है तो आपका code normally Run हो जायगा , और अगर validation fail हो गया तो validation rules के according proper error messages के साथ request back हो जाती है। means हम जहाँ से Form submit किया था वापस वही आ जातें हैं।

 /*PostController@store method*/
public function store(Request $request)
{
    $validatedData = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    // The blog post is valid...now you can save it.
}

अब समझते हैं कि ऊपर क्या लिखा गया है , Laravel में हर एक validation को | से अलग किया जाता है।

title के लिए यहाँ teen validations (required|unique:posts|max:255) हैं -

  1. required : इसका मतलब है कि , title field null नहीं होना चाहिए।
  2. unique : posts means title unique होना चाहिए यह पर "posts" Database Table name है।
  3. max : 255 means title की length 255 से ज्यादा नहीं होना चाहिए।

वही body field के लिए सिर्फ एक ही validation (required) है।

हालाँकि अगर आप चाहे तो validations rules को as a Array भी define कर सकते हैं।

$rules['title']  = "required|unique:posts|max:255";
$rules['body'] = "required";
$request->validate($rules); /*it is same as we just did above*/

Laravel Validation : Stopping On First Validation Failure

अगर आप चाहते हैं कि पहले validation failure पर ही बाकी validations को stop करना चाहें तो required से पहले bail का use कर सकते हैं।

$request->validate([
    'title' => 'bail|required|unique:posts|max:255',
    'body' => 'required',
]);

Laravel Displaying Validation Errors

जैसा आपने ऊपर पढ़ा कि , Validation fail होने पर एक Exception Through होती है है और हम previous request (means जहाँ से Form submit किया था।) पर आ जाते हैं। जहाँ पर हम उन Validation Errors को show करा सकते हैं।

 <!-- /resources/views/post/create.blade.php   -->
<h1>Create Post</h1>

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<!-- Create Post Form -->

Laravel @error Directive

हालाँकि अगर आप सभी Validations errors की एक साथ Listing नहीं करना चाहते तो @error blade directive की help से एक एक करके भी error check कर सकते हैं।
For Example-

@error('title')
    <div class="alert alert-danger">{{ $message }}</div>
@enderror

Laravel Validation With Custom Message

custom error messages के साथ request back करने के लिए validator() function में custom messages array pass कर दिया जाता है।

$rules['title']  = "required|unique:posts|max:255";
$rules['body'] = "required";

$custom_messages['title.required'] = 'Enter post title';
$custom_messages['title.max'] = 'Post title can not be more than 255';
$custom_messages['body.required'] = 'Enter post body conent';
$request->validate($rules, $custom_messages);

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers