Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation of POST data without a model in Silex / Symfony 2?

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);
like image 877
gremo Avatar asked Aug 10 '12 22:08

gremo


2 Answers

Well you can validate array with Symfony2 Validator component, e.g

//namespace declaration    
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\Validator\Constraints\NotBlank;
//....

 //validation snippet

  $constraint = new Collection(array(
    'email' => new Email(),
    'last'  => new NotBlank(),
    'birthday' => new Date(),
  ));

  $violationList = $this->get('validator')->validateValue($request->request->all(), $constraint);

  $errors = array();
  foreach ($violationList as $violation){
    $field = preg_replace('/\[|\]/', "", $violation->getPropertyPath());
    $error = $violation->getMessage();
    $errors[$field] = $error;
  }
like image 133
Mun Mun Das Avatar answered Nov 19 '22 12:11

Mun Mun Das


If you want to build an API with Symfony2 (similar with silex), there is a good tutorial here: http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/

The best way to validate the sent values on silex is still to use validation & form components (with model) ! They are made to perform this ! Read a full slide created by Hugo Hamon to build your API with silex ! http://www.slideshare.net/hhamon/silex-meets-soap-rest (check page 42 for validation)

Don't validate one by one sent elements in the action.

By doing this, you'll keep your code clean and evolutive !

like image 3
Sybio Avatar answered Nov 19 '22 10:11

Sybio