Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

Form Validation in Laravel5: The Right Way

Laravel 5.0 introduces Form Requests, which are a special type of class devoted to validating and authorizing form submissions. Each class contains at least a rules() method which returns an array of rules and an authorize() method which returns a true or false (Boolean) of whether or not the user is authorized to perform their request.

In this tutorial I am going to show you form validation with Form Request class.By using this technique we are validating data with out hitting the actual controller post method(Example:store()).

create routes :app/Http/routes.php

Route::get('/post/create', '[email protected]');
Route::post('/post/store', '[email protected]');

Create a controller – app/Http/controllers/PostController.php

Artisan command to generate post Controller, or else you can create post controller manually using any text editor.

php artisan make:controller PostController

In the postController we have imported CreatePostRequest class with use App\Http\Requests\CreatePostRequest and we are using method injection technique(public function store(CreatePostRequest $request)) since we are using CreatePostRequest class only while creating new post. so we are injecting that into sore method.

Create view to show form - resources/views/post/create.blade.php

HTML to show the form.


Create Post
@if (count($errors) > 0)
Whoops! There were some problems with your input.

    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach

Create FormRequest class - app/http/requests/CreatePostRequest.php

Artisan command to create form request class , or you can create manually.

php artisan make:request CreatePostRequest

route->parameter('id'); Here id is the route parameter EX: Route::get('post/view/{id}','[email protected]');
		return true;

	 * Get the validation rules that apply to the request.
	 * @return array
	public function rules()
        return [
            'title' => 'required',
            'content' => 'required'


Now go to your browser and access web page by pointing to http://localhost/pubilc/post/create, you will get form. Submit it with out filling information ,you will get error messages. See the demo.

That is it. I hope you find this post useful. If you have questions or want to share your own advice, leave a comment!

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments