guest [80.153.12.208]  Thu, 22 Sep 2016 14:03:07 +0000

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":
+
+<code>
+<?php
+require_once __DIR__ . '/lib/Application.php';
+Horde_Registry::appInit('myapp');
+
+
+$page_output->header(array(
+    'title' => _("My Page"),
+));
+
+//
+// Example code goes here
+//
+
+$page_output->footer();
+
+</code>
+
+----
+
+Here are some examples for introducing features of Horde_Forms:
+
+----
+

 <code type="php">
-/* 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<br />';
     }
 }

-/* 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');
+
 </code>

 ----

@@ -53,92 +85,79 @@

Here's an example that uses the conditional_enable action to enable a text description if the user picks "Other".

 <code type="php">
-/**
- * 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_<action> class
+
+// Render out the form.
+$myform->renderActive(new Horde_Form_Renderer(), $vars, 'form.php', 'post');
 </code>

 ----

-<code type="php">
-require_once 'Horde/Form.php';
-require_once 'Horde/Variables.php';
-require_once 'Horde/Form/Renderer.php';
-
-$vars = Variables::getDefaultVariables();
+<code>
+// Example 3: validating parameters

-/* Set up the form object. */
-$form = new Horde_Form($vars, 'A simple form');
+$vars = Horde_Variables::getDefaultVariables();
+$myform = new Horde_Form($vars, 'A simple form');

-$form->setButtons('Submit this form', true);
-$form->addVariable('Insert some text', 'some_text', 'text', false, false, 'Insert in this box some text');
+$myform->setButtons('Submit this form', true);
+$myform->addVariable('Insert some text', 'some_text', 'text', true, 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));
+$myform->addVariable('Select something', 'choices', 'enum', false, false, 'Use the selection box to make your choice', array($choices, true));

-if ($form->validate($vars)) {
-    $form->getInfo($vars, $info);
+if ($myform->validate($vars)) {
+    $myform->getInfo($vars, $info);
     echo 'You have submitted:<br /><pre>';
     var_dump($info);
     echo '</pre>';
 }

-/* Render the form. */
-$form->renderActive(new Horde_Form_Renderer(), $vars, 'simple_form.php', 'post');
+// Render out the form.
+$myform->renderActive(new Horde_Form_Renderer(), $vars, 'form.php', 'post');
+
 </code>

 ----

 <code type="php">
-require_once 'Horde/Form.php';
-require_once 'Horde/Form/Renderer.php';
-require_once 'Horde/Variables.php';
+// Example 4: a chain of forms

 class TestForm1 extends Horde_Form {

     var $_useFormToken = false;

     function TestForm1(&$vars)
     {
-        parent::Horde_Form($vars, _("Step 1"));
-
+        parent::__construct($vars, _("Step 1")); // _() for gettext()
+
         $enum = array('' => _("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));
+        // addVariable( Visible Text, name, type, required, readonly,
+        // description, params)
+ $this->addVariable(_("Enum"), 'opciones', 'enum', false, 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 );
     }
@@ -150,37 +169,39 @@
     var $_useFormToken = false;

     function TestForm2(&$vars)
     {
-        parent::Horde_Form($vars, _("Step 2"));
+        parent::__construct($vars, _("Step 2"));

$this->addVariable(_("Email"), 'email_address', 'email', true, false );
     }

 }

 $r = new Horde_Form_Renderer();
-$vars = Variables::getDefaultVariables();
+$vars = Horde_Variables::getDefaultVariables();

 $form1 = new TestForm1($vars);
 $form2 = new TestForm2($vars);

-$form1Valid = $form1->validate($vars, true);
+$form1Valid = $form1->validate($vars, true); // Form1 can be autofilled: highlight incorrect fields right away
 $form2Valid = $form2->validate($vars);

-/* Check if one form is not valid. */
+// Check if one form is not valid.
 if (!$form1Valid || !$form2Valid) {
     if ($form1Valid) {
         $form2->open($r, $vars, 'form.php', 'post');

-        // preserve form1 variables
-        $form1->preserve($vars);
+        // show fields of Form1 as inactive
         $r->beginInactive($form1->getTitle());
         $r->renderFormInactive($form1, $vars);
         $r->end();

-        // now render the active form
-        $r->beginActive($form2->getTitle);
+        // include TestForm1 variables as invisible form elements
+        $form1->preserve($vars);
+
+        // render TestForm2 as active
+        $r->beginActive($form2->getTitle());
         $r->renderFormActive($form2, $vars);
         $r->submit();
         $r->end();
         $form2->close($r);

--
commits mailing list
Frequently Asked Questions: http://wiki.horde.org/FAQ
To unsubscribe, mail: commits-unsubscr...@lists.horde.org

Reply via email to