[[toc]]
++ What form input types are there?
There are many different [HordeFormTypes form input types]. To many to list here, follow [HordeFormTypes this link].
++ Filling a form with values
The following code could be used to fill a form through Horde_Form, for example to edit a record. The example is a bit abstracted from reality, in that you would also need to build some validation around this code, checking for a form submission, etc. to eventually have a useable form.
/* Require and set up Horde_Form. */
require_once 'Horde/Form.php';
require_once 'Horde/Form/Renderer.php';
/* Get the data to be inserted, obviously with keys corresponding
to the available fields in the form. */
$somedata = array(.....);
/* Set up this data as a new Horde_Form_Vars object. */
$vars = &new Horde_Form_Vars($somedata);
/* Get formname var to check if submitted later on. */
$formname = $vars->getVar('formname');
/* Get the form object, setting the vars and the title. */
$myform = &Horde_Form::singleton('SomeForm', $vars, _("An Example Form"));
/* Set up the form fields. */
$myform->addHidden('', 'example_hidden', 'int', false);
$myform->addVariable(_("Foo field"), 'example_foo', 'text', true);
$myform->addVariable(_("Bar field"), 'example_bar', 'longtext', true, false, _("You have to fill in some long text here"), array(4, 40));
/* Check if form submitted and validate. */
if ($formname) {
$myform->validate($vars);
if ($myform->isValid()) {
$myform->getInfo($vars, $info);
/* Do something with this form data. */
$result = $someclass->saveMyFormData($info);
if (is_a($result, 'PEAR_Error')) {
$notification->push(sprintf(_("Could not create save data."), $result->message), 'horde.error');
} else {
$notification->push(_("Data saved."), 'horde.success');
$url = Horde::applicationUrl('somescript.php', true);
header('Location: ' . $url);
exit;
}
}
}
/* Render out the form and it should have the fields filled in
according to the $somedata array values. */
$renderer = &new Horde_Form_Renderer();
$myform->renderActive($renderer, $vars, 'edit.php', 'post');
++ Using Horde_Form_Action
Here's an example that uses the conditional_enable action to enable a text description if the user picks "Other".
'choices',
'enabled' => true,
'values' => array('other'));
$action = &Horde_Form_Action::singleton('conditional_enable', $params);
$choices = array('big' => 'BIG',
'small' => 'small',
'other' => 'Other');
$form->addVariable('Select something', 'choices', 'enum', true, false, '', array($choices, true));
$o = &$form->addVariable('If other, please describe', 'other_text', 'text', false, false);
$o->setAction($action);
/* Render the form. */
$renderer = &new Horde_Form_Renderer();
$form->renderActive($renderer, $vars, 'form.php', 'post');
----
setButtons('Submit this form', true);
$form->addVariable('Insert some text', 'some_text', 'text', false, false, 'Insert in this box some text');
$choices = array('big' => 'BIG',
'small' => 'small',
'mixed' => 'mIxED');
$form->addVariable('Select something', 'choices', 'enum', true, false, 'Use the selection box to make your choice', array($choices,
true));
if ($form->validate($vars)) {
$form->getInfo($vars, $info);
echo 'You have submitted:
';
var_dump($info);
echo '';
}
/* Render the form. */
require_once 'Horde/Form/Renderer.php';
$renderer = &new Horde_Form_Renderer();
$form->renderActive($renderer, $vars, 'simple_form.php', 'post');
----
_("Select:"),
1 => _("Yes"),
0 => _("No"));
/* addVariable( Visible Text, name, type, required, readonly,
* description, params) */
$this->addVariable(_("Enum"), 'opciones', 'enum', true, false, _("Simple description"), array($enum));
$this->addVariable(_("Boolean"), 'bool', 'boolean', false, false );
$this->addVariable(_("Integer"), 'number', 'int', true, false );
$this->addVariable(_("A Date"), 'mybday', 'date', false, false );
}
}
class TestForm2 extends Horde_Form {
var $_useFormToken = false;
function TestForm2(&$vars)
{
parent::Horde_Form($vars, _("Step 2"));
$this->addVariable(_("Email"), 'email_address', 'email', true, false );
}
}
$RENDERER = &new Horde_Form_Renderer();
$vars = &Variables::getDefaultVariables();
$form1 = &Horde_Form::singleton('TestForm1', $vars);
$form2 = &Horde_Form::singleton('TestForm2', $vars);
$form1->validate($vars);
$form2->validate($vars);
/* check if one form is not valid */
if (!$form1->isValid() || !$form2->isValid()) {
if ($form1->isValid()) {
$form2->open($RENDERER, $vars, 'form.php', 'post');
// preserve form1 variables
$form1->preserve($vars);
$RENDERER->beginInactive($form1->getTitle());
$RENDERER->renderFormInactive($form1, $vars);
$RENDERER->end();
// now render the active form
$RENDERER->beginActive($form2->getTitle);
$RENDERER->renderFormActive($form2, $vars);
$RENDERER->submit();
$RENDERER->end();
$form2->close($RENDERER);
} else {
$form1->open($RENDERER, $vars, 'form.php', 'post');
$RENDERER->beginActive($form1->getTitle());
$RENDERER->renderFormActive($form1, $vars);
$RENDERER->submit();
$RENDERER->end();
$form1->close($RENDERER);
}
} else {
$form1->getInfo($vars, $info);
$form2->getInfo($vars, $info);
echo "";
print_r($info);
echo "";
}