ersanserkan
1 month ago
112
1
Laravel

Opposite of $this->merge() in Form Request

Posted 1 month ago by ersanserkan

Hello!

I want to remove name input from validation according to the condition. How can I do this?

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AccountRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation()
    {
        if (auth()->user()->cannot('edit accounts name')) {
            // I want to remove name input from $request->validated()

            // I tried;
            $this->forget('name');
            $this->pull('name');
            unset($this->name);
        }
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'nullable|min:3',
        ];
    }
}

Thanks

Please sign in or create an account to participate in this conversation.