<?php require __DIR__ . '/vendor/autoload.php'; $validate = new \PHPixie\Validate(); $data = array('name' => 'Pixie', 'home' => 'Oak', 'age' => 200, 'type' => 'fairy'); // There are multiple syntaxes supported // for building the validator // The standard approach $validator = $validate->validator(); $document = $validator->rule()->addDocument(); $document->valueField('name')->required()->addFilter()->alpha()->minLength(3); // You can also add filters as array $document->valueField('home')->required()->addFilter()->filters(array('alpha', 'minLength' => array(3))); // A shorthand approach $document->valueField('age')->required()->filter('numeric'); // Shorthand with multiple filters $document->valueField('type')->required()->callback(function ($result, $value) { if (!in_array($value, array('fairy', 'pixie'))) { // If does not pass add an error to the result $result->addMessageError("Type can be either 'fairy' or 'pixie'"); } }); // By default valdiator 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"); }
<?php require __DIR__ . '/vendor/autoload.php'; $validate = new \PHPixie\Validate(); // Imagine you want to validate this structure $data = array('name' => 'Pixie', 'home' => array('location' => 'forest', 'name' => 'Oak'), 'spells' => array('charm' => array('name' => 'Charm Person', 'type' => 'illusion'), 'blast' => array('name' => 'Fire Blast', 'type' => 'evocation'))); // There are two approaches to define the validator, // you can use either builder methods or callbacks // Builder approach $validator = $validate->validator(); $document = $validator->rule()->addDocument(); $document->valueField('name')->required()->addFilter()->alpha()->minLength(3); $homeDocument = $document->valueField('home')->required()->addDocument(); $homeDocument->valueField('location')->required()->addFilter()->in(array('forest', 'meadow')); $homeDocument->valueField('name')->required()->addFilter()->alpha(); $spellsArray = $document->valueField('spells')->required()->addArrayOf()->minCount(1); // Validate array keys $spellDocument = $spellsArray->valueKey()->filter('alpha'); $spellDocument = $spellsArray->valueItem()->addDocument(); $spellDocument->valueField('name')->required()->addFilter()->minLength(3); $spellDocument->valueField('type')->required()->addFilter()->alpha(); // Or a callback approach $validator = $validate->validator(function ($value) { $value->document(function ($document) { $document->field('name', function ($name) { $name->required()->filter(array('alpha', 'minLength' => array(3))); })->field('home', function ($home) { $home->required()->document(function ($home) { $home->field('location', function ($location) { $location->required()->addFilter()->in(array('forest', 'meadow')); });