I'm building a RESTful application that will serve only json/xml data and i've chosen Silex because i already know (a bit) Symfony 2 and because is small, i don't need Twig, etc...
There are no models, just plain old SQL queries using Doctrine dbal, and the serializer. Anyway, i should validate POST/PUT requests. How this can be done without using form component and models?
I mean POST data is an array. Can i validate it (adding constraints) and how?
EDIT: Ok, right now i've found an interesting library, that is respect/validation. It uses also sf constraints, if needed. I ended up with something like this (early code :P), that i will use if there is nothing better:
$v = $app['validation.respect'];
$userConstraints = array(
'last' => $v::noWhitespace()->length(null, 255),
'email' => $v::email()->length(null, 255),
'mobile' => $v::regex('/^\+\d+$/'),
'birthday' => $v::date('d-m-Y')->max(date('d-m-Y')),
);
// Generic function for request keys intersection
$converter = function(array $input, array $allowed)
{
return array_intersect_key($input, array_flip($allowed));
};
// Convert POST params into an assoc. array where keys are only those allowed
$userConverter = function($fields, Request $request) use($converter) {
$allowed = array('last', 'email', 'mobile', 'birthday');
return $converter($request->request->all(), $allowed);
};
// Controller
$app->match('/user', function(Application $app, array $fields)
use($userConstraints) {
$results = array();
foreach($fields as $key => $value)
$results[] = $userConstraints[$key]->validate($value);
})->convert('fields', $userConverter);