Custom Validation Rule in Laravel Framework

  1. Download and install the latest version of Composer from https://getcomposer.org/download/

  2. Open Windows Terminal in Visual Studio Code install Laravel Installer with command as below:

    composer global require laravel/installer
  3. Create new folder named LearnLaravelWithRealApps. Use Visual Studio Code open to LearnLaravelWithRealApps foler. In this folder, create new project named LearnLaravelWithRealApps with command as below:

    laravel new LearnLaravelWithRealApps

  4. Run LearnLaravelWithRealApps project with command as below:

    php artisan serve
  5. Open LearnLaravelWithRealApps project with url as below:

    http://localhost:8000

Create new folder named Rules in app folder. Create new PHP file named UsernameRule.php in app/Rules folder as below:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class UsernameRule implements Rule
{
	public function __construct()
	{
	}

	public function passes($attribute, $value)
	{
		return 'abc' !== $value;
	}

	public function message()
	{
		return 'Username Exists';
	}
}

Create new PHP file named DemoController.php in app/Http/Controllers folder as below:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Rules\UsernameRule;

class DemoController extends Controller
{
	public function index()
	{
		return view('demo/index');
	}

	public function save(Request $request)
	{
		$this->validate($request, [
			'username' => [
				'required',
				'min:3',
				'max:8',
				new UsernameRule(),
			]
		]);
		return view('demo/success');
	}
}

Create new folder named demo in resources/views folder. In demo folder, create new views as below:

Create new PHP file named index.blade.php in resources/views/demo as below:

<html>

	<head>
		<title>Laravel</title>
	</head>

	<body>

		<h3>Register</h3>
		@if (count($errors) > 0)
		<div class="alert alert-danger">
			<ul>
				@foreach ($errors->all() as $error)
				<li>{{ $error }}</li>
				@endforeach
			</ul>
		</div>
		@endif
		<form method="post" action="{{url('/demo/save')}}">
			@csrf
			<table border="0">
				<tr>
					<td>Username</td>
					<td><input type="text" name="username" value="{{ old('username') }}"></td>
					<td>
						{!! $errors->first('username', '<p class="help-block">:message</p>') !!}
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td><input type="submit" value="Save"></td>
				</tr>
			</table>
			
		</form>

	</body>

</html>

Create new PHP file named success.blade.php in resources/views/demo as below:

<html>

	<head>
		<title>Laravel</title>
	</head>

	<body>

		Success

	</body>

</html>

Open web.php file in routes folder, add new routes as below:

<?php
				
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\DemoController;

Route::group([], function () {
	Route::get('/', [DemoController::class, 'index']);
	Route::get('/demo', [DemoController::class, 'index']);
	Route::get('/demo/index', [DemoController::class, 'index']);
	Route::post('/demo/save', [DemoController::class, 'save']);
});

Access index action in Demo controller with following url: http://localhost:8000/demo/index

Output

Click Save button submit form to Save action in Demo controller with invalid data as below:

Click Save button submit form to Save action in Demo controller with valid data

Output