Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Zend_Form - raz działa, a raz nie
Forum PHP.pl > Forum > PHP > Frameworki
KOMPsognat
Zdaję sobie sprawę z tego, że nazwa tematu wiele nie mówi. Niestety nic lepszego nie wpadło mi do głowy.

Zend Framework poznaję od niedawna i problemów mi nie sprawia prócz jednego modułu - Zend_Form. Ale do rzeczy:

1. logowanie do systemu wykonałem w następujący sposób:
Administration_LoginController:
  1. <?php
  2.  
  3. class Administration_LoginController extends Zend_Controller_Action {
  4. public function init() {
  5. $this->_helper->layout()->setLayout('login');
  6. $this->_auth = Zend_Auth::getInstance();
  7. }
  8.  
  9. public function indexAction() {
  10. $this->view->form = $this->getForm()->render();
  11. }
  12.  
  13. protected function getForm() {
  14. $form = new Zend_Form();
  15. $form->setAction('/administracja/logowanie')
  16.  ->setMethod('get');
  17.  
  18. $username = $form->createElement('text', 'login', array('label' => 'Login:'));
  19. $username->setRequired(true);
  20.  
  21. $password = $form->createElement('password', 'password', array('label' => 'Hasło:'));
  22. $password->setRequired(true);
  23.  
  24. $form->addElement($username)
  25.  ->addElement($password)
  26.  ->addElement('submit', 'zaloguj', array('label' => 'Zaloguj'));
  27. return $form;
  28. }
  29.  
  30. public function loginAction() {
  31. $this->view->status = 1;
  32. if (!$this->getRequest()->isPost()) {
  33. return $this->_forward('index');
  34. }
  35. $form = $this->getForm();
  36. if (!$form->isValid($_POST)) {
  37. $this->view->form = $form->render();
  38. $this->render('index');
  39. } else {
  40. $values = $form->getValues();
  41. $authAdapter = Zend_Registry::get('auth');
  42. $authAdapter->setIdentity($values['login'])
  43. ->setCredential($values['password']);
  44. $result = $this->_auth->authenticate($authAdapter);
  45. if(!$result->isValid()) {
  46. $this->view->status = 0;
  47. return $this->_forward('index');
  48. } else {
  49. $this->_redirect('/administracja');
  50. }
  51. }
  52. }
  53.  
  54. public function logoutAction() {
  55. $this->_auth->clearIdentity();
  56. $this->_redirect('/administracja');
  57. }
  58. }
  59. ?>


Router oczywiście przepisuje adresy pod odpowiednie akcje. I całośc działa świetnie.

2. Na jednej ze stron panelu administracyjnego chcę zrobić formularz do nadawania uprawnień użytkownikom. Teoretycznie żaden problem jednak jakimś cudem dane nie chcą przejść. Wkleiłem więc IDENTYCZNY (jak w kontrolerze powyżej) formularz do tego panelu. Efekt? "Proteza" w UserController nie dziala. ohmy.gif
Administration_UserController:
  1. <?php
  2.  
  3. class Administration_UserController extends Zend_Controller_Action {
  4. public function init() {
  5. $this->_helper->layout()->setLayout('administration');
  6. $this->_auth = Zend_Auth::getInstance();
  7. if (!$this->_auth->hasIdentity()) {
  8. $this->_redirect('/administracja/logowanie');
  9. } else {
  10. $this->_model['users'] = new Users();
  11. $this->user = $this->_model['users']->getUserInfo($this->_auth->getIdentity());
  12. $this->view->user = $this->user;
  13. }
  14. }
  15.  
  16. public function getForm() {
  17. $form = new Zend_Form();
  18. $form->setAction('/administracja/uzytkownicy/zatwierdz')
  19.  ->setMethod('get');
  20.  
  21. $username = $form->createElement('text', 'login', array('label' => 'Login:'));
  22.  
  23. $password = $form->createElement('password', 'password', array('label' => 'Hasło:'));
  24.  
  25. $form->addElement($username)
  26.  ->addElement($password)
  27.  ->addElement('submit', 'zaloguj', array('label' => 'Zaloguj'));
  28. return $form;
  29. }
  30.  
  31. public function saveAction() {
  32. $form = $this->getForm();
  33. $values = $form->getValues();
  34. if (!$form->isValid($_POST)) {
  35. echo '?';
  36. } else {
  37. print_r($values);
  38. }
  39. }
  40.  
  41. public function editAction() {
  42. $user = $this->_model['users']->find($this->getRequest()->getParam('nick'))->current();
  43. $this->view->user = $user;
  44. $form = $this->getForm();
  45. $this->view->form = $form->render();
  46. }
  47. }
  48. ?>


/administracja/uzytkownicy/zatwierdz - akcja save usercontrollera.

3. Po wysłaniu formularza z UserController efekt jest... o taki:
Kod
Array ( [login] => [password] => [zaloguj] => )


A na zakończenie, niczym prostak czekający aż ktoś zrobi coś za niego:
Liczę na szybką odpowiedź.
sapper
Hmm.
Formularz wysyła dane getem
  1. <?php
  2. ->setMethod('get');
  3. ?>


a ty odbierasz je postem
  1. <?php
  2. if (!$form->isValid($_POST)) {
  3. ?>


albo GET albo POST lub REQUEST cool.gif
chlebik
I zasadniczo zgodnie z obiektowka: $this->_request->getPost().
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.