validationrequestlaravel-8laravel-formrequest

get request type when use FormRequest


My Action In Controller all action in my project On this pattern

public function Add(Request $request)
    {
        if($request->isMethod('POST'))
        {
            #code........
        }
        else{
            #code........
        }
    }

I Use FormRequest

 class TestValidation 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(Request $request)
    {
        if($request->isMethod('POST'))
        {
           
                return [
                **cod.........**
            ]; 
        }    
    }
    public function messages()
    {
        return [
           **code.....**
        ];
    }

}

all action in my project check if the request post or get

I want to use Test Validation just in case $request POST

How can I do that?


Solution

  • Laravel's FormRequest extends from Request and for this reason you can use that inside the TestValidation:

      if ($this->method() == 'POST') {
         // validation related to Create action
      }