Download the PHP package code-corner/validation without Composer
On this page you can find all versions of the php package code-corner/validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download code-corner/validation
More information about code-corner/validation
Files in code-corner/validation
Package validation
Short Description The Validation Library is a robust PHP toolkit designed to streamline the validation of form fields upon submission. It offers a variety of methods that cater to different data types and validation criteria, ensuring that your application receives accurate and secure input data.
License MIT
Informations about the package validation
Validation Library
Introduction
This is a comprehensive validation library for PHP, providing various methods to validate different types of data against specified rules.
Installation
Ensure you have Composer installed. You can install the library using Composer by running:
Usage
-
Require Composer's Autoload File
Make sure to include Composer's autoload file at the beginning of your script:
-
Use the Validation Class
Use the
Validation
class from theCodeCorner\Validation
namespace in your script: -
Perform Validation
Define your validation rules and validate your data using the
validate
method of theValidation
class: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 leastvalue
characters long (for strings) or numeric value.max:value
: The field must be at mostvalue
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 forin_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:This will output an array of validation errors encountered during the validation process.
-
Using Database level validation
- add your DB object in the config.php file.
then you will use database level validation
Example
Here's a complete example of how you might use this library:
In this example:
- We include Composer's autoload file.
- Define various validation rules for different fields.
- Validate the provided data against these rules.
- Print any validation errors encountered.
All versions of validation with dependencies
php Version >=7.4.2
symfony/polyfill-mbstring Version ^1.17
giggsey/libphonenumber-for-php Version ^8.13