tallaljamshed's avatar

Laravel custom form request only process 11 or 12 validation rules

I have 18 validation rules and only 12 runs at a time. So when I submit a form it has 18 inputs. I made 18 rules in a custom validation (App\Http\Requests\CustomValidationRequest) . only (random) 12 rules work and returns errors. If I add 13th rule it does returns to previous page but does not return anything in $errors .

0 likes
20 replies
jove's avatar

Do you want us to guess? You need to provide something. Start with the request.

narwy's avatar

What @jove said, Please provide some kind of examples so we can assist you further.

tallaljamshed's avatar

// Full Form //

<form  method="POST" action="{{route('addschoolbranchindb')}}">
        @csrf
        <div class="col-xl-12 order-xl-1 mt-5">
            <div class="card bg-secondary shadow">
                <div class="card-header bg-white border-0">
                    <div class="row align-items-center">
                        <div class="col-8">
                            <h3 class="mb-0">Add School Branch</h3>
                        </div>
                        <div class="col-4 text-right">
                            <input type="submit" value="submit" name="submit" class="btn btn-sm btn-primary">
                        </div>
                    </div>
                </div>
                <div class="card-body">
                   {{ count($errors) }}
                    {{ dd($errors) }}
                    <h6 class="heading-small text-muted mb-4">School information</h6>
                    <div class="row mt-4">
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold; width:100%" class="" for="fk_school_id">Select School</label>
                            </div>
                            <div class="col-md-8">
                                <select class="form-control form-control-alternative fk_school_id" id="fk_school_id" name="fk_school_id">
                                    <option value=""></option>
                                    @foreach ($schools as $school)
                                        <option value="{{$school->school_id}}">{{$school->school_name}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('fk_school_id')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="sc_br_name">Branch Name</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="sc_br_name" name="sc_br_name" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Branch Name">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('sc_br_name')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row mt-4">
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="sc_br_address">School Address</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="sc_br_address" name="sc_br_address" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="School Address">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('sc_br_address')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="sc_br_status">School status</label>
                            </div>
                            <div class="col-md-8">
                                <select class="disable_on_sc_id form-control form-control-alternative sc_br_status" id="sc_br_status" name="sc_br_status">
                                    <option value="1">Un-Registered</option>
                                    <option value="2">Under Process</option>
                                    <option value="3">Registered</option>
                                    {{-- @foreach ($schools as $school)
                                        <option value="{{$school->school_id}}">{{$school->school_name}}</option>
                                    @endforeach --}}
                                </select>
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('sc_br_status')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row mt-4">
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="no_of_boys">No of Boys</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="no_of_boys" name="no_of_boys" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Number of Boys">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('no_of_boys')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="no_of_girls">No of Girls</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="no_of_girls" name="no_of_girls" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Number of girls">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('no_of_girls')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row mt-4">
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="sc_br_covered_area">Covered Area</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="sc_br_covered_area" name="sc_br_covered_area" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Total Covered Area">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('sc_br_covered_area')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="no_of_teachers">No of Teachers</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="no_of_teachers" name="no_of_teachers" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Number Of Teachers">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('no_of_teachers')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>

                    <hr class="my-4" />

                    <h6 class="heading-small text-muted mb-4">Owner information</h6>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="owner_name">Owner Name</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="owner_name" name="owner_name" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Owner Name">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('owner_name')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="col-md-5">
                                <label style="font-weight:bold" class="" for="owner_phone">Owner Phone No</label>
                            </div>
                            <div class="col-md-7">
                                <input type="text" id="owner_phone" name="owner_phone" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Owner Number">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('owner_phone')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row mt-5">
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="owner_email">Owner Email</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="owner_email" name="owner_email" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Owner Email">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('owner_email')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>

                    <hr class="my-4" />

                    <h6 class="heading-small text-muted mb-4">Principal information</h6>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="principal_name">Principal Name</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="principal_name" name="principal_name" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Principal Name">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('principal_name')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="col-md-5">
                                <label style="font-weight:bold" class="" for="principal_phone">Principal Phone No</label>
                            </div>
                            <div class="col-md-7">
                                <input type="text" id="principal_phone" name="principal_phone" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Principal Number">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('principal_phone')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row mt-5">
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="principal_email">Principal Email</label>
                            </div>
                            <div class="col-md-8">
                                <input type="text" id="principal_email" name="principal_email" class="disable_on_sc_id form-control form-control-alternative" 
                                placeholder="Principal Email">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('principal_email')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>

                    <hr class="my-4" />
                        
                    <h6 class="heading-small text-muted mb-4">Area information</h6>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="col-md-4">
                                <label style="font-weight:bold" class="" for="pov_for_subarea">Select Province</label>
                            </div>
                            <div class="col-md-8">
                                <select class="form-control form-control-alternative pov_for_subarea" id="pov_for_subarea" name="pov_for_subarea">
                                    <option value=""></option>
                                    @foreach ($provinces as $province)
                                        <option value="{{$province->province_id}}">{{$province->province_name}}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="col-md-5">
                                <label style="font-weight:bold" class="" for="city_for_subarea">Select City</label>
                            </div>
                            <div class="col-md-7">
                                <select class="form-control form-control-alternative city_for_subarea" id="city_for_subarea" name="city_for_subarea">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="row mt-5">
                        <div class="col-md-6">
                            <div class="col-md-5">
                                <label style="font-weight:bold" class="" for="areaselect">Select Area</label>
                            </div>
                            <div class="col-md-7">
                                <select class="form-control form-control-alternative areaselect" id="areaselect" name="areaselect">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="col-md-5">
                                <label style="font-weight:bold" class="" for="fk_subarea_id">Select Sub-Area</label>
                            </div>
                            <div class="col-md-7">
                                <select class="form-control form-control-alternative fk_subarea_id" id="fk_subarea_id" name="fk_subarea_id">
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="row mt-5">
                        <div class="col-md-6">
                            <div class="col-md-5">
                                <label style="font-weight:bold" class="" for="latitude">Latitude</label>
                            </div>
                            <div class="col-md-7">
                                <input  type="text" id="latitude" name="latitude" class="form-control form-control-alternative" 
                                placeholder="Latitude">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('latitude')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="col-md-5">
                                <label style="font-weight:bold" class="" for="longitude">Longitude</label>
                            </div>
                            <div class="col-md-7">
                                <input  type="text" id="longitude" name="longitude" class="form-control form-control-alternative" 
                                placeholder="Longitude">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('longitude')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row mt-5">
                        <div class="col-md-6">
                            <div class="col-md-5">
                                <label style="font-weight:bold" class="" for="location_string">Location String</label>
                            </div>
                            <div class="col-md-7">
                                <input  type="text" id="location_string" name="location_string" class="form-control form-control-alternative" 
                                placeholder="Location String">
                            </div>
                            <div class="row" style="text-align:center">
                                <div class="col-md-8">
                                    @error('location_string')
                                        <span class="ml-auto mr-auto" style="color:red">{{$message}}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

// the route //

Route::post('/school/addbranch','SchoolbranchController@addSchoolBranchInDb')->name('addschoolbranchindb');

// the controller //

public function addSchoolBranchInDb(SchoolBranchRequest $request)
    {
        $sc_branch = new Schoolbranch;
        $sc_branch->fill($request->all());     
        $sc_branch->save();                                                                                                       
        Session::flash('message','School Branch is added');
        Session::flash('alert-class', 'alert-success'); 
        return redirect()->back();
    }

// the validatorn in App\Http\Requests\SchoolBranchRequest //

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SchoolBranchRequest extends FormRequest
{

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        $rules = [
            'fk_school_id' => 'required',
            'sc_br_name' => 'required',
            'sc_br_address' => 'required',
            'sc_br_status' => 'required',
            'no_of_boys' => 'required',
            'no_of_girls' => 'required',
            'sc_br_covered_area' => 'required',
            'no_of_teachers' => 'required',
            'owner_name' => 'required',
            'owner_phone' => 'required',
            'owner_email' => 'required',
            'principal_name' => 'required',
            'principal_phone' => 'required',
            'principal_email' => 'required',
            // 'fk_subarea_id' => 'required',
            // 'latitude' => 'required',
            // 'longitude' => 'required',
            // 'location_string' => 'required',


        ];
        return $rules;
    }

    public function messages()
    {
        $messages = [
            'fk_school_id.required' => 'School Name is required',
            'sc_br_name.required' => 'School Branch Name is required',
            'sc_br_address.required' => 'School Address is required',
            'sc_br_status.required' => 'School Status is required',
            'no_of_boys.required' => 'Number of boys is required',
            'no_of_girls.required' => 'Number of girls is required',
            'sc_br_covered_area.required' => 'School covered area is required',
            'no_of_teachers.required' => 'Number of teachers is required',
            'owner_name.required' => 'Owner Name is required',
            'owner_phone.required' => 'Owner Phone is required',
            'owner_email.required' => 'Owner Email is required',
            'principal_name.required' => 'Principal Name is required',
            'principal_phone.required' => 'Principal Phone is required',
            'principal_email.required' => 'Principal Email is required',
            // 'fk_subarea_id.required' => 'Subarea is required',
            // 'latitude.required' => 'Latitude is required',
            // 'longitude.required' => 'Longitude is required',
            // 'location_string.required' => 'Location String is required',


        ];
        return $messages;
    }
}

