Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace ApiBundle\Controller;
- use ApiBundle\Service\Authenticate;
- use FOS\RestBundle\Controller\FOSRestController;
- //use FOS\RestBundle\Controller\Annotations\RequestParam;
- use FOS\RestBundle\Request\ParamFetcher;
- use Symfony\Component\Form\FormInterface;
- use Symfony\Component\HttpFoundation\Request;
- use TaskBundle\Entity\Project;
- use TaskBundle\Entity\User;
- use FOS\RestBundle\Controller\Annotations\RequestParam;
- class ProjectController extends FOSRestController
- {
- /**
- *
- * @RequestParam(name="firstname", description="foo")
- *
- * @return \Symfony\Component\HttpFoundation\Response
- */
- public function postProjectsAction(Request $request, ParamFetcher $paramFetcher)
- {
- $form = $this->createForm(\TaskBundle\Form\ProfileType::class, new User());
- $this->processForm($request, $paramFetcher, $form);
- if (!$form->isValid()) {
- $errors = $this->getErrorsFromForm($form);
- var_dump($errors);
- die;
- }
- $data = [
- 'random' => random_int(1, 100000)
- ];
- $view = $this->view($data);
- return $this->handleView($view);
- }
- private function processForm(Request $request, ParamFetcher $paramFetcher, FormInterface $form)
- {
- $data = [];
- foreach ($paramFetcher->getParams() as $param) {
- $data[$param->getKey()] = $paramFetcher->get($param->getKey());
- }
- $clearMissing = $request->getMethod() != 'PATCH';
- $form->submit($data, $clearMissing);
- }
- private function getErrorsFromForm(FormInterface $form)
- {
- $errors = [];
- foreach ($form->getErrors() as $error) {
- $errors[] = $error->getMessage();
- }
- foreach ($form->all() as $childForm) {
- if ($childForm instanceof FormInterface) {
- if ($childErrors = $this->getErrorsFromForm($childForm)) {
- $errors[$childForm->getName()] = $childErrors;
- }
- }
- }
- return $errors;
- }
- }
Add Comment
Please, Sign In to add comment