| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
PHP Validator is a fast, extensible, and simple PHP validation library that allows you to easily validate various types of data.
You can install this library via Composer. Ensure your project meets the minimum PHP version requirement of 7.4.
composer require phpdevcommunity/php-validatorThe PHP Validator library enables you to validate data in a simple and flexible manner using pre-configured validation rules. Here are some usage examples:
Validate an email address to ensure it is not null and matches the standard email format.
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Email;
// Instantiate Validation object for email validation
$validation = new Validation([
'email' => [new NotNull(), new Email()]
]);
// Example data array
$data = ['email' => 'john.doe@example.com'];
// Validate the data
if ($validation->validateArray($data) === true) {
echo "Email is valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . implode(", ", $errors['email']);
}Validate the age to ensure it is a non-null integer and is 18 or older.
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Integer;
// Instantiate Validation object for age validation
$validation = new Validation([
'age' => [new NotNull(), new Integer(['min' => 18])]
]);
// Example data array
$data = ['age' => 25];
// Validate the data
if ($validation->validateArray($data) === true) {
echo "Age is valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . implode(", ", $errors['age']);
}By default, when using validate(ServerRequestInterface $request), the convertEmptyToNull option is automatically enabled. This ensures that all empty strings ("") are converted to null before validation.
When using validateArray(array $data) directly, you need to enable this option manually:
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Item;
use PhpDevCommunity\Validator\Assert\Alphabetic;
// Define validation rules
$validation = new Validation([
'person' => [new NotNull(), new Item([
'first_name' => [new NotNull(), new Alphabetic()],
'last_name' => [new NotNull(), new Alphabetic()],
])]
]);
// Example input with an empty string
$validInput = [
'person' => [
'first_name' => '', // will be converted to null
'last_name' => 'Doe'
]
];
// Manually enable conversion of "" to null
$validation->convertEmptyToNull();
if ($validation->validateArray($validInput) === true) {
echo "Data is valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . json_encode($errors, JSON_PRETTY_PRINT);
}In this example:
Let's explore more examples covering various validators:
Ensure that a username is not null, has a minimum length of 3 characters, and contains only alphanumeric characters.
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Alphanumeric;
use PhpDevCommunity\Validator\Assert\StringLength;
// Instantiate Validation object for username validation
$validation = new Validation([
'username' => [new NotNull(), new StringLength(['min' => 3]), new Alphanumeric()]
]);
// Example data array
$data = ['username' => 'john_doe123'];
// Validate the data
if ($validation->validateArray($data) === true) {
echo "Username is valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . implode(", ", $errors['username']);
}The Item rule allows you to validate nested objects or associative arrays with specific rules for each key.
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Item;
use PhpDevCommunity\Validator\Assert\StringLength;
use PhpDevCommunity\Validator\Assert\Alphabetic;
// Define validation rules for a nested object (e.g., a "person" object)
$validation = new Validation([
'person' => [new NotNull(), new Item([
'first_name' => [new NotNull(), new Alphabetic(), (new StringLength())->min(3)],
'last_name' => [new NotNull(), new Alphabetic(), (new StringLength())->min(3)],
])]
]);
// Example data
$data = [
'person' => [
'first_name' => 'John',
'last_name' => 'Doe'
]
];
// Validate the data
if ($validation->validateArray($data) === true) {
echo "Person object is valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . json_encode($errors, JSON_PRETTY_PRINT);
}The Collection rule is used to validate arrays where each item in the array must satisfy a set of rules.
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotEmpty;
use PhpDevCommunity\Validator\Assert\Collection;
use PhpDevCommunity\Validator\Assert\Item;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\StringLength;
// Define validation rules for a collection of articles
$validation = new Validation([
'articles' => [new NotEmpty(), new Collection([
new Item([
'title' => [new NotNull(), (new StringLength())->min(3)],
'body' => [new NotNull(), (new StringLength())->min(10)],
])
])]
]);
// Example data
$data = [
'articles' => [
['title' => 'Article 1', 'body' => 'This is the body of the first article.'],
['title' => 'Article 2', 'body' => 'Second article body here.']
]
];
// Validate the data
if ($validation->validateArray($data) === true) {
echo "Articles are valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . json_encode($errors, JSON_PRETTY_PRINT);
}Validate a URL to ensure it is not null and is a valid URL format.
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Url;
// Instantiate Validation object for URL validation
$validation = new Validation([
'website' => [new NotNull(), new Url()]
]);
// Example data array
$data = ['website' => 'https://example.com'];
// Validate the data
if ($validation->validateArray($data) === true) {
echo "Website URL is valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . implode(", ", $errors['website']);
}Validate a numeric value to ensure it is not null and represents a valid numeric value.
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Numeric;
// Instantiate Validation object for numeric value validation
$validation = new Validation([
'price' => [new NotNull(), new Numeric()]
]);
// Example data array
$data = ['price' => 99.99];
// Validate the data
if ($validation->validateArray($data) === true) {
echo "Price is valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . implode(", ", $errors['price']);
}Implement a custom validation rule using a callback function.
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Custom;
// Custom validation function to check if the value is a boolean
$isBoolean = function ($value) {
return is_bool($value);
};
// Instantiate Validation object with a custom validation rule
$validation = new Validation([
'active' => [new NotNull(), new Custom($isBoolean)]
]);
// Example data array
$data = ['active' => true];
// Validate the data
if ($validation->validateArray($data) === true) {
echo "Value is valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . implode(", ", $errors['active']);
}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.
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.
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:
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Email;
use PhpDevCommunity\Validator\Assert\Integer;
// Define validation rules for each field
$validation = new Validation([
'username' => [new NotNull()],
'email' => [new NotNull(), new Email()],
'password' => [new NotNull()],
'age' => [new NotNull(), new Integer()]
]);
// Assume $request is the \Psr\Http\Message\ServerRequestInterface object containing form data
if ($validation->validate($request) === true) {
// Validation passed, retrieve validated data
$validatedData = $validation->getData();
// Process registration logic here (e.g., save to database)
// $username = $validatedData['username'];
// $email = $validatedData['email'];
// $password = $validatedData['password'];
// $age = $validatedData['age'];
echo "Registration successful!";
} else {
// Validation failed, retrieve validation errors
$errors = $validation->getErrors();
// Handle validation errors (e.g., display error messages to the user)
echo "Validation errors: " . implode(", ", $errors);
}In this example:
Consider validating input data received via an API endpoint. Here's how you can use the validate method in this context:
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Assert\NotNull;
use PhpDevCommunity\Validator\Assert\Numeric;
// Define validation rules for API input data
$validation = new Validation([
'product_id' => [new NotNull(), new Numeric()],
'quantity' => [new NotNull(), new Numeric()]
]);
// Assume $request is the \Psr\Http\Message\ServerRequestInterface object containing API input data
if ($validation->validate($request) === true) {
// Validation passed, proceed with processing API request
$validatedData = $validation->getData();
// Extract validated data
$productId = $validatedData['product_id'];
$quantity = $validatedData['quantity'];
echo "API request validated successfully!";
} else {
// Validation failed, retrieve validation errors
$errors = $validation->getErrors();
// Handle validation errors (e.g., return error response to the client)
echo "Validation errors: " . implode(", ", $errors);
}In this example:
This library is open-source software licensed under the MIT license.
| Back | FazBrowse Home | New Git URL |