use to validate your form fields with dynamicaly with proper message.
This is a comprehensive validation library for PHP, providing various methods to validate different types of data against specified rules.
Ensure you have Composer installed. You can install the library using Composer by running:
composer require codecorners/validation
Require Composer's Autoload File
Make sure to include Composer's autoload file at the beginning of your script:
require_once 'vendor/autoload.php';
Use the Validation Class
Use the Validation
class from the CodeCorner\Validation
namespace in your script:
use CodeCorner\Validation\Validation;
Perform Validation
Define your validation rules and validate your data using the validate
method of the Validation
class:
Validation::validate([
'name' => 'required|min:3',
'email' => 'required|email',
'age' => 'numeric|min:18',
'url' => 'URL',
'phone' => 'phone',
'amount' => 'decimal',
'role' => 'in_array:admin,moderator,user',
'username' => 'unique:users',
'password' => 'nullable|string|min:6',
], [
'name' => 'John Doe',
'email' => '[email protected]',
'age' => 25,
'url' => 'https://example.com',
'phone' => '+1234567890',
'amount' => 123.45,
'role' => 'admin',
'username' => 'johndoe',
'password' => null,
]);
In this example:
name
is required and must be at least 3 characters long.email
must be a valid email address.age
must be numeric and at least 18.url
must be a valid URL.phone
must be a valid phone number.amount
must be a decimal number.role
must be one of: admin, moderator, or user.username
must be unique (e.g., not already taken in a database).password
is nullable (optional) and if provided, must be a string of at least 6 characters.Available Validation Methods
Here are the available validation methods and their usage:
required
: The field must be present and not empty.string
: The field must be a string.numeric
: The field must be numeric.in_array:val1,val2,...
: The field must be one of the specified values.array_required
: The field must be an array and not empty.file
: The field must be a file upload. // 'size.5000000&type.png,jpeg&name.jpeg,png'min:value
: The field must be at least value
characters long (for strings) or numeric value.max:value
: The field must be at most value
characters long (for strings) or numeric value.nullable
: Allows the field to be null or empty.unique:table,column
: Checks if the field value is unique in a specified database table and column.email
: The field must be a valid email address.URL
: The field must be a valid URL.regex:pattern
: The field must match the specified regular expression pattern.not_in:val1,val2,...
: The field must not be one of the specified values.in:val1,val2,...
: Alias for in_array
.phone
: The field must be a valid phone number.decimal
: The field must be a valid decimal number.Retrieve Validation Errors
After performing validation, you can retrieve any validation errors using the getErrors
method:
$errors = Validation::getErrors();
print_r($errors);
This will output an array of validation errors encountered during the validation process.
Using Database level validation
Validation::dbConfigure(self::$pdo);
then you will use database level validation
<?php
require_once 'vendor/autoload.php';
use CodeCorner\Validation\Validation;
Validation::validate([
'name' => 'required|min:3',
'email' => 'required|unique:`tablename`.`fieldname`',
'phone' => 'phone|not_in:`tablename`.`fieldname`,`fieldname`-`value`',
'role' => 'in:`tablename`.`fieldname',
'username' => 'assign:`tablename`.`fieldname`',
], [
'name' => 'John Doe',
'email' => '[email protected]',
'phone' => '+1234567890',
'role' => 'admin',
'username' => 'johndoe',
]);
$errors = Validation::getErrors();
print_r($errors);
Here's a complete example of how you might use this library:
<?php
require_once 'vendor/autoload.php';
use CodeCorner\Validation\Validation;
Validation::validate([
'name' => 'required|min:3',
'email' => 'required|email',
'age' => 'numeric|min:18',
'url' => 'URL',
'phone' => 'phone',
'amount' => 'decimal',
'role' => 'in_array:admin,moderator,user',
'username' => 'unique:users',
'password' => 'nullable|string|min:6',
], [
'name' => 'John Doe',
'email' => '[email protected]',
'age' => 25,
'url' => 'https://example.com',
'phone' => '+1234567890',
'amount' => 123.45,
'role' => 'admin',
'username' => 'johndoe',
'password' => null,
]);
$errors = Validation::getErrors();
print_r($errors);
In this example: