<?php
/* vim: set number autoindent tabstop=4 shiftwidth=4 softtabstop=4: */
/**
 * Example of usage for PEAR class HTML_QuickForm_LiveText
 *
 * @author     Fabio Ambrosanio <fabio@ambrosanio.com>
 * @version    0.1.0
 * @package    HTML_QuickForm_LiveText
 *
 * $Id: sample.php,v 1.3 2007/04/12 06:42:21 fabamb Exp $
 */
require_once 'HTML/QuickForm.php';
require_once 
'HTML/QuickForm/livetext.php';

$twoLabel = <<<_HTML
<tr valign="top">
    <td align="right">
        <!-- BEGIN required --><span style="color: #F00;">*</span><!-- END required --><b>
{label}</b>
    </td>
    <td align="left">
        <!-- BEGIN error --><span style="color: #F00;">
{error}</span><br /><!-- END error -->{element}
        <!-- BEGIN label_2 --><br /><span style="font-size: 80%;">
{label_2}</span><!-- END label_2 -->
    </td>
</tr>
_HTML;

$groupTemplate = <<<HTML
<table border=0 cellpadding=0 cellspacing=1>
    <tr>
        
{content}
    </tr>
</table>
HTML;

$groupElementTemplate = <<<HTML
<td valign="top">
    
{element}<br />
    <span class="minilabel">
{label}<!-- BEGIN required --><span style="color: #f00">* </span><!-- END required --></span>
</td>
HTML;

$form = new HTML_QuickForm('form');

$form->addElement('livetext''live1', array('Simple search:<br/>(type "a")''Search for a fruit:'), array(
    
'minSearch' => 1
));
unset(
$group);
$group[] =& $form->createElement('livetext''live2''name', array(
    
'method' => 'search2',
    
'minSearch' => 2,
    
'map' => array('live2' => 'name''id2' => 'id'),
    
'keys' => array('id''name')
));
$group[] =& $form->createElement('text''id2''id', array(
    
'size' => 10,
    
'id' => 'id2',
    
'readonly' => 'true',
    
'style' => 'background-color: transparent !important; border: 1px solid #ccc;'
));
$form->addGroup($group'group''Search for a name:<br/>(min 2 chars, type "jo")''');

unset(
$group);
$group[] =& $form->createElement('text''id3''id', array(
    
'size' => 5,
    
'id' => 'id3',
    
'readonly' => 'true',
    
'style' => 'background-color: transparent !important; border: 1px solid #ccc;'
));
$group[] =& $form->createElement('livetext''live3''name', array(
    
'method' => 'search2',
    
'minSearch' => 2,
    
'showAsTable' => true,
    
'map' => array(
       
'live3' => 'name',
       
'id3' => 'id',
       
'department3' => 'department'
    
)
));
$group[] =& $form->createElement('text''department3''department', array(
    
'size' => 20,
    
'id' => 'department3',
    
'readonly' => 'true',
    
'style' => 'background-color: transparent !important; border: 1px solid #ccc;'
));
$form->addGroup($group'group2''Search for a name again with table rendering:<br/>(min 2 chars, type "jo")''');

$form->addElement('livetext''live4', array('Simple search from a 2nd server:<br/>(like the first but from a different server page)''Search for a fruit'), array(
    
'minSearch' => 1,
    
'server' => 'auto_server2.php',
    
'stub' => 'LiveText2'
));


$form->addElement('text''required''For error check:');
$form->addRule('required''Field required.''required');
$form->addElement('submit'null'Submit');

$renderer =& $form->defaultRenderer();
$renderer->setElementTemplate($twoLabel'live1');
$renderer->setElementTemplate($twoLabel'live4');
$renderer->setGroupTemplate($groupTemplate'group');
$renderer->setGroupElementTemplate($groupElementTemplate'group');
$renderer->setGroupTemplate($groupTemplate'group2');
$renderer->setGroupElementTemplate($groupElementTemplate'group2');

// Tries to validate the form
if ($form->validate()) {
    
// Form is validated, then processes the data
    
$form->freeze();
    
$form->process('myProcess'false);
    echo 
"\n<HR>\n";
}

/**
 * Process callback
 * @ignore
 */
function myProcess($values)
{
    echo 
'<pre>';
    
var_dump($values);
    echo 
'</pre>';
}

$form->display();
?>