// the 12 errors //

Illuminate\Support\ViewErrorBag {#299 ▼
  #bags: array:1 [▼
    "default" => Illuminate\Support\MessageBag {#300 ▼
      #messages: array:11 [▼
        "fk_school_id" => array:1 [▼
          0 => "School Name is required"
        ]
        "sc_br_name" => array:1 [▶]
        "sc_br_address" => array:1 [▶]
        "no_of_boys" => array:1 [▶]
        "no_of_girls" => array:1 [▶]
        "sc_br_covered_area" => array:1 [▶]
        "no_of_teachers" => array:1 [▶]
        "owner_name" => array:1 [▶]
        "owner_phone" => array:1 [▶]
        "owner_email" => array:1 [▶]
        "principal_name" => array:1 [▶]
      ]
      #format: ":message"
    }
  ]
}

///// but the 13th rules active and no error /////

Illuminate\Support\ViewErrorBag {#301 ▼
  #bags: []
}

//// im using dd($errors) to show this error bag stuff ///

{{ dd($errors) }}
tallaljamshed's avatar

I also tried putting these validation within controller

$validator = validator::make($request->all() , $rules , $messages)->validate(); 

... it all works with other controller and forms but they dont have as many validation rules

jove's avatar

@tallaljamshed Your rules are simple so my guess is some of your fields are named wrong / you don't send them but I have no clue since you decide to not show them. I can't help unless I see more.

