5

I am using laravel 5.6 resources controllers and form request the problem is that i have some inputs that are required on created, but on edit are optionals like file inputs. So i have this form request

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ProgramRequest 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 [
            //
            'name.*'        => 'required',
            'description.*' => 'required',
            'logo'          => 'required|image|max:3000',
            'logo_alt'      => 'required|image|max:3000'
        ];
    }
}

the fields logo and logo_alt must be sent when creating the program, but when editing it sending a logo is optional.

is there a way to validate both cases with the same form request or i have to create a different form request for editing and for creating?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Carlos Salazar
  • 1,818
  • 5
  • 25
  • 48

3 Answers3

7

You can use $this->method() to check which request method has been used and show different rules for each case:

public function rules()
    {
        switch($this->method())
        {
            case 'GET':
            case 'DELETE':
            {
                return [];
            }
            case 'POST':
            {
                 return [
                   'name.*'        => 'required',
                   'description.*' => 'required',
                   'logo'          => 'required|image|max:3000',
                   'logo_alt'      => 'required|image|max:3000'
                ];
            }
            case 'PUT':
            {
                return [
                   'description.*' => 'required',
                   'logo'          => 'nullable|image|max:3000',
                   'logo_alt'      => 'nullable|image|max:3000'
                ];
            }
            case 'PATCH':
            {
                return [];
            }
            default:break;
        }
    }

In this above example the POST will be for your create and the PUT will be for your update.

Notice I've used nullable for the PUT validation rules, this tells the request object that the field is optional.

user3574492
  • 6,225
  • 9
  • 52
  • 105
  • Is there any way so we no need to repeat the validation? something like use switch case put only for field description. Thank you for this answer btw – Bariq Dharmawan Jun 14 '20 at 19:35
3

Instead of:

 return [
            //
            'name.*'        => 'required',
            'description.*' => 'required',
            'logo'          => 'required|image|max:3000',
            'logo_alt'      => 'required|image|max:3000'
        ];

you can use:

$rules =  [
    'name.*'        => 'required',
    'description.*' => 'required',
    'logo'          => ['image', 'max:3000'],
    'logo_alt'      => ['image', 'max:3000'],
];

if ($this->isMethod('POST')
{
   $rules['logo'][] = 'required';
   $rules['logo_alt'][] = 'required';
}

return $rules;

So basically you have rules for update but in addition for POST method you make logo and logo_alt required. You could use pipe syntax | too, but it's more convenient to use array syntax for rules so you can later do such things when needed.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
1

I know I'm late. But I found some better solustion like this

$requiredOrNull = '';
switch ($this->method()) {
    case 'POST':
        $requiredOrNull = 'nullable';
        break;
    case 'PUT':
        $requiredOrNull = 'required';
        break;
}
return [
      //
      'name.*'        => 'required',
      'description.*' => 'required',
      'logo'          => $requiredOrNull . '|image|max:3000',
      'logo_alt'      => $requiredOrNull . '|image|max:3000'
  ];
}

Basically it check if the method is post, your logo and logo_alt would be required, but if the method is put, it would be nullable

Bariq Dharmawan
  • 755
  • 4
  • 16
  • 26
  • I think you need to remove `required` from the last two lines because they are filled with the variable declared inside switch statement. – VijayRana May 12 '22 at 10:41