Download the PHP package phpdevcommunity/php-validator without Composer
On this page you can find all versions of the php package phpdevcommunity/php-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phpdevcommunity/php-validator
More information about phpdevcommunity/php-validator
Files in phpdevcommunity/php-validator
Package php-validator
Short Description PHP Validator is a fast, extensible, and simple PHP validation library that allows you to easily validate various types of data.
License MIT
Informations about the package php-validator
PHP Validator
PHP Validator is a fast, extensible, and simple PHP validation library that allows you to easily validate various types of data.
Installation
You can install this library via Composer. Ensure your project meets the minimum PHP version requirement of 7.4.
Requirements
- PHP version 7.4 or higher
- Required package for PSR-7 HTTP Message (e.g.,
guzzlehttp/psr7
)
Usage
The PHP Validator library enables you to validate data in a simple and flexible manner using pre-configured validation rules. Here are some usage examples:
Example 1: Email Address Validation
Validate an email address to ensure it is not null and matches the standard email format.
Example 2: Age Validation
Validate the age to ensure it is a non-null integer and is 18 or older.
Additional Examples
Let's explore more examples covering various validators:
Username Validation
Ensure that a username is not null, has a minimum length of 3 characters, and contains only alphanumeric characters.
URL Validation
Validate a URL to ensure it is not null and is a valid URL format.
Numeric Value Validation
Validate a numeric value to ensure it is not null and represents a valid numeric value.
Custom Validation Rule
Implement a custom validation rule using a callback function.
Certainly! Below is a chapter you can add to your README specifically covering examples for using the validate(ServerRequestInterface $request)
method from your Validation
class.
Using validate(ServerRequestInterface $request)
Method
The Validation
class provides a convenient method validate(ServerRequestInterface $request)
to validate data extracted from a \Psr\Http\Message\ServerRequestInterface
object. This method simplifies the process of validating request data typically received in a web application.
Example 1: Validating User Registration Form
Suppose you have a user registration form with fields like username
, email
, password
, and age
. Here's how you can use the validate
method to validate this form data:
In this example:
- We instantiate a
Validation
object with validation rules defined for each field (username
,email
,password
,age
). - We call the
validate
method with the$request
object containing form data. - If validation passes (
validate
method returnstrue
), we retrieve the validated data using$validation->getData()
and proceed with the registration logic. - If validation fails, we retrieve the validation errors using
$validation->getErrors()
and handle them accordingly.
Example 2: Validating API Input Data
Consider validating input data received via an API endpoint. Here's how you can use the validate
method in this context:
In this example:
- We define validation rules for
product_id
andquantity
. - We call the
validate
method with the$request
object containing API input data. - If validation passes, we retrieve the validated data using
$validation->getData()
and proceed with processing the API request. - If validation fails, we retrieve the validation errors using
$validation->getErrors()
and handle them appropriately.
Additional Features
- Simple Interface: Easily define validation rules using a straightforward interface.
- Extensible: Extend the library with custom validation rules by implementing the
RuleInterface
. - Error Handling: Retrieve detailed validation errors for each field.
License
This library is open-source software licensed under the MIT license.
All versions of php-validator with dependencies
psr/http-message Version ^1.0|^2.0
ext-json Version *
ext-ctype Version *