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<?php
// Load the controller
require_once 'HTML/QuickForm/Controller.php';
// Load the base Action class (we will subclass it later)
require_once 'HTML/QuickForm/Action.php';
// Class representing a form
class FirstPage extends HTML_QuickForm_Page
{
function buildForm()
{
$this->_formBuilt = true;
// Add some elements to the form
$this->addElement('header', null, 'QuickForm tutorial example');
$this->addElement('text', 'name', 'Enter your name:', array('size' => 50, 'maxlength' => 255
)); // Note how we set the name of the submit button
$this->addElement('submit', $this->getButtonName('submit'), 'Send');
// Define filters and validation rules
$this->applyFilter('name', 'trim');
$this->addRule('name', 'Please enter your name', 'required', null, 'client');
$this->setDefaultAction('submit');
}
}
// Action to process the form
class ActionProcess extends HTML_QuickForm_Action
{
function perform(&$page, $actionName)
{
}
}
$page =& new FirstPage('firstForm');
// We only add the 'process' handler, Controller will care for default ones
$page->addAction('process', new ActionProcess());
// Instantiate the Controller
$controller =& new HTML_QuickForm_Controller('tutorial');
// Set defaults for the form elements
$controller->setDefaults(array( 'name' => 'Joe User'
));
// Add the page to Controller
$controller->addPage($page);
// Process the request
$controller->run();
?>
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:
<?php
require_once 'HTML/QuickForm.php';
require_once 'HTML/Page2.php';
require_once 'HTML/Table';
$obj_Page = new HTML_Page2();
$obj_Page->setTitle( 'MOJA STRONA' );
$colA = '<b>HTML czyli jakaś zawartość</b>';
$form = new HTML_QuickForm('firstForm');
$form->addElement('header', null, 'My form');
$form->addElement('text', 'name', 'Enter your name:', array('size' => 50, 'maxlength' => 255
)); $form->addElement('submit', null, 'Send');
// Zawartość formularza zapisana do html'a, w odróżnieniu do użycia display() wyświetlona na ekranie
$colB = $form->toHtml();
$table = new HTML_Table();
$table->addCol($colA, array('style'=>'width: 250px') ); $table->addCol($colB, array('style'=>'vertical-align: top;'));
$obj_Page->addBodyContent( $table->toHtml() );
$obj_Page->display();
?>
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.