|
|
(Next) QuickHelp |
||||
| |
|||||
|
|||||
The purpose of this tutorial is to give the new users of QuickForm an overview of its features and usage patterns. It describes a small subset of available functionality, but points to the parts of the documentation that give a more in-depth overview.
There also exists a somewhat bigger tutorial on QuickForm usage made by Keith Edmunds.
Basic QuickForm usage
<?php
// Load the main class
require_once 'HTML/QuickForm.php';
// Instantiate the HTML_QuickForm object
$form = new HTML_QuickForm('firstForm');
// Set defaults for the form elements
$form->setDefaults(array(
'name' => 'Joe User'
));
// Add some elements to the form
$form->addElement('header', null, 'QuickForm tutorial example');
$form->addElement('text', 'name', 'Enter your name:', array('size' => 50, 'maxlength' => 255));
$form->addElement('submit', null, 'Send');
// Define filters and validation rules
$form->applyFilter('name', 'trim');
$form->addRule('name', 'Please enter your name', 'required', null, 'client');
// Try to validate a form
if ($form->validate()) {
echo '<h1>Hello, ' . htmlspecialchars($form->exportValue('name')) . '!</h1>';
exit;
}
// Output the form
$form->display();
?>
Lets review this example step by step.
The line
<?php
$form = new HTML_QuickForm('firstForm');
?>
You might guess that
<?php
$form->setDefaults(array(
'name' => 'Joe User'
));
?>
Our form will consist of three elements:
<?php
$form->addElement('header', null, 'QuickForm tutorial example');
$form->addElement('text', 'name', 'Enter your name:', array('size' => 50, 'maxlength' => 255));
$form->addElement('submit', null, 'Send');
?>
The line
<?php
$form->applyFilter('name', 'trim');
?>
Next we define a rule for the name field:
<?php
$form->addRule('name', 'Please enter your name', 'required', null, 'client');
?>
We now have the form built and rules defined and need to decide whether to process it or display:
<?php
if ($form->validate()) {
// Do some stuff
}
?>
If the form is validated we need to process the values
<?php
echo '<h1>Hello, ' . htmlspecialchars($form->exportValue('name')) . '!</h1>';
exit;
?>
The last line is pretty easy:
<?php
$form->display();
?>
You now should have an understanding of basic QuickForm functionality, but there are many more features in the package, each of them deserving a separate tutorial. This section will give a short overview of them and point you to the complete documentation.
Groups allow to combine several individual elements into one entity and to use it as one element. This is also used to create pseudo-elements like date and hierselect.
QuickForm offers a lot of possibilities to customize form layout and appearance. Form output is done via renderers - special classes containing necessary logic. There are renderers that directly output HTML and those that use template engines for this.
And finally, you can extend QuickForm by adding your own element types, rules and renderers.
|
|
(Next) QuickHelp |
||||||||
| |
|||||||||
|
|||||||||