Exemplo n.º 1
0
 /**
  * Handle Create or Update Workflow of a ZFDoctrine_Form_Model instance
  * 
  * @throws ZFDoctrine_DoctrineException
  * @param ZFDoctrine_Form_Model $form
  * @param string $action
  * @param string $controller
  * @param string $module
  * @param array $params
  * @return void
  */
 public function handleForm(ZFDoctrine_Form_Model $form, $action = null, $controller = null, $module = null, array $params = array())
 {
     $actionController = $this->getActionController();
     $request = $actionController->getRequest();
     if (!$action) {
         $action = $this->getActionController();
     }
     $id = null;
     $actionParams = array();
     if ($this->getRecordIdParam()) {
         $id = $request->getParam($this->getRecordIdParam());
         if ($id) {
             $actionParams = array($this->getRecordIdParam() => $id);
             $table = Doctrine_Core::getTable($form->getModelName());
             $record = $table->find($id);
             if (!$record) {
                 throw new ZFDoctrine_DoctrineException("Cannot find record with given id.");
             }
             $actionController->view->assign('recordId', $id);
             $actionController->view->assign('record', $record);
             $form->setRecord($record);
         }
     }
     $urlHelper = $actionController->getHelper('url');
     $form->setMethod('post');
     $form->setAction($urlHelper->simple($request->getActionName(), $request->getControllerName(), $request->getModuleName(), $actionParams));
     if ($request->isPost() && $form->isValid($request->getPost())) {
         $form->save();
         $redirector = $actionController->getHelper('redirector');
         return $redirector->gotoSimple($action, $controller, $module, $params);
     }
     $actionController->view->assign('form', $form);
 }
Exemplo n.º 2
0
 public function testRecordLoading()
 {
     $this->_initAdapter('User');
     $this->_adapter->expects($this->any())->method('getRecord')->will($this->onConsecutiveCalls(false, true));
     $this->_adapter->expects($this->once())->method('getNewRecord')->will($this->returnValue(false));
     $form = new ZFDoctrine_Form_Model(array('model' => 'User', 'adapter' => $this->_adapter));
     //First getRecord is set up to return false
     $this->assertFalse($form->getRecord());
     $user = array('login' => 'Login', 'password' => 'Password');
     $this->_adapter->expects($this->once())->method('setRecord')->with($this->equalTo($user));
     //NOTE: will cause a wrong value to be inputted into the password field!
     $this->_adapter->expects($this->any())->method('getRecordValue')->will($this->returnValue('Login'));
     $form->setRecord($user);
     //Second getRecord is set up to return true
     $this->assertTrue($form->getRecord());
     $this->assertEquals('Login', $form->getElement('login')->getValue());
 }