PHP Laravel 5.6 Validation example for signup form with error messages
In this post we will give you information about PHP Laravel 5.6 Validation example for signup form with error messages. Hear we will give you detail about PHP Laravel 5.6 Validation example for signup form with error messagesAnd how to use it also give you demo for it if it is necessary.
In this PHP Laravel 5.6 Tutorial, I will let you know how to implement form input validation rules on signup form and show the messages accordingly.
It’s very important for every web application to validate request data before interacting with the database.
There are pre-defined validation rules in Laravel that can be used easily in Laravel application.
In this example, I will have a singup form with “name”, “phone”, “email”, “password” and “confirm_password” field and I am going to apply validation rules to each input field of signup form.
Step1 : Add Routes
In this first step, I need two routes to work with on form validation request in Laravel 5.6
routes/web.php
Route::get('signup', 'UserController@create'); Route::post('signup','UserController@store');
Step2 : Create UserController.php
In this step, I will create a “UserController.php” file to display a view and handle the request on form submission.
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppUser; classUserControllerextends Controller { publicfunctioncreate() { return view('signup'); } publicfunctionstore() { 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.'); } }
You can also stop validation rules on an attribute if the first validation rule on the attribute is fails by assigning the bail
rule to attribute.
'email' => 'bail|required|email|unique:users'
If you want to set validation rules on Optional Fields then you can use nullable
modifier.
'phone' => 'nullable|numeric',
In above validation rules, I have set the validation on the phone field that can be either null or valid number value.
Step3 : Create Blade View File signup.blade.php
In this step, I will create a view file to display a SignUp form with multiple input field and submit this form to the server to check against validation rules.
<!DOCTYPE html> <html> <head> <title>Laravel 5.6 form input validation rules for signup a new user with Bootstrap</title> <linkrel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <divclass="container"> <h3>Laravel 5.6 form input validation rules for signup a new user with Bootstrap</h3> @if (count($errors) > 0) <divclass="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif @if ($message = Session::get('success')) <divclass="alert alert-success"> <p>{{ $message }}</p> </div> @endif <form action="{{ url('signup') }}"method="POST"id="signupForm"> {{ csrf_field() }} <divclass="form-group {{ $errors->has('name') ? 'has-error' : '' }}"> <labelclass="control-label">Name:</label> <inputtype="text"name="name"class="form-control"value="{{ old('name') }}"> @if ($errors->has('name')) <spanclass="text-danger">{{ $errors->first('name') }}</span> @endif </div> <divclass="form-group {{ $errors->has('phone') ? 'has-error' : '' }}"> <labelclass="control-label">Phone:</label> <inputtype="text"name="phone"class="form-control"value="{{ old('phone') }}"> @if ($errors->has('phone')) <spanclass="text-danger">{{ $errors->first('phone') }}</span> @endif </div> <divclass="form-group {{ $errors->has('email') ? 'has-error' : '' }}"> <labelclass="control-label">Email:</label> <inputtype="email"name="email"class="form-control"value="{{ old('email') }}"> @if ($errors->has('email')) <spanclass="text-danger">{{ $errors->first('email') }}</span> @endif </div> <divclass="form-group {{ $errors->has('password') ? 'has-error' : '' }}"> <labelclass="control-label">Password:</label> <inputtype="password"name="password"class="form-control"> @if ($errors->has('password')) <spanclass="text-danger">{{ $errors->first('password') }}</span> @endif </div> <divclass="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}"> <labelclass="control-label">Confirm Password:</label> <inputtype="password"name="confirm_password"class="form-control"> @if ($errors->has('confirm_password')) <spanclass="text-danger">{{ $errors->first('confirm_password') }}</span> @endif </div> <divclass="form-group"> <buttonclass="btn btn-success"type="submit">Submit</button> </div> </form> </div> </body> </html>
exists()
and unique()
, these two rules are used to validate request data against data that is stored in the database table.
// exists example 'user_id' => 'required|exists:users,id' // unique example 'email' => 'required|unique:users,email'
You can also use the Rule
class to define the rule fluently.
use IlluminateValidationRule; Validator::make($data, [ 'email' => [ 'required', Rule::exists('users')->where(function ($query) { $query->where('status', 1); }), ], ]);
How To Create Custom Validation Rules With Laravel 5
Hope this code and post will helped you for implement PHP Laravel 5.6 Validation example for signup form 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