Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ZF] Dwa formularze, jedna akcja
Forum PHP.pl > Forum > PHP > Frameworki
razielnr1
Witajcie,

Mam napisaną akcję, która obsługuje dwa formularze.
Jeden się nie wyświetla choć wszystko wygląda dobrze..
Proszę pomóżcie bo już wysiadam... sad.gif

UserController.php

  1. public function editAction()
  2. {
  3. $auth = Zend_Auth::getInstance();
  4. $identity = $auth->getIdentity();
  5.  
  6.  
  7.  
  8. if ($identity) {
  9.  
  10. $form1 = new Application_Form_EditProfile();
  11. $form1->setAction($this->view->url());
  12.  
  13.  
  14. $form2 = new Application_Form_ChangePassword();
  15. $form2->setAction($this->view->url());
  16.  
  17.  
  18.  
  19.  
  20. if ($this->_request->isPost() && $this->_getParam('submitform1') && $form1->isValid($this->_request->getParams())) {
  21. $users = new Application_Model_DbTable_Users();
  22.  
  23.  
  24. $form1->populate(array(
  25. 'mail' => $identity->mail,
  26. 'imie' => $identity->imie,
  27. 'nazwisko' => $identity->nazwisko,
  28. 'data_ur' => $identity->data_ur,
  29. 'miejscowosc' => $identity->miejscowosc
  30. ));
  31.  
  32. $post = $this->getRequest()->getPost();
  33.  
  34. if (isset($post['mail']) && $post['mail'] != $identity->mail) {
  35.  
  36. $form1->mail->addValidator('Db_NoRecordExists', true, array('table' => 'users', 'field' => 'mail', 'messages' => array('recordFound' => 'Ten e-mail jest już zajęty')));
  37. }
  38.  
  39. if ($post && $form1->isValid($post)) {
  40. $rows = $users->find($identity->users_id);
  41.  
  42. if ($rows->count()) {
  43. $user = $rows->current();
  44. $user->setFromArray($post)->save();
  45.  
  46. $auth->clearIdentity();
  47. $adapter = new Zend_Auth_Adapter_DbTable (null, 'users', 'nazwa', 'users_id');
  48.  
  49. $adapter->setIdentity($user->nazwa);
  50. $adapter->setCredential($user->users_id);
  51.  
  52. $result = $auth->authenticate($adapter);
  53.  
  54. if ($result->isValid()) {
  55. $data = $adapter->getResultRowObject(null, array('haslo', 'salt'));
  56. $auth->getStorage()->write($data);
  57. }
  58. $this -> getHelper('viewRenderer') -> setNoRender(true);
  59. echo $this -> view -> render('user/_saveSuccess.phtml');
  60. return;
  61. }
  62.  
  63. }
  64. $this->view->EditProfileform= $form1;
  65. }
  66.  
  67.  
  68. elseif ($this->_request->isPost() && $this->_getParam('submitform2') && $form2->isValid($this->_request->getParams())) {
  69.  
  70. $User = new Application_Model_DbTable_Users();
  71. $select = $User->select()->where('nazwa = ?', $auth->getIdentity());
  72. $u = $User->fetchRow($select);
  73.  
  74. $this->_helper->viewRenderer('edit');
  75.  
  76. if ($u && $form2->isValid($this->getRequest()->getPost())) {
  77. $haslo = $form2->getValue('haslo');
  78. $salt = Application_My_Salt::getSalt();
  79.  
  80. $u->salt = $salt;
  81. $u->haslo = sha1($salt . $haslo);
  82. $u->save();
  83.  
  84. $mail = new Application_My_Mail_Gmail();
  85. $mail->mailChangePassword($u->mail, $haslo, $auth->getIdentity());
  86.  
  87. return $this->_helper->redirector('index', 'index', 'default');
  88. }
  89. $this->view->ChangePasswordform = $form2;
  90. }
  91.  
  92. }
  93.  
  94.  
  95. else {
  96. $this -> getHelper('viewRenderer') -> setNoRender(true);
  97. echo $this -> view -> render('user/_loginRequired.phtml');
  98. return;
  99. }
  100.  
  101. }


