Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ZendFramework] problem z widokiem/akcją podczas walidowania formularza
Forum PHP.pl > Forum > PHP > Frameworki
csharp
Cześć,
problem wygląda następująco: (sorry za całe kawałki kodu ale zależy mi aby ktoś rzucił swoim okiem i stwierdził w jakim stopniu jest to bezpieczne).
- mam formularz do logowania - wygląda o tak:

  1. public function init()
  2. {
  3. $this->setMethod('post');
  4.  
  5. $view = Zend_Layout::getMvcInstance()->getView();
  6. $url = $view->url(array(
  7. 'controller' => 'index', 'action' => 'login'
  8. ));
  9.  
  10. $this->setAction($url);
  11. $this->addElement('text', 'username', array('value' => 'Login', 'required' => true, 'filters' => array('StringTrim'),));
  12. $this->addElement('password', 'password', array('value' => 'Hasło', 'required' => true,));
  13. $this->addElement('submit', 'submit', array('label' => '', 'id' => 'submit_login', 'ignore' => true));
  14.  
  15.  
  16.  
  17. $this->setOptions(array(
  18. 'class' => 'login_aside_form'
  19. ));
  20.  
  21. $this->addDecorators(array(
  22. 'FormElements',
  23. array('Form')
  24. ));
  25.  
  26. $this->username->clearDecorators();
  27. $this->password->clearDecorators();
  28.  
  29. $this->setElementDecorators(
  30. 'ViewHelper',
  31. 'Errors',
  32. )
  33. );
  34. }


dalej mam akcję odpowiedzialną za formularz: (dorobić muszę tutaj jeszcze jakieś sha1() dla hasła)
  1. Zend_Db_Table_Abstract::setDefaultAdapter('db1');
  2.  
  3. $this->_helper->viewRenderer('index');
  4. $form = new Application_Form_Login();
  5. if ($form->isValid($this->getRequest()->getPost())) {
  6.  
  7. $adapter = new Zend_Auth_Adapter_DbTable(
  8. null,
  9. 'user',
  10. 'username',
  11. 'password'
  12. );
  13.  
  14. $adapter->setIdentity($form->getValue('username'));
  15. $adapter->setCredential($form->getValue('password'));
  16.  
  17. $auth = Zend_Auth::getInstance();
  18.  
  19. $result = $auth->authenticate($adapter);
  20.  
  21. if ($result->isValid()) {
  22. return $this->_helper->redirector(
  23. 'index',
  24. 'index',
  25. 'default'
  26. );
  27. }
  28. $form->password->addError('Błędna próba logowania!');
  29. }
  30. $this->view->formLogin = $form;


sam formularz wyświetlam w taki sposób:

  1. <form action="<?php echo $this->formLogin->getAction() ?>" method="post">
  2. <?php echo trim($this->formLogin->username)."\n" ?>
  3. <?php echo trim($this->formLogin->password)."\n" ?>
  4. <?php //<input type="text" id="password-clear" value="Hasło:"> ?>
  5. <div class="login_links">
  6. <a href="#" id="register_link_aside"
  7. title="Zarejestruj się">Zarejestruj się</a>
  8. <a href="#" id="password_link_aside"
  9. title="Przypomnij hasło">Przypomnij hasło</a>
  10. <?php echo trim($this->formLogin->submit)."\n" ?>
  11. </div><!-- end .login_links -->
  12. </form>



i teraz mam taki problem, że formularz jest wyświetlany w głównym widoku layout.phtml i w czasie sprawdzania czy np. pole formularza nie jest puste, przenosi użytkownika do akcji index/login, a chciałbym aby cały czas pozostawał on w index/index.
Problem nie występuje jak się user poprawnie zaloguje, wtedy zrobiłem coś takiego:

  1. if ($result->isValid()) {
  2. return $this->_helper->redirector(
  3. 'index',
  4. 'index',
  5. 'default'
  6. );
  7. }


i user trafia do index/index

niby akcja index/login korzysta z widoku index
  1. $this->_helper->viewRenderer('index');
ale to nie eliminuje mi problemu.. co tu można zmienić?
Pilsener
  1. $view = Zend_Layout::getMvcInstance()->getView();


Można chyba tak:
  1. $view = $this->getView();


A co do problemu to czegoś nie rozumiem, chcesz, żeby
Cytat
w czasie sprawdzania czy np. pole formularza nie jest puste, przenosi użytkownika do akcji index/login, a chciałbym aby cały czas pozostawał on w index/index


to czemu dajesz do action url:
  1. $url = $view->url(array(
  2. 'controller' => 'index', 'action' => 'login'
  3. ));
-? Tym bardziej, że piszesz, że i tak przekierowujesz usera po udanym logowaniu na index/index - to dlaczego nie dać action=""?

csharp
masz rację.. w zasadzie sam pokręciłem, już sobie to poukładałem i działa jak należy.

action url dawałem do index/login, bo tam była akcja odpowiedzialna za poprawne logowanie ale już sobie z tym wszystkim poradziłem.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.