Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to shift the Validation from Controller to Request Class in Laravel 5.2.15

I have a very simple Rule method in request class like below.

public function rules()
{
    return [
        'Subject'           => 'required|max:50',
        'Description'       => 'required|max:500',
        'DepartmentID'      => 'required|integer|min:1',
        'PriorityID'        => 'required|integer|min:1'
    ];
}

Inside Controller Action method, below is the code.

private function SaveChanges(\App\Http\Requests\TicketRequest $request) {

    $v = \Validator::make($request->all(), [
    ]);

    $DepartmentAdmins = $this->getDepartmentAdmins();

    //Check if department admin missing then no need to add the record
    if($DepartmentAdmins == null || count($DepartmentAdmins) == 0) {
        $v->errors()->add('MissingAdmins', 'Department admin missing.');
        return redirect()->back()->withErrors($v->errors());
    }
}

Question:

As we can see in the rule method there are 4 form fields. Is there any way to shift the check for Department Admin existence from Controller Action method to request class?

like image 630
Pankaj Avatar asked Apr 09 '16 13:04

Pankaj


1 Answers

Laravel's Request has after hook that can be run after normal validation completes. This is how you can use it in your case:

namespace App\Http\Requests;

use App\Http\Requests\Request;
use App\Models\Property;
use Illuminate\Validation\Validator;

class SomeRequest extends Request
{
    /**
     * Get the validator instance for the request.
     *
     * @return Validator
     */
    protected function getValidatorInstance()
    {
        $instance = parent::getValidatorInstance();
        $instance->after(function ($validator) {
            $this->validateDepartmentAdmins($validator);
        });

        return $instance;
    }

    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'Subject'           => 'required|max:50',
            'Description'       => 'required|max:500',
            'DepartmentID'      => 'required|integer|min:1',
            'PriorityID'        => 'required|integer|min:1'
        ];
    }

    /**
     * @param Validator $validator
     */
    public function validateDepartmentAdmins(Validator $validator)
    {
        $DepartmentAdmins = $this->getDepartmentAdmins();

        //Check if department admin missing then no need to add the record
        if($DepartmentAdmins == null || count($DepartmentAdmins) == 0) {
            $validator->errors()->add('MissingAdmins', 'Department admin missing.');
        }
}

That way you won't have to do any validation in your SaveChanges controller method.

This code is used in Laravel 5.1, but I believe it will work the same in 5.2.

like image 70
Ivan Yarych Avatar answered Sep 28 '22 16:09

Ivan Yarych