PHP code example of phpdevcommunity / php-validator
1. Go to this page and download the library: Download phpdevcommunity/php-validator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpdevcommunity / php-validator example snippets
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Rules\NotNull;
use PhpDevCommunity\Validator\Rules\Email;
// Instantiate Validation object for email validation
$validation = new Validation([
'email' => [new NotNull(), new Email()]
]);
// Example data array
$data = ['email' => '[email protected]'];
// Validate the data
if ($validation->validateArray($data) === true) {
echo "Email is valid!";
} else {
$errors = $validation->getErrors();
echo "Validation errors: " . implode(", ", $errors['email']);
}
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Rules\NotNull;
use PhpDevCommunity\Validator\Rules\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']);
}
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Rules\NotNull;
use PhpDevCommunity\Validator\Rules\Alphanumeric;
use PhpDevCommunity\Validator\Rules\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']);
}
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Rules\NotNull;
use PhpDevCommunity\Validator\Rules\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']);
}
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Rules\NotNull;
use PhpDevCommunity\Validator\Rules\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']);
}
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Rules\NotNull;
use PhpDevCommunity\Validator\Rules\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']);
}
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Rules\NotNull;
use PhpDevCommunity\Validator\Rules\Email;
use PhpDevCommunity\Validator\Rules\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);
}
use PhpDevCommunity\Validator\Validation;
use PhpDevCommunity\Validator\Rules\NotNull;
use PhpDevCommunity\Validator\Rules\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);
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.