guest [80.153.12.208] Thu, 22 Sep 2016 14:03:07 +
Modified page: https://wiki.horde.org/Doc/Dev/FormPackage
New Revision: 14
Change log: updated examples, introduced page code as environment for
examples
@@ -8,44 +8,76 @@
++ 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.
+
+For using the below examples, consider Horde application "myapp" with
page "form.php":
+
+
+header(array(
+'title' => _("My Page"),
+));
+
+//
+// Example code goes here
+//
+
+$page_output->footer();
+
+
+
+
+
+Here are some examples for introducing features of Horde_Forms:
+
+
+
-/* Require and set up Horde_Form. */
+// Require and set up Horde_Form.
require_once 'Horde/Form.php';
require_once 'Horde/Form/Renderer.php';
require_once 'Horde/Variables.php';
-/* Set up this data as a new Horde_Form_Vars object. */
-$vars = Variables::getDefaultVariables();
+// Example 1: a simple form
+
+// Set up this data as a new Horde_Form_Vars object.
+$vars = Horde_Variables::getDefaultVariables();
if (!$vars->exists('example_foo')) {
$vars->set('example_foo', 'Foo Sample Data');
}
-/* Get formname var to check if submitted later on. */
+// Get formname var to check if submitted later on.
$formname = $vars->get('formname');
-/* Get the form object, setting the vars and the title. */
+// Get the form object, setting the vars and the title.
$myform = new Horde_Form($vars, _("An Example Form"), 'some_form');
-/* Set up the form fields. */
+// 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));
+$myform->addVariable(_("Make editable"), 'example_edit',
'monthdayyear', false);
+$myform->setButtons('Abschicken', true);
-/* Check if form submitted and validate. */
+// Check if form submitted and validate.
if ($formname) {
if ($myform->validate($vars)) {
$myform->getInfo($vars, $info);
-/* Do something with this form data. */
+// Do something with this form data.
echo 'saving data';
}
}
-/* Render out the form. */
-$myform->renderActive(new Horde_Form_Renderer(), $vars, 'edit.php', 'post');
+// Render out the form.
+$myform->renderActive(new Horde_Form_Renderer(), $vars, 'form.php', 'post');
+
@@ -53,92 +85,79 @@
Here's an example that uses the conditional_enable action to enable
a text description if the user picks "Other".
-/**
- * Example for the conditional_enable action.
- */
+// Example 2: conditional enable
-@define('HORDE_BASE', dirname(__FILE__));
-require_once HORDE_BASE . '/lib/core.php';
-require_once 'Horde/Form.php';
-require_once 'Horde/Form/Action.php';
-require_once 'Horde/Form/Renderer.php';
-require_once 'Horde/Variables.php';
-
-$registry = &Registry::singleton();
-
-$vars = Variables::getDefaultVariables();
-$form = new Horde_Form($vars, 'Using conditional enable');
+$vars = Horde_Variables::getDefaultVariables();
+$myform = new Horde_Form($vars, 'Using conditional enable');
$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);
-$params = array('target' => 'choices',
-'enabled' => true,
-'values' => array('other'));
-$o->setAction(Horde_Form_Action::factory('conditional_enable', $params));
+$myform->addVariable('Select something', 'choices', 'enum', true,
false, '', array($choices, true));
+$o = &$myform->addVariable('If other, please describe', 'other_text',
'text', false, false);
-/* Render the form. */
-$form->renderActive(new Horde_Form_Renderer(), $vars, 'form.php', 'post');
+// $o is a Horde_Form_Variable
+$params = array('target' => 'choices',// variable name on
which the enable state depends
+'values' => array('other'), // values of target on
which to change state
+'enabled' => true);// state if variable
contains one of the values
+// assign an action to the variable
+$o->setAction(Horde_Form_Action::factory('ConditionalEnable', $params));
+// action defined through Horde_Form_Action_ class
+
+// Render out the form.
+$myform->renderActive(new Horde_Form_Renderer(), $vars, 'form.php', 'post');
-
-require_once 'Horde/Form.php';
-require_once 'Horde/Variables.php';
-require_once 'Horde/Form/Renderer.php';
-
-$vars = Variables::getDefaultVariables();
+
+/