[cvs] [Wiki] changed: Doc/Dev/FormPackage
Chuck Hagenbuch
chuck at horde.org
Sun Apr 16 21:55:39 PDT 2006
chuck Sun, 16 Apr 2006 21:55:39 -0700
Modified page: http://wiki.horde.org/Doc/Dev/FormPackage
New Revision: 1.10
Change log: This + HEAD properly installed framework == working examples
@@ -10,20 +10,20 @@
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">
+<?php
+
/* Require and set up Horde_Form. */
require_once 'Horde/Form.php';
require_once 'Horde/Form/Renderer.php';
require_once 'Horde/Variables.php';
-
-/* Get the data to be inserted, obviously with keys corresponding
- to the available fields in the form. */
-$somedata = array('example_foo' => 'foo value',
- 'example_bar' => 'bar value');
/* Set up this data as a new Horde_Form_Vars object. */
-$vars = new Variables($somedata);
+$vars = Variables::getDefaultVariables();
+if (!$vars->exists('example_foo')) {
+ $vars->set('example_foo', 'Foo Sample Data');
+}
/* Get formname var to check if submitted later on. */
$formname = $vars->getVar('formname');
@@ -36,28 +36,17 @@
$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()) {
+ if ($myform->validate($vars)) {
$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;
- }
+ echo 'saving data<br />';
}
}
-/* Render out the form and it should have the fields filled in
- according to the $somedata array values. */
+/* Render out the form. */
$myform->renderActive(new Horde_Form_Renderer(), $vars, 'edit.php', 'post');
</code>
----
@@ -101,8 +90,9 @@
----
<code type="php">
<?php
+
require_once 'Horde/Form.php';
require_once 'Horde/Variables.php';
require_once 'Horde/Form/Renderer.php';
@@ -180,14 +170,14 @@
$form1 = new TestForm1($vars);
$form2 = new TestForm2($vars);
-$form1->validate($vars);
-$form2->validate($vars);
+$form1Valid = $form1->validate($vars, true);
+$form2Valid = $form2->validate($vars);
/* Check if one form is not valid. */
-if (!$form1->isValid() || !$form2->isValid()) {
- if ($form1->isValid()) {
+if (!$form1Valid || !$form2Valid) {
+ if ($form1Valid) {
$form2->open($r, $vars, 'form.php', 'post');
// preserve form1 variables
$form1->preserve($vars);
More information about the cvs
mailing list