私はjson/xmlデータのみを提供するRESTfulアプリケーションを構築しています.Symfony 2を(少し)知っているのでSilexを選択しました.Symfony 2は小さいのでTwigは必要ありません.
モデルはなく、Doctrine dbal とシリアライザーを使用した単純な古い SQL クエリだけです。とにかく、POST/PUT リクエストを検証する必要があります。フォーム コンポーネントとモデルを使用せずにこれを行うにはどうすればよいでしょうか?
つまり、POST データは配列です。私はそれを検証できますか (制約を追加します)、どのように?
編集:わかりました、今、私は興味深いライブラリを見つけました。それは、尊重/検証です。必要に応じて、sf 制約も使用します。私はこのようなものになりました(初期コード:P)。これ以上良いものがなければ使用します:
$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);