Example #1
0
 public function addAction()
 {
     $form = new BookForm();
     $form->get('submit')->setValue('Add');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $book = new Book();
         $form->setInputFilter($book->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $book->exchangeArray($form->getData());
             $this->getBookTable()->saveBook($book);
             //send email
             $mail = new Mail\Message();
             $mail->setBody('A new book called ' . $book->title . ' has been added.');
             $mail->setFrom('*****@*****.**', 'Zend Course');
             $mail->addTo('*****@*****.**', 'Myself');
             $mail->setSubject('A Book was added');
             $transport = new Mail\Transport\Sendmail();
             $transport->send($mail);
         }
         //Redirect to books list
         return $this->redirect()->toRoute('book');
     }
     return array('form' => $form);
 }
 /**
  * Add Action
  * <br/> Responsible for :
  *       <br/>In case Get verb  -> Display Add new Book Form
  *       <br/>In case Post verb -> Add new book details into Database
  *                                 After Submit Book Form
  * @return ViewModel add view
  */
 public function addAction()
 {
     $form = new BookForm();
     $form->get('submit')->setValue('Add');
     // Another way to set Submit button value
     // $form->get('submit')->setAttribute('value', 'Add');
     $request = $this->getRequest();
     // Check If request is Post verb
     if ($request->isPost()) {
         $book = new Book();
         $form->setInputFilter($book->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $book->exchangeArray($form->getData());
             $this->getBookTable()->saveBook($book);
             //Send an email when a new book Was Added
             $mail = new Mail\Message();
             $mail->setBody('A new Book called ' . $book->title . ' has been added.');
             $mail->setFrom('*****@*****.**', 'Zend course');
             $mail->addTo('*****@*****.**', 'Ahmed Hamdy');
             $mail->setSubject('A Book was Added');
             $transport = new Mail\Transport\Sendmail();
             $transport->send($mail);
         }
         // redirect to list of books
         return $this->redirect()->toRoute('book');
     }
     return new ViewModel(array('form' => $form));
 }
 public function addAction()
 {
     $form = new BookForm();
     $form->get('submit')->setValue('Add');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $book = new Book();
         $form->setInputFilter($book->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $book->exchangeArray($form->getData());
             $this->getBookTable()->saveBook($book);
         }
         return $this->redirect()->toRoute('book');
     }
     return array('form' => $form);
 }