PHP code example of fabstract / validator
1. Go to this page and download the library: Download fabstract/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/ */
fabstract / validator example snippets
use Fabstract\Component\Validator\ValidatableInterface;
use Fabstract\Component\Validator\Validation\PatternValidation;
use Fabstract\Component\Validator\Validation\StringValidation;
use Fabstract\Component\Validator\ValidationMetadata;
use Fabstract\Component\Validator\Validator;
// Create class
class RegistrationPostData implements ValidatableInterface
{
// Has to be 3-30 characters
public $name = null;
// Has to ->addValidation('password', PatternValidation::create('/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,32}$/'));
}
}
// Create validator instance, one instance is enough
$validator = new Validator();
// Let's create instance of our RegistrationPostData and fill it with POST data
$post_data = new RegistrationPostData();
$post_data->name = $_POST['name'];
$post_data->password = $_POST['password'];
// Validate the data
$validation_error_list = $validator->validate($post_data);
// Here if validation error list is empty, it means all your post data is valid.
$is_post_data_valid = count($validation_error_list) === 0;
if (!$is_post_data_valid) {
// If not empty, you can see meaningful validation messages like below
foreach ($validation_error_list as $validation_error) {
echo $validation_error;
echo PHP_EOL;
}
}