/** * Processes API Form * * @param $entity * @param null $parameters * @param string $method * * @return Response */ protected function processForm($entity, $parameters = null, $method = 'PUT') { if ($parameters === null) { //get from request $parameters = $this->request->request->all(); } //unset the ID in the parameters if set as this will cause the form to fail if (isset($parameters['id'])) { unset($parameters['id']); } //is an entity being updated or created? if ($entity->getId()) { $statusCode = Codes::HTTP_OK; $action = 'edit'; } else { $statusCode = Codes::HTTP_CREATED; $action = 'new'; } $form = $this->createEntityForm($entity); $submitParams = $this->prepareParametersForBinding($parameters, $entity, $action); $form->submit($submitParams, 'PATCH' !== $method); if ($form->isValid()) { $this->preSaveEntity($entity, $form, $parameters, $action); $this->model->saveEntity($entity); $headers = array(); //return the newly created entities location if applicable if (Codes::HTTP_CREATED === $statusCode) { $headers['Location'] = $this->generateUrl('mautic_api_get' . $this->entityNameOne, array('id' => $entity->getId()), true); } $this->preSerializeEntity($entity, $action); $view = $this->view(array($this->entityNameOne => $entity), $statusCode, $headers); $this->setSerializationContext($view); } else { $formErrors = $this->getFormErrorMessages($form); $msg = $this->getFormErrorMessage($formErrors); return $this->returnError($msg, Codes::HTTP_BAD_REQUEST, $formErrors); } return $this->handleView($view); }