I am using lumen. I want to use Form Request validation in like in laravel

932 views Asked by At
 public function post(Request $request){
        $validator = Validator::make($request->all(),[
            'title'=>'required',
            'job_type_id'=>'required|exists:job_type,id',
            'work_level_id'=>'required|exists:work_level,id',
            'no_of_candidate'=>'required',

         ]);

         if ( $validator->fails()) {
            return $this->validationErrors($validator->errors());
        }
}

this is my validation code which i have written in my controller.I just want to remove this validation from controller. So that i can make same Form Request Validation what we do in the laravel like this

 public function rules()
    {
        return [
            'name'=>'required|unique:course,name'
        ];
    }

    public function messages(){
        return [
            'name.required'=>__('message.validation.course.name'),
            'name.unique'=>__('message.validation.course.unique')
        ];
    }
1

There are 1 answers

0
brunobraga On

Considering you are using Lumen 8x(you can change the version in the link I sent here), from the docs that you can read here:

Form requests are not supported by Lumen. If you would like to use form requests, you should use the full Laravel framework.

And i continuous by saying:

Unlike Laravel, Lumen provides access to the validate method from within Route closures:

use Illuminate\Http\Request;

$router->post('/user', function (Request $request) {
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email|unique:users'
    ]);

    // Store User...
});

And it also says:

Of course, you are free to manually create validator instances using the Validator::make facade method just as you would in Laravel.