public function editAction()
 {
     $auth = new AuthenticationService();
     if (!$auth) {
         return $this->redirect()->toRoute('home');
     }
     $form = new CategoryForm();
     $id = (int) $this->params('id');
     $category = $this->getCategoryTable()->getCategory($id);
     $form->bind($category);
     $form->get('submit')->setAttribute('value', 'edit');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $category = $request->getPost();
         //echo '<pre>'; print_r($category); echo '<pre>'; die;
         $this->getCategoryTable()->saveCategory($category);
         return $this->redirect()->toRoute('category');
     }
     $viewModel = new ViewModel(array('form' => $form, 'id' => $id));
     $viewModel->setTerminal(true);
     return $viewModel;
 }
Example #2
0
 public function editAction()
 {
     $id = (int) $this->params('id');
     if (!$id) {
         return $this->redirect()->toUrl('zfcadmin/category');
     }
     try {
         $category = $this->getServiceLocator()->get('Application\\Service\\CategoryService')->getById($id);
     } catch (\Exception $ex) {
         return $this->redirect()->toUrl('zfcadmin/category');
     }
     $formCategory = new CategoryForm();
     $formCategory->bind($category);
     $formCategory->get('submit')->setValue('Modifier');
     // On récupère l'objet Request
     $request = $this->getRequest();
     // On vérifie si le formulaire a été posté
     if ($request->isPost()) {
         // Et on passe l'InputFilter de Category au formulaire
         $formCategory->setInputFilter($category->getInputFilter());
         $formCategory->setData($request->getPost());
         // Si le formulaire est valide
         if ($formCategory->isValid()) {
             // On enregistre ces données dans la table Category
             $this->getServiceLocator()->get('Application\\Service\\CategoryService')->saveCategory($category);
             $this->getServiceLocator()->get('Zend\\Log')->info("La catégorie '{$category->name}' a été modifié");
             $this->flashMessenger()->addMessage(array('success' => "La catégorie '{$category->name}' a été modifié"));
             // Puis on redirige sur la page d'accueil.
             return $this->redirect()->toRoute('zfcadmin/category');
         }
         // Si le formulaire n'est pas valide, on reste sur la page et les erreurs apparaissent
         foreach ($formCategory->getMessages() as $messageId => $message) {
             $this->getServiceLocator()->get('Zend\\Log')->err("Validation failure '{$messageId}': {$message}");
             $this->flashMessenger()->addMessage(array('error' => "Validation failure '{$messageId}': {$message}"));
         }
     }
     return new ViewModel(array('form' => $formCategory, 'id' => $id, 'flashMessages' => $this->flashMessenger()->getMessages()));
 }