0

For form validation I made a Request class via php artisan make:request UpdatePlanRequest.

However after using the UpdatePlanRequest class in store the method isn't called anymore.

The UpdatePlanRequest:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdatePlanRequest 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()
    {   //TODO: CHECK IF THE PROTOTYPE IDS ARE OWNED BY THE USER (https://stackoverflow.com/questions/42662579/validate-an-array-of-integers/42693970)
        return [
            'start_date' => 'required|date',
            'end_date' => 'required|date|after:start_date',
            'name' => 'required|string'
        ];
    }
}

The controller method:

use App\Http\Requests\UpdatePlanRequest;

public function store(UpdatePlanRequest $request)
    {
        //
        dd('hello');
    }

If the function header is store(Request $request) hello is shown, in that example it isn't.

The custom Request class is necessary to call $request->validated(); later for validation purposes according to the docs.

Lewis Tudor
  • 77
  • 1
  • 8

5 Answers5

0

Is there a reason you have your Request class as being abstract? The default class that is created when running php artisan make:request <name> doesn't define the class as being abstract. This seems to work for me, but not when declaring it as abstract.

Jam1e
  • 43
  • 6
0

$request->validated(); is used to retrieve the validated inputs, so just by calling the UpdatePlanRequest it should validate the request

Mohammad hayajneh
  • 623
  • 2
  • 11
  • 32
0
    //Try This 
    use App\Http\Requests\UpdatePlanRequest;

    public function store(UpdatePlanRequest $request)
        {
           $validatedData = $request->validated();
           dd('$validatedData');
           $profile = new Profile([
            'user_id'              => $request->get('user_id'),

        ]);
        $profile->save();
        echo $request->session()->flash('alert-success', 'Profile details Succefully Added!');
        return redirect('create')->with('success', 'Data saved!');

        }

Your route will be.

Route::get('profile','ProfileController@store');
Route::post('profile/create','ProfileController@store')->name('create');
0

Well this works right! When the method is called, it checks the request class (UpdatePlanRequest). If there is an error, it does not enter the method anymore and you can not see the output of dd() function. If the data is correct after checking the rules, then dd() will be displayed. You must manage errors

0

I also encountered this issue when I needed to return error message from Laravel FormRequest. The main reason is that the failedValidation() method in the FormRequest file will by default redirect when the data you send is invalid. Therefore, we can simply solve the above problem by customizing the failedValidation() method.

Please open the FormRequest.php file and you will see that the failedValidation() method takes a parameter which is a validator. We also see that FormRequest provides us with a getValidatorInstance() method. Here is the code I customized in the BaseRequest.php file. From now on, requests that need to return error messages will inherit from BaseRequest instead of FormRequest.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class BaseRequest extends FormRequest
{
    protected $stopOnFirstFailure = false;

    public function authorize()
    {
        return true;
    }

    public function failedValidation(Validator $validator)
    {
        return $validator->errors()->messages();
    }

    public function getValidator()
    {
        return $this->getValidatorInstance();
    }
}

FormRequest custom file

...

class StoreRequest extends BaseRequest
{
    \\ ...

    public function rule()
    {
        return [];
    }
    
    public function message()
    {
        return [];
    }
}

And in controller file we have

<?php

namespace Modules\User\Http\Controllers;

use Illuminate\Routing\Controller;

class UserController extends Controller
{
    \\ ...

    public function store(StoreRequest $request)
    {
        $errors = $request->failedValidation($request->getValidator());
        if ($errors) return response()->json(['errors' => $errors], 422);
        return $this->service->createUser($data);
    }
}

Good luck!

thep200
  • 29
  • 6