Laravel 5.8 Form Validation Tutorial With Example

In this tutorial you will learn about the Laravel 5.8 Form Validation Tutorial With Example and its application with practical example.

Laravel 5.8 Form Validation Tutorial With Example

Form Input validation is a process where we check whether the input data provided by user or client is in correct format or not. Input validation is always a best practice to avoid potential errors.Generally Form validation is performed at server side, but can be performed at both the server and client side. In this laravel form validation tutorial we will be learning about laravel server side validations. In this tutorial, I’ll show you how to define laravel validation rules, how to validate form input and to display validation errors.

Laravel 5 comes many built-in validation rules that can be easily implemented. In this example, we have used some of following validation rules –

required:- It set input field is required.

min:- Validate minimum character length.

max:- Validate maximum character length.

email: It set input must be a valid email address.

unique: It checks for unique value against a database column.

numeric: Input value must numeric.

same:- It validates two input fields value must be same.

Before starting with example I assume that you already have fresh laravel 5.8 installation ready, if you have not installed it yet you can follow laravel 5.8 installation instruction here.

Create Laravel Controller

Next, we have to create a controller to display contact form and to handle form validation and submit operations. Lets Create a controller named FormValidationController using command given below –

Once the above command executed, it will create a controller file FormValidationController.php in app/Http/Controllers/formValidationExample directory.

Open the formValidationExample/FormValidationController.php file and put the following code in it.

app/Http/Controllers/formValidationExample/FormValidationController.php

In this controller, we have following methods –

index() :- To display form.

store() :- To validate and submit form data.

Create Laravel View Files

In this step, we will create laravel view/blade file to perform display form and to show errors if any. Lets create a blade file “index.blade.php” in “resources/views/formValidationExample/” directory and put the following code in it respectively.

resources/views/formValidationExample/index.blade.php

Define Laravel Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Start Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/laravel-form-validation-example

Output 1:-

laravel-5-8-form-validation-example-1

Output 2:-

Validate form data and display error messages.

laravel-5-8-form-validation-example-2

Output 3:-

Form data submitted successfully.

laravel-5-8-form-validation-example-3

In this tutorial we have learn about the Laravel 5.8 Form Validation Tutorial With Example and its application with practical example. I hope you will like this tutorial.