Search code examples
phplaravel-5.3

Request Validation with Laravel 5.3 for Multiple rows


My Request File rules are

 public function rules()
{
    return [
    'mobile' => 'required',
    'code' => 'required',
    ];
}

My input data can be a simple => for which the request validation is working fine.

  {
    "mobile":"81452569",
    "code":"4858"
}

My input data can be a complex too => for which the request validation is not working fine.

[{
    "mobile":"81452569",
    "code":"4858"
},
{
    "mobile":"81452570",
    "code":"4858"
}]

How to validate for multiple rows with request.


Solution

  • I would sent it all in array to the request object like so:

    "data" => [
      [
        "mobile" => "81452569",
        "code" => "4858"
      ],
      [
         "mobile" =>"81452570",
         "code" =>"4858"
      ]
    ];
    

    Then in your validation rules do this:

     public function rules()
     {
        return [
            'data.*.mobile' => 'required',
            'data.*.code' => 'required',
        ];
     }