Writing a Blog in Laravel: Validation
We have created a blog post in our previous posts. We have learned how to display a form to create blog post and how to save it in the database.
There were few issues with how we did things in previous post, Let’s fix one of those issues.
Validation
Laravel provides Form Requests to help with validation. These provide one place to put your validation logic in a form. To validate our forms, we just need to create a request class and hint the class in the controller where we are making the post request.
Let’s validate our create post form. To do this, we need to create a form request. Let’s name the form CreatePostForm. Whenever, you think of creating something in laravel, there is command for that.
Note: If you want to see a list of artisan commands that laravel provides, you can simply run
php artisan
php artisan make:request CreatePostFormAfter running this command, a file will be created at app/Http/Requests/CreatePostForm.php with content.
Let me explain the content of the file.
public function authorize()
{
return false;
}this function contains the logic to check authority of the form being posted. Typically this will check if the currently logged in user can post the form. Since, we allow any logged in user to submit the form, we can simply return true here.
public function rules()
{
return [
//
];
}This function contains the rules for the form validation in the form of an array. Key of the array corresponds to the name of the form field and the value is the rules that the form field should comply to. For eg.
public function rules()
{
return [
'title' => 'required',
'content' => 'required'
];
}These rules mean that title is required and content is also required.
The full content of the file after modification would look like this.
Now, we have decided on the rules, we need to implement this in the view file. Whenever we submit the file and get validation error, it needs to be shown to the user.
For the form to take affect on posting the form, we simply need to hint the class in the function we are posting the request. In our case, the function we are posting the request is PostController@store
In the file app/Http/Controllers/PostController.php , simply replace the Request class we type-hinted to our newly create class CreatePostForm.
As you can see we simply hinted the class in the function and added a use statement to import the validation class.
As for the view file, laravel provides an errors variable that stores all the validation errors. It is a ParamBag of errors. To get error of a form field we can simply use $errors->get('field') . This will give us all the errors for that field. To get the first error for that field, we can use $errors->first('field') . We can also check if there exists an error for a field with $errors->has('field').
Implementing this in our view file would look like this.
After this change, if we submit our form without any content, it would look like this

In the outer div of the form, we are checking if the field has error and adding a has-error class in it with this code
<div class="form-group @if($errors->has('title')) has-error @endif">and displaying the first error of the field in a span, with this code
<span class="text-danger">{{ $errors->first('title') }}</span>This is it for validation, Laravel makes it very easy. The changes made during this is here.