[cvs] [Wiki] changed: HordeForm

Chuck Hagenbuch chuck at horde.org
Wed Jun 1 23:23:24 PDT 2005


chuck  Wed, 01 Jun 2005 23:23:24 -0700

Modified page: http://wiki.horde.org/HordeForm
New Revision:  1.2

@@ -1,4 +1,61 @@
++ Horde_Form
+
+++ 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.
+
+<code type="php">
+/* Require and set up Horde_Form. */
+require_once HORDE_LIBS . 'Horde/Form.php';
+require_once HORDE_LIBS . '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');
+</code>
+
+----
+
 <code type="php">
 <?php
 require_once 'Horde/Form.php';
 require_once 'Horde/Variables.php';


More information about the cvs mailing list