简体   繁体   中英

Laravel custom Request validation not working

I am trying to use form Laravel Request validation by creating custom form request. Here is my code snippet

api.php

Route::group(['namespace' => 'User'], function () {
  Route::put('user/{user}', 'UpdateUserGeneralInfoController@UpdateUserGeneralInfo');
});

UpdateUserGeneralInfoController.php

use App\Http\Controllers\Controller;
use App\Http\Requests\User\UpdateUserGeneralInfoRequest;

class UpdateUserGeneralInfoController extends Controller
{
   public function UpdateUserGeneralInfo(UpdateUserGeneralInfoRequest $request,User $user)
   {
      $user->name = $request->name;
      $user->email = $request->email;
      $user->date_of_birth = $request->date_of_birth;
      $user->save();

      $response = [
          'message' => 'User info updated successfully'
      ];

      return response($response, 201);

    }
 }

UpdateUserGeneralInfoRequest.php

namespace App\Http\Requests\User;
use Illuminate\Foundation\Http\FormRequest;

class UpdateUserGeneralInfoRequest extends FormRequest
{
  public function authorize()
  {
     return true;    
  }

  public function rules()
  {
     return [
        'name' => 'required|max:50',
        'email' => 'required',
        'date_Of_birth' => 'required'
     ];
  }

  public function messages()
  {
    return [
        'name.required' => 'Name is required',
        'email.required' => 'An email is required',
        'date_Of_birth' => 'Date of Birth is required'
    ];
  }
}

The folder structure is

在此处输入图像描述

I am trying to test the request through Postman. So here is my json input

{
  "id" : 1,
  "name":"Osman Rafi",
  "email": "rafi@devport.com",
  "current_address": "House-26,Islampur R/A, Mejortila, Sylhet-3100,Bangladesh",
  "bio": "But a man is not made for defeat. A man can be destroyed,but not defeated.",
  "linkedin": "linkedin.com/in/osman-goni-chowdhury-bb421a16b",
  "facebook": "https://www.facebook.com/osman.rafi140",
  "github": "https://github.com/Osman-Rafi",
  "blood_group": "O(+ve)",
  "password": "111111"
}

It showing me

404 | Not Found

It work perfectly if I try this is controller

public function UpdateUserGeneralInfo(Request $request,User $user)
{
}

you have to pass all these 3 variables in the request (you are missing date_Of_birth):

'name' => 'required|max:50',
'email' => 'required',
'date_Of_birth' => 'required'

However, I think your error comes from few other places because 404 not found comes from the route not being found in the link.

have you cleared the route cache after adding the route you are calling:

try

php artisan route:cache

also add

'name.required' => 'Name is required',
'email.required' => 'An email is required',
'date_Of_birth' => 'Date of Birth is required'

date_Of_birth.required

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM