Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [ZF][ZendFramework] Pole select waliduje się niepoprawnie
Vill
post
Post #1





Grupa: Zarejestrowani
Postów: 62
Pomógł: 0
Dołączył: 22.05.2009

Ostrzeżenie: (0%)
-----


Mam pole select, które zawiera wartość nullową i kilku klientów do wyboru. Walidacja przechodzi poprawnie tylko w przypadku wyboru wartości null, w przypadku wyboru jakiegoś klienta to nie przechodzi. Nie mam ustawionych na tym polu żadnych walidatorów ani filtrów.

Polę buduję w ten sposób:

Tworzę pole w klasie dziedziczącej po Zend_Form budującej formularz
  1. $client_login = $this->createElement('select', 'client', array('label' => 'Wybierz klienta:'));


Potem uzupełniam w odpowiednim kontrolerze pobierającym wcześniej dane z bazy (formularz w zmiennej $this->createForm)
  1. $stmt = $this->db->query($select);
  2. $result = $stmt->fetchAll();
  3.  
  4. $clientSelect = $this->createForm->getElement('client');
  5.  
  6. $clientSelect->addMultiOption(null, 'Brak klienta');
  7.  
  8. foreach($result as $c) {
  9. $clientSelect->addMultiOption($c['ClientLogin'], $c['ClientLogin']);
  10. }


No i przekazuję formularz do widoku.

Gdzie może leżeć błąd? Dlaczego walidacja nie przepuszcza wszystkich wartości?

Ten post edytował Vill 14.12.2014, 14:44:24
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Vill
post
Post #2





Grupa: Zarejestrowani
Postów: 62
Pomógł: 0
Dołączył: 22.05.2009

Ostrzeżenie: (0%)
-----


Klasa z formularzem

  1. class Application_Form_CreateEvent extends Zend_Form {
  2.  
  3. public function init() {
  4.  
  5. $this->setMethod('post');
  6.  
  7. $this->setAttrib('id', 'create-event-form');
  8. $this->setAttrib('class', 'form');
  9.  
  10. //elementy
  11. $client_login = $this->createElement('select', 'client', array('label' => 'Wybierz klienta:'));
  12. $title = $this->createElement('text', 'title', array('label' => 'Tytuł:'));
  13. $desc = $this->createElement('textarea', 'desc', array('label' => 'Opis:'));
  14. $date = $this->createElement('text', 'date', array('label' => 'Data:'));
  15. $time = $this->createElement('text', 'time', array('label' => 'Godzina:'));
  16.  
  17. $login_input = $this->createElement('hidden', 'login_input');
  18. $submit = $this->createElement('submit', 'submit', array('value' => 'Wyślij'));
  19.  
  20. //walidatory i filtry
  21. $title->addValidator('stringLength', false, array(2, 255));
  22. ....
  23.  
  24. //złożenie formularza
  25. $this->addElement($client_login)
  26. ->addElement($title)
  27. ->addElement($date)
  28. ->addElement($time)
  29. ->addElement($desc)
  30. ->addElement($login_input)
  31. ->addElement($submit)
  32. ->addElement('hash', 'csrf', array('ignore' => true));
  33. }
  34. }


Akcja kontrolera uzupełniająca ten formularz o dodatkowe dane:
  1.  
  2. public function createAction() {
  3.  
  4. //ACL
  5. $hasAccess = $this->_helper->aclHelper($this->_request->getControllerName(), $this->getRequest()->getActionName());
  6.  
  7. if(!$hasAccess['access']){
  8. $this->view->message = $this->_helper->messageHelper('FORBIDDEN','RED');
  9. $this->view->redirectPath = '/trainer/login';
  10. return;
  11. }
  12.  
  13. //login trenera
  14. $login = $hasAccess['login'];
  15.  
  16. $this->createForm->setAction('/individevent/createform');
  17.  
  18. //uzupełnienie formularza o login trenera
  19. $loginInput = $this->createForm->getElement('login_input');
  20. $loginInput->setValue($login);
  21.  
  22. //uzupełnienie pola select o klientów
  23. $select = $this->db->select()
  24. ->from('trainer_client', 'ClientLogin')
  25. ->where('TrainerLogin = ?', $login);
  26.  
  27. $stmt = $this->db->query($select);
  28. $result = $stmt->fetchAll();
  29.  
  30. $clientSelect = $this->createForm->getElement('client');
  31.  
  32. $clientSelect->addMultiOption(null, 'Brak klienta');
  33. foreach($result as $c) {
  34. $clientSelect->addMultiOption($c['ClientLogin'], $c['ClientLogin']);
  35. }
  36.  
  37. //przekazanie formularza do widoku
  38. $this->view->createForm = $this->createForm;
  39. }


Akcja kontrolera przetwarzająca dane z formularza:

  1. public function createformAction() {
  2.  
  3. //sprawdzenie, czy nastąpiło przesłanie danych
  4. if(!$this->_request->isPost()) {
  5. $this->view->message = $this->_helper->messageHelper('POSTFAIL','RED');
  6. $this->view->redirectPath = '/';
  7. return;
  8. }
  9.  
  10. //walidacja
  11. if (!$this->createForm->isValid($this->_request->getPost())) {
  12. echo 'client: '.$this->_request->getPost('client').'</br>'; //wypisuje się prawidłowo wybrany klient
  13. $this->view->message = $this->_helper->messageHelper('VALIDATIONFAIL','RED');
  14. $this->view->redirectPath = '/';
  15. return;
  16. }
  17.  
  18. //przedworzenie danych
  19. //.......
  20.  
  21. }


Ten post edytował Vill 14.12.2014, 16:34:32
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 12.10.2025 - 11:29