Laravel Validation

Validation is the process of checking the incoming data. By default, laravel provides the base controller class that uses the ValidatesRequests trait to validate all the incoming Http requests.

Let's understand the validation through an example.

We will create an application in which we add the name of the student.

  • First, we create the new laravel project in which we perform the validation. Enter the command which is given below in a command-line tool

composer create-project laravel/laravel=5.8 student_app -prefer-dist;

Laravel Validation

The above output shows that the student_app project has been created successfully in the xampp/htdocs directory.

  • After the creation of a project, we will first create a model with the database migration.
Laravel Validation 1
  • The above statement creates a model 'Student' in app folder, and 'create_students_table' in Migrations folder. The structure of 'create_students_table.php' file is given below:

The above code creates a table 'students' that has four columns (id, name, created_at, updated_at).

Data available in the user table:

user table

Laravel Validation 2
  • Migrate the above changes in a database by using the command given below:

php artisan migrate;

Laravel Validation 3
  • Now, we create a controller that handles all the database operations.
Laravel Validation 4
  • After creating a controller, we will create the route of all the methods of a controller. To create a route, we need to write the following code in web.php file:

Route::resource('student','StudentController');

Laravel Validation 5
  • In this step, we will define the index() method of StudentController class, and the code of index() method is given below:
  • Now, we create a view page(index.blade.php) of our application.

index.blade.php

Output of the above code is shown below:

Laravel Validation 6

As we know that the URI of the index() method of a StudentController is '/student', so when we hit the url 'localhost/student_app/public/student', it calls the index() method. The index() method returns the view of the index.blade.php file, which is shown in the above screenshot.

  • When we enter the data in the text box shown in the above screenshot, it should be saved in a database. In order to achieve this, the code of the store() function is given below:

Output

Laravel Validation 7

When we click on the 'Add Students' button, and then we refresh the page, the output would be:

Laravel Validation 8

As we can see in the above screenshot that the 'Himanshu' is added in the students list, which means that the 'Add Students' button is working correctly.

Sometimes the situation arises when we do not enter any data, and we press the 'Add Student' button; this requires validation. We added the validation code in the store() method that validates the 'name' field, but we have not displayed any error message. To display the error message, laravel has provided the error variable that displays the error message. It can be used as:

After adding the above line in index.blade.php, the code of index.blade.php file looks like:

index.blade.php

Output

Laravel Validation 9

We can also restrict the characters in the textbox field. If we want to enter atleast 5 characters in the name field, then we can use the min field in a validate function.

Output

Laravel Validation 10
Laravel Validation 11




Latest Courses