EditProfile.php

  1. <?php
  2.  
  3. class Application_Form_EditProfile extends Zend_Form
  4. {
  5.  
  6. public function init()
  7. {
  8. $this->setMethod('post');
  9.  
  10.  
  11. //E-mail
  12. $this->addElement(
  13. 'text',
  14. 'mail',
  15. 'label' => 'E-mail',
  16. 'required' => true,
  17. 'filters' => array(
  18. array('StringTrim'),
  19. array('StringToLower'),
  20. array('StripNewlines'),
  21. array('StripTags')
  22. ),
  23. 'validators' => array(
  24. array('notEmpty', true,
  25. array('messages' => array('isEmpty' => 'Proszę wpisać adres e-mail')
  26. )),
  27. array('EmailAddress', true,
  28. array('messages' =>
  29. array(Zend_Validate_EmailAddress::INVALID => 'Wpisany adres e-mail jest niepoprawny', Zend_Validate_EmailAddress::INVALID_FORMAT => 'Wpisany adres e-mail jest niepoprawny'
  30. ))),
  31. ),
  32.  
  33. )
  34.  
  35. );
  36.  
  37.  
  38. //Imię
  39. $this->addElement(
  40. 'text',
  41. 'imie',
  42. 'label' => 'Imię',
  43. 'required' => true,
  44. 'filters' => array('StringTrim'),
  45. 'validators' => array(
  46. array('notEmpty', true,
  47. array('messages' => array('isEmpty' => 'Proszę wpisać imię')
  48. ))),
  49. )
  50. );
  51.  
  52. //Nazwisko
  53. $this->addElement(
  54. 'text',
  55. 'nazwisko',
  56. 'label' => 'Nazwisko',
  57. 'required' => true,
  58. 'filters' => array('StringTrim'),
  59. 'validators' => array(
  60. array('notEmpty', true,
  61. array('messages' => array('isEmpty' => 'Proszę wpisać nazwisko')
  62. ))),
  63. )
  64. );
  65.  
  66. //Data urodzenia
  67. $this->addElement(
  68. 'text',
  69. 'data_ur',
  70. 'label' => 'Rok urodzenia',
  71. 'required' => false,
  72. 'filters' => array('StringTrim'), array('Int'),
  73. 'validators' => array('Int')
  74. )
  75. );
  76. $this->data_ur->getValidator('Int')->setMessages(array(Zend_Validate_Int::NOT_INT => "'%value%' nie jest poprawnym numerem roku"));
  77.  
  78. //Miejscowość
  79. $this->addElement(
  80. 'text',
  81. 'miejscowosc',
  82. 'label' => 'Miejscowość',
  83. 'required' => false,
  84. 'filters' => array('StringTrim'),
  85. )
  86. );
  87.  
  88. //Przycisk rejestracji
  89. $this->addElement(
  90. 'submit',
  91. 'submitform1',
  92. 'label' => 'Zapisz zmiany',
  93. 'class' => 'btn btn-primary'
  94. )
  95. );
  96. $this->submitform1->setDecorators(array('ViewHelper'));
  97. $this->submitform1->addDecorator('HtmlTag', array('tag' => 'dd', 'class' => 'first-button'));
  98.  
  99. }
  100.  
  101.  
  102. }





