Search code examples
phplaravellaravel-6laravel-validation

Laravel 6 validation: there is limit in number of rules?


I am using Laravel 6.13.1.

I have the following validation

$validator = Validator::make($request->all(), [
    'name' => 'required|max:100',
    'email' => 'required|email',
    'mobile_number' => 'required',
    'date_of_birth' => 'required',
    'address' => 'required',
    'category' => 'required',
    'other_category' => 'required_if:category,==,Others',
    'sub_caste' => 'required',
    'photo' => 'required',
    'status' => 'required|integer',
    'father_name' => 'required',
    'father_occupation' => 'required',
]);


if ($validator->fails()) {
    return back()->withErrors($validator)->withInput();                    
}

It has 12 rules and it works. If I add one more rule then the validator stops working.

{{$errors}} gives an empty array in the view file.

Edit 1: The validation with 12 rules shows all error messages, but if I add one more validation like

$validator = Validator::make($request->all(), [
    'name' => 'required|max:100',
    'email' => 'required|email',
    'mobile_number' => 'required',
    'date_of_birth' => 'required',
    'address' => 'required',
    'category' => 'required',
    'other_category' => 'required_if:category,==,Others',
    'sub_caste' => 'required',
    'photo' => 'required',
    'status' => 'required|integer',
    'father_name' => 'required',
    'father_occupation' => 'required',
    'mother_name' => 'required',         
]);

then no error messages. {{$errors}} is an empty array.

In my view, I am using the following code to list errors

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

Edit 2: I tried the same validation on Laravel 5.5 and it works well as indented.


Solution

  • I don't think that it has a limit, However you can create a request and put all your validation on it like that:

    php artisan make:request UserRequest
    

    You can check out this link Form Request Validation