1. Go to this page and download the library: Download icanboogie/validate 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/ */
icanboogie / validate example snippets
use ICanBoogie\Validate\Validation;
use ICanBoogie\Validate\Reader\ArrayAdapter;
$validation = new Validation([
'name' => '>validate(new ArrayAdapter([
'name' => "Ol",
'email' => "olivier",
'password' => "123",
]));
var_dump($errors['name']);
var_dump((string) reset($errors['name']));
use ICanBoogie\Validate\Validation;
$validation = new Validation([
'name' => 'th' => [ 3 ],
]
]);
# or
use ICanBoogie\Validate\Validator;
$validation = new Validation([
'name' => [
Validator\Required::class => [],
Validator\MinLength::class => [ 3 ],
]
]);
# or
$validation = new Validation([
'name' => [
Validator\Required::class => [],
Validator\MinLength::class => [ Validator\MinLength::PARAM_REFERENCE => 3 ],
]
]);
use ICanBoogie\Validate\Reader\ArrayAdapter;
use ICanBoogie\Validate\Validation;
$validation = new Validation([
'' => '',
]));
isset($errors['
use ICanBoogie\Validate\Reader\RequestAdapter;
use ICanBoogie\Validate\Validation;
/* @var $validation Validation */
$errors = $validation->validate(new RequestAdapter($_POST));
if (!$errors)
{
// because `$errors` is an empty array we can check if it is empty with `!`.
}
foreach ($errors as $attribute => $messages)
{
// …
}
use ICanBoogie\Validate\Validation;
use ICanBoogie\Validate\ValidationFailed;
use ICanBoogie\Validate\Reader\RequestAdapter;
/* @var $validation Validation */
try
{
$validation->assert(new RequestAdapter($_POST));
}
catch (ValidationFailed $e)
{
echo get_class($e->errors); // ICanBoogie\Validate\ValidationErrors
}
use ICanBoogie\Validate\Context;
use ICanBoogie\Validate\Validation;
use ICanBoogie\Validate\Validator\Required;
use ICanBoogie\Validate\Validator\Email;
$validation = new Validation([
'email' => [
Required::class => [
Required::OPTION_MESSAGE => "An email address must be supplied if your wish to register.",
Required::OPTION_IF => function(Context $context) {
return $context->value('name');
},
Required::OPTION_UNLESS => function(Context $context) {
return !$context->value('register');
},
Required::OPTION_STOP_ON_ERROR => true // already defined by Require
],
Email::class => [
Email::OPTION_MESSAGE => "`{value}` is an invalid email address for the field E-Mail.",
]
]
]);
use ICanBoogie\Validate\Context;
use ICanBoogie\Validate\Validator\ValidatorAbstract;
class SampleValidator extends ValidatorAbstract
{
const PARAM_REFERENCE = 'reference';
const OPTION_STRICT = 'strict';
/**
* @inheritdoc
*/
public function validate($value, Context $context)
{
$reference = $context->param(self::PARAM_REFERENCE);
$strict = $context->option(self::OPTION_STRICT, false);
$other_value = $context->value('some_other_value');
}
}
use ICanBoogie\Validate\Validation;
use ICanBoogie\Validate\ValidatorProvider\BuiltinValidatorProvider;
/* @var $rules array */
$builtin_validator_provider = new BuiltinValidatorProvider;
$validation = new Validation($rules, $builtin_validator_provider);
# or
$validation = new Validation($rules);
use ICanBoogie\Validate\UndefinedValidator;
use ICanBoogie\Validate\Validation;
use ICanBoogie\Validate\Validator;
/* @var $sample_validator Validator */
$rules = [ 'attribute' => $sample_validator::ALIAS ];
$sample_validator_provider = function ($class_or_alias) use ($sample_validator) {
switch ($class_or_alias)
{
case get_class($sample_validator):
case $sample_validator::ALIAS:
return $sample_validator;
default:
throw new UndefinedValidator($class_or_alias);
}
};
$validation = new Validation($rules, $sample_validator_provider);
use ICanBoogie\Validate\Validation;
use ICanBoogie\Validate\ValidatorProvider\ContainerValidatorProvider;
use Psr\Container\ContainerInterface;
/* @var $rules array */
/* @var $container ContainerInterface */
$prefix = 'validator.';
$container_validator_provider = new ContainerValidatorProvider($container, $prefix);
$validation = new Validation($rules, $container_validator_provider);
use ICanBoogie\Validate\Validation;
use ICanBoogie\Validate\ValidatorProvider\BuiltinValidatorProvider;
use ICanBoogie\Validate\ValidatorProvider\ContainerValidatorProvider;
use ICanBoogie\Validate\ValidatorProvider\ValidatorProviderCollection;
/* @var $rules array */
/* @var $container_validator_provider ContainerValidatorProvider */
/* @var $sample_validator_provider callable */
/* @var $builtin_validator_provider BuiltinValidatorProvider */
$validator_provider_collection = new ValidatorProviderCollection([
$container_validator_provider,
$sample_validator_provider,
$builtin_validator_provider,
]);
$validation = new Validation($rules, $validator_provider_collection);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.