- PHP 5.4
- composer (preferably latest)
$ composer require renegare/aiv:dev-master
<?php
$app = new \Silex\Application();
$app->register(new \AIV\Integration\SilexProvider, [
'aiv.validators' => [
'test-name' => [
'options' => [
'allow.extra.params' => true,
'allow.missing.params' => true
],
'params' => [
'name' => [
'not.blank',
[
'type' => 'length',
'options' => ['min' => 2, 'max' => 20]]],
'email' => ['not.blank', '%email.validator%'],
'password' => ['not.blank']]]]]);
$app['email.validator'] = $app->share(function() {
return new \Symfony\Component\Validator\Constraints\Email;
});
$app->post('/', function(Application $app) {
$apiValidator = $app['validator'];
if($apiValidator->hasErrors('test-name')) {
$errors = [];
foreach($apiValidator->getErrors('test-name') as $violation) {
$path = preg_replace('/[\[\]]/', '', $violation->getPropertyPath());
$errors[$path] = $violation->getMessage();
}
return sprintf('You have errors: <pre>%s</pre>', print_r($errors, true));
} else {
return sprintf('You sent me valid data:<br /><pre>%s</pre>', print_r($apiValidator->getData('test-name'), true));
}
});
$app->run();
Typical JSON Rest API applications take input from the body in the form of json. There the default input handler will not work as it effectively looks in the $_POST array.
Simply add this code after registering the provider:
$app['aiv.input'] = $this->share(function(){
new \AIV\Input\SymfonyRequest\JSONInput;
});
Check out the repo and from the top level directory run the following command (xdebug required for coverage):
$ composer update && vendor/bin/phpunit --coverage-text
- "Modelesque" Classes that represent validation requirements ($instance_variables vs annotations)
The actual validation of data is handled by: Symfony/Validator Component