Ad

Laravel 6 Validation: There Is Limit In Number Of Rules?

- 1 answer

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.

Ad

Answer

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

Ad
source: stackoverflow.com
Ad