1. Go to this page and download the library: Download phpixie/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/ */
phpixie / validate example snippets
$validate = new \PHPixie\Validate();
//Or if you are using PHPixie
$validate = $builder->components()->validate();
// The data we will be validating
$data = array(
'name' => 'Pixie',
'home' => 'Oak',
'age' => 200,
'type' => 'fairy'
);
// There are multiple syntaxes supported
// for building a validator
// The standard approach
$validator = $validate->validator();
// A flat file is just a document
$document = $validator->rule()->addDocument();
// A ,
'minLength' => array(3)
));
// Or a shorthand
$document->valueField('home')
->lid add an error to the result
$result->addMessageError("Type can be either 'fairy' or 'pixie'");
//Or you can just return an error message to be added
return "Type can be either 'fairy' or 'pixie'";
}
});
// By default validator will only allow
// fields that have rules attached to them
// If you wish to allow extra fields:
$document->allowExtraFields();
// Custom validator function
$validator->rule()->callback(function($result, $value) {
if($value['type'] === 'fairy' && $value['home'] !== 'Oak') {
$result->addMessageError("Fairies live only inside oaks");
}
});
$result = $validator->validate($data);
var_dump($result->isValid());
// Add some errors
$data['name'] = 'Pi';
$data['home'] = 'Maple';
$result = $validator->validate($data);
var_dump($result->isValid());
// Print errors
foreach($result->errors() as $error) {
echo $error."\n";
}
foreach($result->invalidFields() as $fieldResult) {
echo $fieldResult->path().":\n";
foreach($fieldResult->errors() as $error) {
echo $error."\n";
}
}
/*
bool(true)
bool(false)
Fairies live only inside oaks
name:
Value did not pass filter 'minLength'
*/
if($error->type() === 'filter') {
if($error->filter() === 'minLength') {
$params = $error->parameters();
echo "Please enter at least {$params[0]} characters";
}
}
$data = array(
'name' => 'Pixie',
// 'home' is just a subdocument
'home' => array(
'location' => 'forest',
'name' => 'Oak'
),
// 'spells' is an array of documents of a particular type
// and a string key (also has to be validated)
// of the same type
'spells' => array(
'charm' => array(
'name' => 'Charm Person',
'type' => 'illusion'
),
'blast' => array(
'name' => 'Fire Blast',
'type' => 'evocation'
),
// ....
)
);
$validator = $validate->validator();
$document = $validator->rule()->addDocument();
$document->valueField('name')
->ellDocument->valueField('name')
->
$result = $validator->validate($data);
var_dump($result->isValid());
//bool(true)
// Add some errors
$data['name'] = '';
$data['spells']['charm']['name'] = '1';
// Invalid key (should be string)
$data['spells'][3] = $data['spells']['blast'];
$result = $validator->validate($data);
var_dump($result->isValid());
//bool(false)
// Recursive function for error printing
function printErrors($result) {
foreach($result->errors() as $error) {
echo $result->path().': '.$error."\n";
}
foreach($result->invalidFields() as $result) {
printErrors($result);
}
}
printErrors($result);
/*
name: Value is empty
spells.charm.name: Value did not pass filter 'minLength'
spells.3: Value did not pass filter 'alpha'
*/