Laravel 5.6 - Registration Form Validation with Error Messages


In this PHP Laravel 5.6 Tutorial, We will learn how to implement custom signup form validation with error messages from scratch.

We will create registration form with "name", "phone", "email", "password", "unique", "min", "max" and "confirm_password" field and I am going to apply validation rules to each input fields of sign up form.

So just follow bellow full example:

Step 1: Create Routes

first of all, we need to create two routes to work with on form validation request in Laravel 5.6 application.

routes/web.php

Route::get('register', 'UserController@create');

Route::post('register','UserController@store');

Step 2: Create UserController Controller

In second step, we need to create a "UserController.php" file to display a view and handle the request on form submission.

app/Http/Controllers/UserController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\User;


class UserController extends Controller

{


public function create()

{

return view('register');

}


public function store()

{

request()->validate([

'name' => 'required|min:2|max:50',

'phone' => 'required|numeric',

'email' => 'required|email|unique:users',

'password' => 'required|min:6',

'confirm_password' => 'required|min:6|max:20|same:password',

], [

'name.required' => 'Name is required',

'name.min' => 'Name must be at least 2 characters.',

'name.max' => 'Name should not be greater than 50 characters.',

]);


$input = request()->except('password','confirm_password');

$user=new User($input);

$user->password=bcrypt(request()->password);

$user->save();


return back()->with('success', 'User created successfully.');

}

}

Step 3: Create Blade File

In this step, I will create a view file to display a register form with multiple input field and submit this form to the server to check against validation rules.

resources/views/register.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5.6 - Registration Form Validation with Error Messages - HDTuto.com</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

</head>

<body>


<div class="container">

<h3>Laravel 5.6 - Registration Form Validation with Error Messages - HDTuto.com</h3>

@if (count($errors) > 0)

<div class="alert alert-danger">


<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>


@endif


@if ($message = Session::get('success'))

<div class="alert alert-success">

<p>{{ $message }}</p>

</div>

@endif


<form action="{{ url('register') }}" method="POST" id="signupForm">

{{ csrf_field() }}


<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">

<label class="control-label">Name:</label>

<input type="text" name="name" class="form-control" value="{{ old('name') }}">

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

<span class="text-danger">{{ $errors->first('name') }}</span>

@endif

</div>

<div class="form-group {{ $errors->has('phone') ? 'has-error' : '' }}">

<label class="control-label">Phone:</label>

<input type="text" name="phone" class="form-control" value="{{ old('phone') }}">

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

<span class="text-danger">{{ $errors->first('phone') }}</span>

@endif

</div>

<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">

<label class="control-label">Email:</label>

<input type="email" name="email" class="form-control" value="{{ old('email') }}">

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

<span class="text-danger">{{ $errors->first('email') }}</span>

@endif

</div>

<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">

<label class="control-label">Password:</label>

<input type="password" name="password" class="form-control">

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

<span class="text-danger">{{ $errors->first('password') }}</span>

@endif

</div>

<div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">

<label class="control-label">Confirm Password:</label>

<input type="password" name="confirm_password" class="form-control">

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

<span class="text-danger">{{ $errors->first('confirm_password') }}</span>

@endif

</div>

<div class="form-group">

<button class="btn btn-success" type="submit">Submit</button>

</div>

</form>

</div>


</body>

</html>

Okay, now we are ready to run. you can check.

I hope you found your best...






June 4, 2018 | Category : PHP Laravel Bootstrap Laravel 5 Laravel 5.5 Laravel 5.6
Related Posts