Laravel 5 Form request, require input on create, but optional on edit
Asked Answered
M

3

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?

Moreville answered 23/6, 2018 at 16:21 Comment(0)
C
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.

Chao answered 23/6, 2018 at 16:25 Comment(1)
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 btwManriquez
K
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.

Keven answered 23/6, 2018 at 16:38 Comment(0)
M
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

Manriquez answered 17/6, 2020 at 16:4 Comment(1)
I think you need to remove required from the last two lines because they are filled with the variable declared inside switch statement.Tectonic

© 2022 - 2024 — McMap. All rights reserved.