Ejemplo n.º 1
0
 /**
  * Edit action
  *
  */
 public function editAction()
 {
     $post = new Post();
     if ($this->params('id') > 0) {
         $post = $this->getEntityManager()->getRepository('Blog\\Entity\\Post')->find($this->params('id'));
     }
     $form = new PostForm($this->getEntityManager());
     $form->setHydrator(new DoctrineEntity($this->getEntityManager(), 'Blog\\Entity\\Post'));
     $form->bind($post);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($post->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $em = $this->getEntityManager();
             $em->persist($post);
             $em->flush();
             $this->flashMessenger()->addSuccessMessage('Post Saved');
             $this->flashMessenger()->addMessage('sadf');
             $this->flashMessenger()->addErrorMessage('2312');
             return $this->redirect()->toRoute('post');
         }
     }
     return new ViewModel(array('post' => $post, 'form' => $form));
 }
Ejemplo n.º 2
0
 public function addAction()
 {
     $formPost = new PostForm($this->categoryTable);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = new Post();
         $formPost->setInputFilter($post->getInputFilter());
         $nonFiles = $this->getRequest()->getPost()->toArray();
         $files = $this->getRequest()->getFiles()->toArray();
         // Pour ZF 2.2.x uniquement
         $data = array_merge_recursive($nonFiles, $files);
         $formPost->setData($data);
         if ($formPost->isValid()) {
             $size = new Size(array('max' => 716800));
             $adapter = new \Zend\File\Transfer\Adapter\Http();
             $adapter->setValidators(array($size), $files['image']);
             if (!$adapter->isValid()) {
                 $dataError = $adapter->getMessages();
                 $error = array();
                 foreach ($dataError as $key => $row) {
                     $error[] = $row;
                 }
                 $formPost->setMessages(array('image' => $error));
             } else {
                 $adapter->setDestination('./public/img/');
                 if ($adapter->receive($files['image']['name'])) {
                     $post->exchangeArray($formPost->getData());
                     $post->image = $files['image']['name'];
                     $this->postTable->savePost($post);
                     return $this->redirect()->toRoute('home');
                 }
             }
         }
     }
     return new ViewModel(array('form' => $formPost));
 }
Ejemplo n.º 3
0
 public function editAction()
 {
     $id = (int) $this->params('id');
     if (!$id) {
         return $this->redirect()->toRoute('posts', array('action' => 'new'));
     }
     try {
         $post = $this->getPostTable()->getPost($id);
     } catch (\Exception $ex) {
         return $this->redirect()->toRoute('posts', array('action' => 'index'));
     }
     $table = $this->getServiceLocator()->get('Blog\\Model\\CategoryTable');
     $form = new PostForm($table);
     $form->bind($post);
     $form->get('submit');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($post->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $this->getPostTable()->savePost($post);
             return $this->redirect()->toRoute('posts', array('action' => view, 'id' => $id));
         }
     }
     return array('id' => $id, 'form' => $form, 'post' => $post);
 }