How can I use different RequestsRules on my laravel method according to a route parameter?

11 Views Asked by At

I have to use differents validation rules on a request according to a model property.

I have a post update method which is taking an id parameter.

Update method :


use App\Http\Requests\Client\ModelSettingsRequest as Request;

    public function update($modelId, Request $request)
    {
        /** @var Model $model */
        if(Auth::user()->models()->findOrFail($modelId)->fahr_unit) {
            $model = Auth::user()->models()->findOrFail($modelId)
            ->fill([
                'uc_hot_min' => strval(round(($request->uc_hot_min - 32) / (9/5), 1)),
                'uc_hot_max' => strval(round(($request->uc_hot_max - 32) / (9/5), 1)),
                'uc_cold_min' => strval(round(($request->uc_cold_min - 32) / (9/5), 1)),
                'uc_cold_max' => strval(round(($request->uc_cold_max - 32) / (9/5), 1)),
            ]);
        } else {
            $model = Auth::user()->models()->findOrFail($modelId)
                ->fill($request->only([
                    'uc_hot_min',
                    'uc_hot_max',
                    'uc_cold_min',
                    'uc_cold_max',
                ]));
        }

        if ($model->isDirty()) {
            $model->updated = true;
        }

        $model->save();

        Notify::success(trans('controllersMessages.newSettings'));

        return Redirect::to(route('client.model-settings.show', $modelId).'#modes');
    }

App\Http\Requests\Client\ModelSettingsRequest :

namespace App\Http\Requests\Client;

use App\Http\Requests\Request;

class ModelSettingsRequest extends Request
{
    public function updateRules()
    {
        $this->redirect = $this->redirector->getUrlGenerator()->previous().'#modes';

        return [
            'uc_hot_min' => 'required|numeric|min:16|max:30|lte:uc_hot_max',
            'uc_hot_max' => 'required|numeric|min:16|max:30|gte:uc_hot_min',
            'uc_cold_min' => 'required|numeric|min:16|max:30|lte:uc_cold_max',
            'uc_cold_max' => 'required|numeric|min:16|max:30|gte:uc_cold_min',
        ];
    }
}

and App\Http\Requests to understand how the two previous files are linked :


namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

abstract class Request extends FormRequest
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $func = $this->getActionMethod() . 'Authorize';

        if (method_exists($this, $func)) {
            return call_user_func([$this, $func]);
        }

        return true;
    }


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $func = $this->getActionMethod() . 'Rules';

        if (method_exists($this, $func)) {
            return call_user_func([$this, $func]);
        }

        return [];
    }


    /**
     * Transform the array into string with comma separator
     *
     * @param $array
     *
     * @return string
     */
    protected function arrayToValidation($array)
    {
        return implode(',', (array)$array);
    }


    protected function getActionMethod()
    {
        list($class, $method) = explode('@', $this->route()->getActionName());

        return $method;
    }

}

In updateRules I want to condition the validation rules to the property $model->fahr_unit which is a boolean.

I search some way to pass parameter to updateRules() méthod in order to make an if condition for the rules.

I have to precise that the app is on laravel 5.8.

I'm wondering if the only way to do it is to move the validation rules on my controller where I have access to this parameter.

but I'd rather not have to move all the validation logic...

Have you an idea?

0

There are 0 best solutions below