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 RoutesIn this first step, I need two routes to work with on form validation request in Laravel 5.6
routes/web.phpRoute::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 App\Http\Controllers; use Illuminate\Http\Request; use App\User; class UserController extends Controller { public function create() { return view('signup'); } 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.'); } }
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.phpIn 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> <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 form input validation rules for signup a new user with Bootstrap</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('signup') }}" 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>
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 Illuminate\Validation\Rule; Validator::make($data, [ 'email' => [ 'required', Rule::exists('users')->where(function ($query) { $query->where('status', 1); }), ], ]);
How To Create Custom Validation Rules With Laravel 5