ChangePassword.php

  1. <?php
  2.  
  3. class Application_Form_ChangePassword extends Zend_Form
  4. {
  5. public function init()
  6. {
  7. $this->setMethod('post');
  8.  
  9. //Nowe hasło
  10. $this->addElement(
  11. 'password',
  12. 'haslo',
  13. 'label' => 'Nowe hasło',
  14. 'required' => true,
  15. 'validators' => array(
  16. array('notEmpty', true,
  17. array('messages' => array('isEmpty'=>'Proszę wpisać nowe hasło i je potwierdzić')
  18. )),
  19. array('stringLength', true,
  20. array('min' => 3, 'max' => 20, 'messages'=>
  21. array('stringLengthTooShort' => 'Hasło musi składać się z co najmniej 3 znaków',
  22. 'stringLengthTooLong' => 'Hasło musi składać się z maksymalnie 20 znaków',
  23. )
  24. )),
  25. ),
  26. )
  27. );
  28.  
  29. //Powtórz nowe hasło
  30. $this->addElement(
  31. 'password',
  32. 'haslo_confirm',
  33. 'label' => 'Powtórz nowe hasło',
  34. 'required' => true,
  35. 'validators' => array(
  36. array('notEmpty', true,
  37. array('messages' => array('isEmpty' => 'Proszę wpisać nowe hasło i je potwierdzić')
  38. )),
  39. array('identical', true,
  40. array('token'=>'haslo', 'messages' =>
  41. array('notSame' => 'Hasła muszą być takie same')
  42. )),
  43. ),
  44. )
  45. );
  46.  
  47. //Przycisk zmiany hasła
  48. $this->addElement(
  49. 'submit',
  50. 'submitform2',
  51. 'label' => 'Zmień hasło',
  52. 'class' => 'btn btn-primary'
  53. )
  54. );
  55. $this->submitform2->setDecorators(array('ViewHelper'));
  56. $this->submitform2->addDecorator('HtmlTag', array('tag' => 'dd', 'class' => 'first-button'));
  57. }
  58. }


Widok

  1. <?php echo $this->EditProfileform; ?>
  2. <script type="text/javascript">
  3. function ne(o){
  4. if(document.getElementById(o).style.display=='') document.getElementById(o).style.display = 'none';
  5. else document.getElementById(o).style.display='';
  6. }
  7. <br>
  8. <div onclick="ne('o1')" class="btn btn-link">
  9. <p>Zmień hasło</p>
  10. </div>
  11. <div id="o1" style="display:none;">
  12. <?php echo $this->ChangePasswordform; ?>
  13. </div>
melkorm
To czy przekazuje do widoku to:

  1. $this->view->EditProfileform= $form1;


