Laravel 6 Form Validation with Error Messages

Laravel 6 Form Validation with Error Messages

In this post we will give you information about Laravel 6 Form Validation with Error Messages. Hear we will give you detail about Laravel 6 Form Validation with Error MessagesAnd how to use it also give you demo for it if it is necessary.

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.

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', 'HomeController@create');

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

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 AppHttpControllers;

use IlluminateHttpRequest;

use AppUser;

class HomeController extends Controller

{

/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

public function create()

{

return view('createUser');

}

/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

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 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

Also see: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 >

<h1>Laravel 6 form validation example</h1>

@if(Session::has('success'))

<div >

{{ Session::get('success') }}

@php

Session::forget('success');

@endphp

</div>

@endif

<form method="POST" action="{{ url('user/create') }}">

{{ csrf_field() }}

<div >

<label>Name:</label>

<input type="text" name="name" placeholder="Name">

@if ($errors->has('name'))

<span >{{ $errors->first('name') }}</span>

@endif

</div>

<div >

<label>Password:</label>

<input type="password" name="password" placeholder="Password">

@if ($errors->has('password'))

<span >{{ $errors->first('password') }}</span>

@endif

</div>

<div >

<strong>Email:</strong>

<input type="text" name="email" placeholder="Email">

@if ($errors->has('email'))

<span >{{ $errors->first('email') }}</span>

@endif

</div>

<div >

<button >Submit</button>

</div>

</form>

</div>

</body>

</html>

Now we can run and check full example.

I hope it can help you…

Hope this code and post will helped you for implement Laravel 6 Form Validation with Error Messages. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  89  =  93

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US