Laravel Basic | Laravel 6 Form Validation with Error Messages

In this tutorial, I will explain to you how to use form validation in laravel 6. we will use laravel 6 form validation with display error messages on the 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.

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

Here, I am going to show you a very simple example of form validation so, you can simply use it in your laravel 6 projects.

Here we are learning simple and easy examples of validation in laravel 6 so just add following both routes in your web.php file.

routes/web.php

Route::get('user/create', 'HomeController@create');Route::post('user/create', 'HomeController@store');

Create Controller:

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

app/Http/Controllers/HomeController.php

<?phpnamespace 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->validate(['name' => 'required','password' => 'required|min:5','email' => 'required|email|unique:users'], ['name.required' => 'Name is required','password.required' => 'Password is required']);$input = $request->all();$input['password'] = bcrypt($input['password']);$user = User::create($input);return back()->with('success', 'User created successfully.');}}

Create Blade File:

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

resources/views/createUser.blade.php

<!DOCTYPE html><html><head><title>Laravel 6 form validation example - raviyatechnical</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"><h1>Laravel 6 form validation example</h1>@if(Session::has('success'))<div class="alert alert-success">{{ Session::get('success') }}@phpSession::forget('success');@endphp</div>@endif<form method="POST" action="{{ url('user/create') }}">{{ csrf_field() }}<div class="form-group"><label>Name:</label><input type="text" name="name" class="form-control" placeholder="Name">@if ($errors->has('name'))<span class="text-danger">{{ $errors->first('name') }}</span>@endif</div><div class="form-group"><label>Password:</label><input type="password" name="password" class="form-control" placeholder="Password">@if ($errors->has('password'))<span class="text-danger">{{ $errors->first('password') }}</span>@endif</div><div class="form-group"><strong>Email:</strong><input type="text" name="email" class="form-control" placeholder="Email">@if ($errors->has('email'))<span class="text-danger">{{ $errors->first('email') }}</span>@endif</div><div class="form-group"><button class="btn btn-success btn-submit">Submit</button></div></form></div></body></html>

Now we can run and check the full examples.

I hope it can help you…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store