/** * @Put("/chapters/{lesson}/{chapter}", * requirements={"lesson" = "\d+"}) * @ParamConverter("lesson", class="IcapLessonBundle:Lesson") * @ParamConverter("chapter", class="IcapLessonBundle:Chapter", options={"mapping": {"chapter": "slug"}}) */ public function editChapterAction(Lesson $lesson, Chapter $chapter) { // CHECK ACCESS $this->apiCheckAccess('EDIT', $lesson); $translator = $this->get('translator'); $chapterType = new ChapterType($this->localeManager); $defaults = array('csrf_protection' => false, 'allow_extra_fields' => true); $form = $this->formFactory->create($chapterType, new Chapter(), $defaults); $payload = $this->request->request->get('chapter'); $form->submit($payload); if ($form->isValid()) { $chapter->setTitle($payload['title']); // Chapter text is not mandatory if (array_key_exists('text', $payload)) { $chapter->setText($payload['text']); } // Set slug to null in order to re-generate it $chapter->setSlug(null); $em = $this->getDoctrine()->getManager(); $unitOfWork = $em->getUnitOfWork(); $unitOfWork->computeChangeSets(); $changeSet = $unitOfWork->getEntityChangeSet($chapter); $em->flush(); $this->dispatchChapterUpdateEvent($lesson, $chapter, $changeSet); return array('message' => $translator->trans('Your chapter has been modified', array(), 'icap_lesson'), 'chapter' => $this->formatChapterData($chapter)); } // Form is not valid. What should we return? return array('message' => $translator->trans('Your chapter has not been modified', array(), 'icap_lesson'), 'errors' => $form); }