/** * Creates a new Post entity. * * @Route("/new", name="admin_category_new") * @Method({"GET", "POST"}) * * NOTE: the Method annotation is optional, but it's a recommended practice * to constraint the HTTP methods each controller responds to (by default * it responds to all methods). */ public function newAction(Request $request) { $category = new Category(); $category->setAuthorEmail($this->getUser()->getEmail()); // See http://symfony.com/doc/current/book/forms.html#submitting-forms-with-multiple-buttons $form = $this->createForm('AppBundle\\Form\\CategoryType', $category)->add('saveAndCreateNew', 'Symfony\\Component\\Form\\Extension\\Core\\Type\\SubmitType'); $form->handleRequest($request); // the isSubmitted() method is completely optional because the other // isValid() method already checks whether the form is submitted. // However, we explicitly add it to improve code readability. // See http://symfony.com/doc/current/best_practices/forms.html#handling-form-submits if ($form->isSubmitted() && $form->isValid()) { $category->setSlug($this->get('slugger')->slugify($category->getTitle())); $category->setAlias($this->get('slugger')->slugify($category->getTitle())); $category->setState(Category::activo); $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($category); $entityManager->flush(); // Flash messages are used to notify the user about the result of the // actions. They are deleted automatically from the session as soon // as they are accessed. // See http://symfony.com/doc/current/book/controller.html#flash-messages $this->addFlash('success', 'category.created_successfully'); if ($form->get('saveAndCreateNew')->isClicked()) { return $this->redirectToRoute('admin_category_new'); } return $this->redirectToRoute('admin_category_index'); } return $this->render('admin/category/new.html.twig', array('category' => $category, 'form' => $form->createView())); }