How to use form validation in Laravel 6

How to use form validation in Laravel 6

In this tutorial, I will explain you how to use form validation in Laravel 6. We will use Laravel 6 form validation with display error messages on view file. We can use Laravel default validation rules like required, email, unique, numeric, date, ip, in_array, it, ite, max, min, image, mimes etc.

Originally published at https://itsolutionstuff.com

You can also define custom error messages in Laravel 6 form validation. we will display error message with each field. we will use has() for checking is error message in Laravel 6.

Here, i am going to show you very simple example of form validation so, you can simply use in your Laravel 6 project.

Step 1:Create Routes:

Here we are learning simple and easy example of validation in Laravel 6 so just add following both route in your web.php file.

routes/web.php

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

Create Controller:

Now we will add two controller method, one will just display blade file with get request, and another for post request, i write validation for that, so simply add both following method on it.

app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('createUser');
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $request-&gt;validate([
            'name' =&gt; 'required',
            'password' =&gt; 'required|min:5',
            'email' =&gt; 'required|email|unique:users'
        ], [
            'name.required' =&gt; 'Name is required',
            'password.required' =&gt; 'Password is required'
        ]);

    $input = $request-&gt;all();
    $input['password'] = bcrypt($input['password']);
    $user = User::create($input);

    return back()-&gt;with('success', 'User created successfully.');
}

}

Create Blade File:

now here we will create createUser.blade.php file and here we will create bootstrap simple form with error validation message. So, let's create following file:

resources/views/createUser.blade.php

Read Also: Laravel 6 Ajax Autocomplete Search from Database

<!DOCTYPE html>
<html>
<head>
<title>Laravel 6 form validation example - ItSolutionStuff.com</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">

    &lt;h1&gt;Laravel 6 form validation example&lt;/h1&gt;

    @if(Session::has('success'))
    &lt;div class="alert alert-success"&gt;
        {{ Session::get('success') }}
        @php
            Session::forget('success');
        @endphp
    &lt;/div&gt;
    @endif

    &lt;form method="POST" action="{{ url('user/create') }}"&gt;

        {{ csrf_field() }}

        &lt;div class="form-group"&gt;
            &lt;label&gt;Name:&lt;/label&gt;
            &lt;input type="text" name="name" class="form-control" placeholder="Name"&gt;
            @if ($errors-&gt;has('name'))
                &lt;span class="text-danger"&gt;{{ $errors-&gt;first('name') }}&lt;/span&gt;
            @endif
        &lt;/div&gt;

        &lt;div class="form-group"&gt;
            &lt;label&gt;Password:&lt;/label&gt;
            &lt;input type="password" name="password" class="form-control" placeholder="Password"&gt;
            @if ($errors-&gt;has('password'))
                &lt;span class="text-danger"&gt;{{ $errors-&gt;first('password') }}&lt;/span&gt;
            @endif
        &lt;/div&gt;

        &lt;div class="form-group"&gt;
            &lt;strong&gt;Email:&lt;/strong&gt;
            &lt;input type="text" name="email" class="form-control" placeholder="Email"&gt;
            @if ($errors-&gt;has('email'))
                &lt;span class="text-danger"&gt;{{ $errors-&gt;first('email') }}&lt;/span&gt;
            @endif
        &lt;/div&gt;

        &lt;div class="form-group"&gt;
            &lt;button class="btn btn-success btn-submit"&gt;Submit&lt;/button&gt;
        &lt;/div&gt;
    &lt;/form&gt;
&lt;/div&gt;

</body>
</html>

Now we can run and check full example.

I hope it can help you...

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

Laravel 5.8 Tutorial for Beginners

Upgrading Laravel To 6.0 From 5.8

Laravel 6 Release New Features and Upgrade

What’s New in Laravel 6.0

Laravel 6 CRUD Application Tutorial

Laravel 6 Image Upload Tutorial

How to Create Ajax Autocomplete Search from Database in Laravel 6


Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]

Laravel Framework Development Services | Laravel Web Development

CMARIX is leading Laravel Web application Development Service Provider with the track record of implementing diverse high-performance Laravel web applications with skilled Laravel developer team for clients across the globe<a href="https://www.cmarix.com/laravel-framework-development-services.html?utm_source=SB" target="_blank">.Continue</a>

CMARIX is leading Laravel Web application Development Service Provider with the track record of implementing diverse high-performance Laravel web applications with skilled Laravel developer team for clients across the globe.Continue

Which is the best PHP Web Development Company in India?

Which is the best PHP Web Development Company in India?

Many experienced players prefer to give it a go ahead with PHP for their web development needs because of the simplicity that PHP provides. PHP is loaded with features that many other languages lack and those pre-defined features can bring new functionalities and finishing touch to your existing or new website that you are looking forward to create. Wouldn’t it be great to get the best PHP Web Development Company for your project; we have a reputation as few of the leading firms in IT industry when it comes to PHP development.

Perks you should keep an eye out for before considering to outsource project to any web development company:

1. Company’s Reputation- It is very important to know about the previous projects that the company has delivered and the success rate of those products. You can visit company’s website for client testimonials or can google reviews given to them by genuine clients. We follow transparency, you can contact our previous clients and let them tell you how their experience was while working with us.

2. Technical Skill set and Expertise- You should select a company that has multiple resource pool of expert and technically sound developers. Our developers are experienced with different PHP frameworks and are exceptionally good with writing bug free codes. You can also hire PHP developer in India from us, as our developers have on hand experience in PHP development (core and frameworks)

3. The Budget- Costing has always played as one of the most important roles in every business deal. We provide competitive pricing with no hidden cost guaranteed. With us you can expect your project to be in best quality with no compromise to be made even with the best price in the market.

Why Data EximIT is the best PHP web development company in India?

Here are some of the features that might love about Data EximIT:

 Reputation of Data EximIT is wonderful among the previous clients.

 We have around 87% of retention ratio of clients.

 We were time committed, means every milestone was achieved before or on time.

 Data EximIT follows Agile development so it was the best possible methodology.

 We have huge team with different expertise over multiple PHP frameworks, so you are provided with ample to options to choose from.

 We are very active in communication part. Method or time of communication was never an issue at Data EximIT.

 We have some of the best designers to ensure that your project not only functions but also looks and feels fabulous.

 We provided customers with best costing without compromising on the quality at all.

 We also provide dedicated PHP developer or team of PHP developers on hire for your project.

Data EximIT is where customers feel confident to come up with idea and leave with fully functioning & best in quality projects. You can also avail our developers at your requested site location if you decide to hire PHP developers in India from us.

Feel free to contact our experts and let success reach you by working with the best PHP web development company.