Fajne są formularze HTML_QuickForm, ale same formularze to za mało, przydało by się móc je wygodnie generować i obsługiwać (oddzielnymi obiektami, bez mieszania formularza i operacji z nim związanych).

W repozytorium PEAR jest ciekawy obiekt, który to umożliwia czyli HTML_QuickForm_Controller

Oto przykład 47-1 z manuala


  1. <?php
  2. // Load the controller
  3. require_once 'HTML/QuickForm/Controller.php';
  4. // Load the base Action class (we will subclass it later)
  5. require_once 'HTML/QuickForm/Action.php';
  6.  
  7. // Class representing a form
  8. class FirstPage extends HTML_QuickForm_Page
  9. {
  10. function buildForm()
  11. {
  12. $this->_formBuilt = true;
  13.  
  14. // Add some elements to the form
  15. $this->addElement('header', null, 'QuickForm tutorial example');
  16. $this->addElement('text', 'name', 'Enter your name:', array('size' => 50, 'maxlength' => 255));
  17. // Note how we set the name of the submit button
  18. $this->addElement('submit', $this->getButtonName('submit'), 'Send');
  19.  
  20. // Define filters and validation rules
  21. $this->applyFilter('name', 'trim');
  22. $this->addRule('name', 'Please enter your name', 'required', null, 'client');
  23.  
  24. $this->setDefaultAction('submit');
  25. }
  26. }
  27.  
  28. // Action to process the form
  29. class ActionProcess extends HTML_QuickForm_Action
  30. {
  31. function perform(&$page, $actionName)
  32. {
  33. echo '<h1>Hello, ' . htmlspecialchars($page->exportValue('name')) . '!</h1>';
  34. }
  35. }
  36.  
  37. $page =& new FirstPage('firstForm');
  38.  
  39. // We only add the 'process' handler, Controller will care for default ones
  40. $page->addAction('process', new ActionProcess());
  41.  
  42. // Instantiate the Controller
  43. $controller =& new HTML_QuickForm_Controller('tutorial');
  44.  
  45. // Set defaults for the form elements
  46. $controller->setDefaults(array(
  47. 'name' => 'Joe User'
  48. ));
  49.  
  50. // Add the page to Controller
  51. $controller->addPage($page);
  52.  
  53. // Process the request
  54. $controller->run();
  55. ?>



Fajna sprawa, może naprawdę ułatwić utrzymywanie porządku w klasach dotyczących formularzy i operacji na ich danych...
toHtml() wysyłająca wynik zamiast na ekran to do zmiennej.
Taka metoda istnieje w tradycyjnym formularzu HTML_QuickForm i można jej użyć jeśli buduje się całą stronę za pomocą obiektu Page i HTML_Table, czyli:

  1. <?php
  2. require_once 'HTML/QuickForm.php';
  3. require_once 'HTML/Page2.php';
  4. require_once 'HTML/Table';
  5.  
  6.  
  7. $obj_Page = new HTML_Page2();
  8. $obj_Page->setTitle( 'MOJA STRONA' );
  9.  
  10. $colA = '<b>HTML czyli jakaś zawartość</b>';
  11.  
  12.  
  13. $form = new HTML_QuickForm('firstForm');
  14. $form->addElement('header', null, 'My form');
  15. $form->addElement('text', 'name', 'Enter your name:', array('size' => 50, 'maxlength' => 255));
  16. $form->addElement('submit', null, 'Send');
  17.  
  18.  
  19. // Zawartość formularza zapisana do html'a, w odróżnieniu do użycia display() wyświetlona na ekranie
  20.  
  21. $colB = $form->toHtml();
  22.  
  23.  
  24.  
  25.  
  26. $table = new HTML_Table();
  27. $table->addCol($colA, array('style'=>'width: 250px') );
  28. $table->addCol($colB, array('style'=>'vertical-align: top;'));
  29.  
  30. $obj_Page->addBodyContent( $table->toHtml() );
  31.  
  32. $obj_Page->display();
  33.  
  34. ?>



Czy ktoś wie jak połączyć kontrolery formularza z pierwszego przykładu ze sposobem wyświetlania stron przez budownie kodu przez <b>toHtml()</b> ?

Niestety nie ma metody $controller->toHtml(), która byłaby idealnym rozwiązaniem problemu.