Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 598
Carlos Salazar Avatar asked Jan 28 '23 21:01

Carlos Salazar


2 Answers

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.

like image 123
user3574492 Avatar answered Feb 05 '23 17:02

user3574492


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.

like image 44
Marcin Nabiałek Avatar answered Feb 05 '23 17:02

Marcin Nabiałek