public function testBindFromProfileWillPopulateDataFromProfileEntity() { $profileRaw = ['id' => 1, 'fullname' => "Trinh Thanh Tam", 'dob' => '2018-06-18', 'email' => '*****@*****.**']; $profile = new Application_Model_Profile(['data' => $profileRaw]); $this->form->bindFromProfile($profile); $this->assertEquals($profileRaw, $this->form->getValues()); }
public function indexAction() { // display the profile form and populate if profile exists $request = $this->getRequest(); $form = new Application_Form_Profile(); $auth = Zend_Auth::getInstance(); $identity = $auth->getIdentity(); $profileMapper = new Application_Model_ProfileMapper(); $profile = new Application_Model_Profile(); $exists = $profileMapper->exists($identity->id); if ($request->isPost()) { if ($form->isValid($request->getPost())) { $profile->setOptions($form->getValues()); $profile->setUserId($identity->id); $profileMapper->save($profile, $exists); // display success message $this->view->msg = "<p class='msg'>Profile saved</p>"; } } else { $profileMapper->find($identity->id, $profile); $data = array('first_name' => $profile->getFirstName(), 'last_name' => $profile->getLastName(), 'birthdate' => date_format(new DateTime($profile->getBirthdate()), 'Y-m-d'), 'gender' => $profile->getGender()); $form->populate($data); } $this->view->form = $form; }
public function indexAction() { $form = new Application_Form_Profile(); $form->setMethod('POST'); $form->setDefaultsFromEntity($this->user); $request = $this->getRequest(); if ($request->isPost() && $form->isValid($request->getPost())) { $values = $form->getValues(); try { if (!empty($values['image'])) { $imageInfo = array_pop($form->image->getFileInfo()); $values['image'] = $this->_helper->service('image')->save($imageInfo); } $this->service->save($values, $this->user); $this->_helper->redirector('index'); } catch (\InvalidArgumentException $e) { switch ($e->getMessage()) { case 'username_conflict': $form->username->addError($this->view->translate("User with given username exists.")); break; default: $form->image->addError($e->getMessage()); break; } } } $this->view->user_first_name = $this->user->getFirstName(); $this->view->user_last_name = $this->user->getLastName(); $this->view->user_email = $this->user->getEmail(); $this->view->form = $form; $this->view->user = new MetaUser($this->user); $this->view->first_time = $this->_getParam('first', false); }
public function editAction() { $authorization = Zend_Auth::getInstance(); $identity = $authorization->getIdentity(); if ($identity) { $form = new Application_Form_Profile(); $model = new Application_Model_Users(); $id = $this->getRequest()->getParam('id'); $user_data = $model->getUserById($id); $form->populate($user_data); // $form->getElement('password')->setValue($user_data[0]['password']); // $form->getElement('re_password')->setValue($user_data[0]['password']); if ($identity->user_type == "admin") { if ($this->getRequest()->isPost()) { $form->getElement('id')->setValue($id); if ($form->isValid($this->getRequest()->getParams())) { $model = new Application_Model_Users(); $form->removeElement("re_password"); $model->editUser($form->getValues(), $id); //$this->view->identity->id $this->redirect("/users/list"); } } } else { $form->removeElement("user_type"); $form->removeElement("active"); if ($this->getRequest()->isPost()) { $form->getElement('id')->setValue($id); if ($form->isValid($this->getRequest()->getParams())) { $model = new Application_Model_Users(); $form->removeElement("re_password"); $model->editUser($form->getValues(), $id); //$this->view->identity->id $this->redirect("categories/list"); } } } $this->view->form = $form; //$this->render("add"); } else { $this->redirect("/users/login"); } }
/** * Edit existed profile * * Handler GET, POST request * @link GET /profile/edit/:id display form profile populated * @link POST /profile/edit persit profile * */ public function editAction() { $profileRepoFactory = new Application_Factory_ProfileRepository(); $profileModelFactory = new Application_Factory_ProfileModel(); $profileForm = new Application_Form_Profile(['id' => 'edit-profile']); $profileForm->submit->setLabel("Save"); $profileRepo = $profileRepoFactory->createService(); $this->view->profileForm = $profileForm; //GET request handler $visitEditProfilePage = !$this->getRequest()->isPost(); if ($visitEditProfilePage) { $profileId = (int) $this->getParam('id', 0); $profileEntity = $profileRepo->findById($profileId); $profileForm->bindFromProfile($profileEntity); return; //render edit profile form } //POST request handler $postInvalidProfile = !$profileForm->isValid($this->getRequest()->getPost()); if ($postInvalidProfile) { return; //represent profile form with error messages } //Persit filtered profile to persistent $profileRepo->save($profileModelFactory->createService($profileForm->getValues())); $this->_helper->redirector('index', 'profile', 'default'); }