1. Go to this page and download the library: Download chmielewskitomasz/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/ */
chmielewskitomasz / validator example snippets
use Hop\Strategy\Strategy;
use Hop\Strategy\Field;
use Hop\StdValidator;
// first, create your input validation strategy
class AlbumInputValidator implements Strategy
{
/**
* @inheritdoc
*/
public function getFields(): array
{
$nameField = new Field(
'name',
true,
null
);
$phoneField = new Field(
'phone'
false,
null
);
$phoneField->registerValidator('Digits');
$phoneField->registerValidator('Length', ['min' => 9, 'max' => 9]);
return [
$nameField,
$phoneField
];
}
}
// second, create stdValidator
$validator = StdValidator::fromConfig(
// In strategy:
// ...
public function getFields(): array
{
$field = new Field(
'phone',
false, // set second parameter 'alid([], new AlbumInputValidator()); // true
$validator->isValid(['phone' => 'abcdefg'], new AlbumInputValidator()); // false
// In strategy:
// ...
public function getFields(): array
{
$streetField = new Field(
'street',
false,
null
);
$houseNumberField = new Field(
'houseNumber',
true,
function (array $inputData) {
return !isset($inputData['street']);
}
);
return [$streetField, $houseNumberField];
}
// ...
// and then
$validator->isValid(['street' => 'High st.'], new AlbumInputValidator()); // false
$validator->isValid([], new AlbumInputValidator()); // true
$validator->isValid(['street' => 'High st.', 'houseNumber' => '12'], new AlbumInputValidator()); // true