PHP code example of wscore / validator
1. Go to this page and download the library: Download wscore/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/ */
wscore / validator example snippets
$vb = new ValidatorBuilder();
$validator = $vb->text([
StringLength::class => ['max' => 12],
]);
$result = $validator->verify($value);
if ($result->isValid()) {
echo $result->value(); // validated value
} else {
var_dump($result->getErrorMessage()); // error messages
}
$form = $vb->form()
->add('name', $vb->text([
Required::class,
StringCases::class => [StringCases::TO_LOWER, StringCases::UC_WORDS],
]))
->add('email', $vb->email([
Required::class,
StringCases::class => [StringCases::TO_LOWER],
ConfirmWith::class => [ConfirmWith::FIELD => 'email_check'],
]));
$result = $form->verify([ // or simply verify $_POST here...
'name' => 'MY NAME',
'email' => '[email protected] ',
'email_check' => '[email protected] ',
]);
if ($result->isValid()) {
echo $result->getChild('name')->value(); // 'My Name'
echo $result->getChild('email')->value(); // '[email protected] '
} else {
// access elements in the form.
foreach($result as $key => $element) {
if (!$element->isValid()) {
echo $element->getErrorMessage(); // error messages
}
}
}
$address = $vb->form()
->add('zip', $vb([
'type' => 'digits',
Required::class,
StringLength::class => [StringLength::LENGTH => 5],
]))
->add('address', $vb([
'type' => 'text',
Required::class,
]))
->add('region', $vb([
'type' => 'text',
Required::class,
InArray::class => [
InArray::REPLACE => [
'abc' => 'ABC Country',
'def' => 'DEF Region',
],
],
]));
$form = $vb->form()
->add('name', $vb->text([Required::class]))
->add('address', $address);
$input = [
'name' => 'test-nested',
'address' => [
'zip' => '12345',
'address' => 'city, street 101',
'region' => 'abc',
]
];
$result = $form->verify($input);
echo $result->getChild('address')->getChild('region')->value(); // 'ABC Country'
$posts = $vb->form()
->add('title', $vb->text([
Required::class,
]))
->add('publishedAt', $vb->date())
->add('size', $vb->integer([
Required::class,
]));
$form = $vb->form()
->add('name', $vb->text([Required::class]))
->addRepeatedForm('posts', $posts);
$input = [
'name' => 'test-one-to-many',
'posts' => [
['title' => 'first title', 'size' => 1234],
['title' => 'more tests here', 'publishedAt' => '2019-04-01', 'size' => 2345],
],
];
$result = $form->verify($input);
$tests = $vb->text([
'multiple' => true, // specify multiple!
StringLength::class => [StringLength::LENGTH => 3],
]);
$result = $tests->verify(['test', 'me']);
$tests = $vb->text([
'multiple' => [
Required::class,
],
StringLength::class => [StringLength::LENGTH => 5],
]);
$result = $tests->verify(['test', 'me']);
$vb = new ValidatorBuilder('ja');
$vb = new ValidatorBuilder('/dir/to/my/locale');
return [
'filter_error_type' => 'error message here',
...
];
return [
'type-name' => [
'filter-name',
'filter-with-option' => ['options' => 'here'],
], ...
];
$filter = new FilterArrayToValue([
'fields' => ['y', 'm', 'd'],
'format' => '%d.%02d.%02d',
]);
$filter = new FilterMbString(['type' => FilterMbString::MB_HANKAKU]);
$result = $filter(new Result('zenkaku@example.com'));
echo $result->value(); // [email protected]
$filter = new ConvertDateTime(['format' => 'm/d/Y']);
$result = $filter(new Result('04/01/2019'));
$date = $result->value(); // should be DateTimeImmutable object.
$ RequiredIf::FIELD => 'type',
RequiredIf::VALUE => 'check-me',
RequiredIf::NULLABLE => true,
]);
$filter = new InArray([
'choices' => [
$obj1->getKey(), $obj1,
$obj2->getKey(), $obj2,
],
'replace' => true,
'strict' => true,
]);