Form request before hook

Submitted by percymamedy - 1 month ago

If you need to perform any action before validation when using form request validation, you can use the prepareForValidation() method in your request class.

<?php

namespace App\Http\Requests\Tasks;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class CreateTaskeRequest extends FormRequest
{
    protected $connection;
    
    /**
     * Prepare the data for validation.
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->connection = config('database.another_connection_name');
    }

    /**
     * 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 [
            'issue_id' => [
                'required', 
                Rule::exists($this->connection.'.issues', 'id')
            ]
        ];
    }
}
comments powered by Disqus