Also, tip: Use dd($errors) instead, it's easier to read since it formats it in an array like syntax.

tallaljamshed's avatar

@jove ok sorry for not posting full code it gets too long , but now i edited the previous reply with full code take a look

tallaljamshed's avatar

but the possibility of wrong input names is not there because as i already told u . i used random 12 rules out of 18 . and 12 works at a time . but as soon as i add 13th rule ( ANY 13th RULE ) it just stops returning errors.....

jove's avatar

@tallaljamshed If the error is when you introduce the uncommented rules then that is because they are required but lets take this fk_subarea_id

<select class="form-control form-control-alternative fk_subarea_id" id="fk_subarea_id" name="fk_subarea_id">
                                </select>

That select is empty, so if you require that to be filled (the required) it cannot be empty which it is.

If the issue is as the code stands now, could you dump the request? It may be that you simply did not fill a field?

tallaljamshed's avatar

@jove here is the request

Illuminate\Http\Request {#51 ▼
  #json: null
  #convertedFiles: null
  #userResolver: Closure($guard = null) {#31 ▶}
  #routeResolver: Closure() {#252 ▶}
  +attributes: Symfony\Component\HttpFoundation\ParameterBag {#53 ▶}
  +request: Symfony\Component\HttpFoundation\ParameterBag {#52 ▼
    #parameters: array:22 [▼
      "_token" => "oP2QuJKFc5QkWd0WgbhfEHREIpJ4PunSqUqQElHA"
      "submit" => "submit"
      "fk_school_id" => null
      "sc_br_name" => null
      "sc_br_address" => null
      "sc_br_status" => "1"
      "no_of_boys" => null
      "no_of_girls" => null
      "sc_br_covered_area" => null
      "no_of_teachers" => null
      "owner_name" => null
      "owner_phone" => null
      "owner_email" => null
      "principal_name" => null
      "principal_phone" => null
      "principal_email" => null
      "pov_for_subarea" => null
      "city_for_subarea" => null
      "areaselect" => null
      "latitude" => null
      "longitude" => null
      "location_string" => null
    ]
  }
  +query: Symfony\Component\HttpFoundation\ParameterBag {#59 ▶}
  +server: Symfony\Component\HttpFoundation\ServerBag {#55 ▶}
  +files: Symfony\Component\HttpFoundation\FileBag {#56 ▶}
  +cookies: Symfony\Component\HttpFoundation\ParameterBag {#54 ▶}
  +headers: Symfony\Component\HttpFoundation\HeaderBag {#57 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/school/addbranch"
  #requestUri: "/school/addbranch"
  #baseUrl: ""
  #basePath: null
  #method: "POST"
  #format: null
  #session: Illuminate\Session\Store {#284 ▶}
  #locale: null
  #defaultLocale: "en"
  -preferredFormat: null
  -isHostValid: true
  -isForwardedValid: true
  basePath: ""
  format: "html"
}

and btw even if i introduce latitude which is just an input and not select . it shows same behavior.

ALSO TELL ME if there is any way i can show all the data coming into my view... it looks like with 13 rules . it is still validating and returning to previous page but not returning any messages within $error. i also tried both ways (giving custom messages) and (without custome messages)

jove's avatar

But all your values are null but you require them to be required so they have to be filled. If you would like to allow nullable values you need to use nullable, see https://laravel.com/docs/master/validation#rule-required

The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:

  • The value is null.
  • The value is an empty string.
  • The value is an empty array or empty Countable object.
  • The value is an uploaded file with no path.

Also see https://laravel.com/docs/master/validation#rule-nullable

tallaljamshed's avatar

ohoooo .... I KNOW they are null ...... and i WANT it to SHOW ME ERRORS when they are null .... it is showing me errors for only 12 rules ..... as soon as i add 13th rule ..... it stops showing me ERRORS ...... I also tried (automatic redirection) AND (manual redirection) after validation ....... nothing changes :( @jove

also how did u add grey box , my code is adding

 black box
jove's avatar

Then I'm actually not sure what is going on here, maybe @nakov or @snapey has the answer for this? They seem to know a lot more about the request feature of Laravel than I do.

The gray box is just a multiline quote, if you want to learn more about these check out the markdown guide here: https://guides.github.com/features/mastering-markdown/ Note that the quote is to quote stuff, not to put code in.

tallaljamshed's avatar

alright thanks this is my first question u taught me alot :) ... where are these people @nakov @snapey can u ask them to take a look

jove's avatar

They have been tagged so they will see, if they can answer they will probably comment :)

Nakov's avatar
Nakov
Best Answer
Level 73

@tallaljamshed there is no limit on the errors that get returned. So the only limitation might be for the Session driver that you use, because the Error messages are returned back to the view from the session. So just test using

SESSION_DRIVER=file

in your .env file in case that's not what you already have. I am using this, tried your view and your rules, and it passes as it should. Here is a screenshot of the results with 16 errors: https://imgur.com/qLJbezu

1 like
tallaljamshed's avatar

Thanks man that worked had no idea of that . So by default it was cookies. Does this mean cookies don't store enough data ? That's why I was getting all errors.

Nakov's avatar

Yeah, the max size for a cookie is 4kb. And you were exceeding that.

And for Laravel > 6.0 cookie is a default session driver.

Glad it worked out :)

1 like

Please or to participate in this conversation.