Search code examples
laravellaravel-formrequest

laravel Form request validation return 404 error


i'm new to laravel , hope someone could help me with this problem ,

i've created a request class to validate my inputs . But when the validation fails it doesn't return any error messages instead showing a 404 error.

my request class , recoverIdRequest

namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class recoverIdRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'dob' => 'required',
            'email' => 'required',
        ];
    }
}

and here's my controller : testController

class testController extends Controller
{
/** 
*
* @param  \App\Http\Requests\recoverIdRequest $request
* @return Illuminate\Http\Response
*/
    public function test(recoverIdRequest $request)
    {
       
      $validated = $request->validated();
    
      
        $dob = $request->input('dob');
        $new_dob = Carbon::parse($dob)->format('Y-m-d');
        $email = $request->input('email');
        $exist =Appl_students::where('email', $email)
            ->whereBetween('dob', [$new_dob, $new_dob])
            ->select('id', 'first_name')
            ->first();
        if ($exist == null) {
            return response()->json(['data' => $exist, 'success' => false, 'message' => "User not found"]);
        } else {
            $date_of_birth = Carbon::parse($dob)->format('d-m-Y');
            $institute =Master::select(
              'institution_name', 'website_url', 'institution_place', 
              'institution_post', 'institution_district', 'institution_pin', 
              'institution_state', 'institution_phone', 'institution_email')
              ->first();
                return $institute;
 Mail::to($email)->send(new RecoverApplicationId($exist->id, $date_of_birth, $exist->first_name, $institute->institution_name));
            return response()->json(['data' => $exist, 'success' => true, 'message' => "Application ID has seen sent to registered mail"]);
        }
    }
      
}

and this is the response in postman when the validation fails :

and this is the response in postman when the validation fails :

routes/api.php

Route::group([ 'prefix'=>'modelTesting', ], function() {
    Route::post('test/{id}' [testController::class,'test'])->middleware(['auth:api', 'scope:admin']);
}); 

Solution

  • Resolved

    it was a problem with postman headers,i was able to fix the issue using the following headers :

    Accept: application/json
    X-Requested-With: XMLHttpRequest