2017-05-30 3 views
1

Zum Beispiel habe ich die Entität Cat mit der Eigenschaft $ name (string).Symfony 2/3 REST API Validierung ohne Formular

ich senden POST/Katzen mit dem Parameter [name = 123]

Wie ich Daten aus Anfrage ohne Form bestätigen kann mit url? Oder REST API mit Formularvalidierung ist normal? Ich kann keine Informationen zur Validierung ohne Formular finden. Ich kann die Validierungskomponente ohne Formular verwenden, aber vielleicht gibt es ein Bündel, das mir bessere Ansätze als $ this-> get ('validator') -> validate ('data from request') geben kann.

Danke.

+0

Eine ähnliche Frage hier https://stackoverflow.com/questions/10324782/validating-entities-without-form-in-symfony-2 – Arno

Antwort

0

würde ich empfehlen, noch ein Formular mit Validierung zu handhaben, und Sie können die manuell einreichen simulieren: http://symfony.com/doc/current/form/direct_submit.html#calling-form-submit-manually

Ein sehr vereinfachtes Beispiel so etwas wie unten könnte dann.

$formFactory = new Symfony\Component\Form\FormFactory(); 

//build your "post" data from your submitted content via api 
$data = json_decode($request->getContent(), TRUE); 

//build your Form and Entity objects. 
$formType = new Path\To\CatForm(); //for example 
$entity = new Path\To\CatEntity(); 

$parameters = array(); 
$method = 'POST'; 

$form = $formFactory->create($formType, $entity, ['method' => $method]); 
$form->submit($parameters, ($method == 'PATCH' ? false : true)); 

if ($form->isValid()) { 
    //do stuff 
}