determinuje ten warunek
  1. if ($this->_request->isPost() && $this->_getParam('submitform1') && $form1->isValid($this->_request->getParams())) {


to samo jest dla drugiego form'a - ciężk osię czyta ten kod, generalnie te dwa przyrównania powinieneś dać najlepiej w momencie tworzenia instancji formów

czyli tu:
  1. $form1 = new Application_Form_EditProfile();
  2.  
  3. $form1->setAction($this->view->url());
  4.  
  5.  
  6.  
  7.  
  8.  
  9. $form2 = new Application_Form_ChangePassword();
  10.  
  11. $form2->setAction($this->view->url());
razielnr1
Poradziłem sobie w inny sposób.

Jeden widok, dwie akcje.
Dwa formularze znajdują się w jednym widoku, a każdy z nich przetwarza oddzielna akcja.


Może komuś się przyda:

Akcje: edit (służy do zmiany danych profilu), changepass (do zmiany hasła)

  1. public function editAction()
  2. {
  3. $auth = Zend_Auth::getInstance();
  4. $identity = $auth->getIdentity();
  5.  
  6. if ($identity) {
  7. $users = new Application_Model_DbTable_Users();
  8.  
  9. $form1 = new Application_Form_EditProfile();
  10.  
  11.  
  12. $form1->populate(array(
  13. 'mail' => $identity->mail,
  14. 'imie' => $identity->imie,
  15. 'nazwisko' => $identity->nazwisko,
  16. 'data_ur' => $identity->data_ur,
  17. 'miejscowosc' => $identity->miejscowosc
  18. ));
  19.  
  20. $post = $this->getRequest()->getPost();
  21.  
  22. if (isset($post['mail']) && $post['mail'] != $identity->mail) {
  23.  
  24. $form1->mail->addValidator('Db_NoRecordExists', true, array('table' => 'users', 'field' => 'mail', 'messages' => array('recordFound' => 'Ten e-mail jest już zajęty')));
  25. }
  26.  
  27. if ($post && $form1->isValid($post)) {
  28. $rows = $users->find($identity->users_id);
  29.  
  30. if ($rows->count()) {
  31. $user = $rows->current();
  32. $user->setFromArray($post)->save();
  33.  
  34. $auth->clearIdentity();
  35. $adapter = new Zend_Auth_Adapter_DbTable (null, 'users', 'nazwa', 'users_id');
  36.  
  37. $adapter->setIdentity($user->nazwa);
  38. $adapter->setCredential($user->users_id);
  39.  
  40. $result = $auth->authenticate($adapter);
  41.  
  42. if ($result->isValid()) {
  43. $data = $adapter->getResultRowObject(null, array('haslo', 'salt'));
  44. $auth->getStorage()->write($data);
  45. }
  46. $this -> getHelper('viewRenderer') -> setNoRender(true);
  47. echo $this -> view -> render('user/_saveSuccess.phtml');
  48. return;
  49. }
  50.  
  51. }
  52.  
  53. $this->view->EditProfileform = $form1;
  54. $form2 = new Application_Form_ChangePassword();
  55. $form2->setAction($this->view->url(array('action' => 'changepass')));
  56. $this->view->ChangePasswordform = $form2;
  57.  
  58. }
  59. else {
  60. $this -> getHelper('viewRenderer') -> setNoRender(true);
  61. echo $this -> view -> render('user/_loginRequired.phtml');
  62. return;
  63. }
  64. }
  65.  
  66. public function changepassAction()
  67. {
  68.  
  69. $auth = Zend_Auth::getInstance();
  70. $identity = $auth->getIdentity();
  71.  
  72. if ($identity) {
  73.  
  74. $User = new Application_Model_DbTable_Users();
  75. $identity = $auth->getIdentity();
  76. $select = $User->select()->where('nazwa = ?', $identity->nazwa);
  77. $u = $User->fetchRow($select);
  78.  
  79. $form2 = new Application_Form_ChangePassword();
  80.  
  81. if ($u && $form2->isValid($this->getRequest()->getPost())) {
  82.  
  83. $haslo = $form2->getValue('haslo');
  84.  
  85. $salt = Application_My_Salt::getSalt();
  86.  
  87. $u->salt = $salt;
  88. $u->haslo = sha1($salt . $haslo);
  89. $u->save();
  90.  
  91. $mail = new Application_My_Mail_Gmail();
  92. $mail->mailChangePassword($u->mail, $haslo, $identity->nazwa);
  93.  
  94. $this -> getHelper('viewRenderer') -> setNoRender(true);
  95. echo $this -> view -> render('auth/_changepassSuccess.phtml');
  96. return;
  97. }
  98. $this->view->ChangePasswordform = $form2;
  99. }
  100. else {
  101. $this -> getHelper('viewRenderer') -> setNoRender(true);
  102. echo $this -> view -> render('user/_loginRequired.phtml');
  103. return;
  104. }
  105.  


Widok:

  1. <?php echo $this->EditProfileform; ?>
  2. <script type="text/javascript">
  3. function ne(o){
  4. if(document.getElementById(o).style.display=='') document.getElementById(o).style.display = 'none';
  5. else document.getElementById(o).style.display='';
  6. }
  7. </script>
  8. <br>
  9. <div onclick="ne('o1')" class="btn btn-link">
  10. <p>Zmień hasło</p>
  11. </div>
  12. <div id="o1" style="display:none;">
  13. <?php echo $this->ChangePasswordform; ?>
  14. </div>
  15.  
kaem
Taka mała moja uwaga offtopic, bo mnie to drażni tongue.gif
Jeśli mam kod takiej postaci:
  1. if(TRUE)
  2. {
  3. /*
  4. *
  5. * jakiś bardzo długi blok kodu w warunku
  6. *
  7. /
  8. }
  9. else
  10. {
  11. /*
  12. * Tutaj coś krótszego z returnem - u Ciebie zaledwie 3 linijki !!!
  13. */
  14. }

to używajmy tego odwrotnie, czyli
  1. if(FALSE)
  2. {
  3. // tutaj te 3 linijki z RETURNEM
  4. }
  5. // TUTAJ RESZTA KODU


Mała rzecz a znacząco podnosi czytelność kodu.
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.