passwordRequired() public method

Set if yes or no password is required when user click on Save
public passwordRequired ( ) : User
return User
Exemplo n.º 1
0
 /**
  * Edit user
  *
  * @return \Zend\View\Model\ViewModel|array
  */
 public function editAction()
 {
     $userId = $this->getRouteMatch()->getParam('id');
     $userModel = User\Model::fromId($userId);
     if (empty($userModel)) {
         $this->flashMessenger()->addErrorMessage("Can't edit this user");
         return $this->redirect()->toRoute('config/user');
     }
     $form = new UserForm();
     $form->setAttribute('action', $this->url()->fromRoute('config/user/edit', array('id' => $userId)));
     $form->loadValues($userModel);
     if ($this->getRequest()->isPost()) {
         $post = $this->getRequest()->getPost()->toArray();
         if (!empty($post['password'])) {
             $form->passwordRequired();
             $form->getInputFilter()->get('password_confirm')->getValidatorChain()->addValidator(new Identical($post['password']));
         }
         $form->setData($post);
         if ($form->isValid()) {
             $userModel->addData($post);
             $userModel->setActive(empty($post['active']) ? false : $post['active']);
             if (!empty($post['password'])) {
                 $userModel->setPassword($post['password']);
             }
             $userModel->save();
             $this->flashMessenger()->addSuccessMessage('This user has been saved');
             return $this->redirect()->toRoute('config/user/edit', array('id' => $userId));
         }
         $this->flashMessenger()->addErrorMessage('User can not be saved');
     }
     return array('form' => $form);
 }