Diff for Doc/Dev/FormPackage between 10 and 11

+ Horde_Form



[[toc]]



++ What form input types are there?



See ((Doc/Dev/FormTypes)).



++ 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">

<?php



/* Require and set up Horde_Form. */

require_once 'Horde/Form.php';

require_once 'Horde/Form/Renderer.php';

require_once 'Horde/Variables.php';



/* Get theSet up this data to be inserted, obviously with keys correspondingas a new Horde_Form_Vars object. */

   to the available fields in the form. */$vars = Variables::getDefaultVariables();

$somedata = array('example_foo' => 'foo value',if (!$vars->exists('example_foo')) {

                  'example_bar' => 'bar value');$vars->set('example_foo', 'Foo Sample Data');



/* Set up this data as a new Horde_Form_Vars object. */

$vars = new Variables($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 = new Horde_Form($vars, _("An Example Form"), 'some_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())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.form. */

$myform->renderActive(new Horde_Form_Renderer(), $vars, 'edit.php', 'post');

</code>



----



++ Using Horde_Form_Action



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



<code type="php">

<?php

/**

 * Example for the conditional_enable action.

 */



@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');



$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));



/* Render the form. */

$form->renderActive(new Horde_Form_Renderer(), $vars, 'form.php', 'post');

</code>



----



<code type="php">

<?php



require_once 'Horde/Form.php';

require_once 'Horde/Variables.php';

require_once 'Horde/Form/Renderer.php';



$vars = Variables::getDefaultVariables();



/* Set up the form object. */

$form = 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');



$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:<br /><pre>';

    var_dump($info);

    echo '</pre>';

}



/* Render the form. */

$form->renderActive(new Horde_Form_Renderer(), $vars, 'simple_form.php', 'post');

</code>



----



<code type="php">

<?php



require_once 'Horde/Form.php';

require_once 'Horde/Form/Renderer.php';

require_once 'Horde/Variables.php';



class TestForm1 extends Horde_Form {



    var $_useFormToken = false;



    function TestForm1(&$vars)

    {

        parent::Horde_Form($vars, _("Step 1"));



        $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));

        $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 );

    }



}



$r = new Horde_Form_Renderer();

$vars = Variables::getDefaultVariables();



$form1 = new TestForm1($vars);

$form2 = new TestForm2($vars);



$form1->validate($vars);$form1Valid = $form1->validate($vars, true);

$form2->validate($vars);$form2Valid = $form2->validate($vars);



/* Check if one form is not valid. */

if (!$form1->isValid()(!$form1Valid || !$form2->isValid())!$form2Valid) {

    if ($form1->isValid())($form1Valid) {

        $form2->open($r, $vars, 'form.php', 'post');



        // preserve form1 variables

        $form1->preserve($vars);

        $r->beginInactive($form1->getTitle());

        $r->renderFormInactive($form1, $vars);

        $r->end();



        // now render the active form

        $r->beginActive($form2->getTitle);

        $r->renderFormActive($form2, $vars);

        $r->submit();

        $r->end();

        $form2->close($r);

    } else {

        $form1->open($r, $vars, 'form.php', 'post');



        $r->beginActive($form1->getTitle());

        $r->renderFormActive($form1, $vars);

        $r->submit();

        $r->end();



        $form1->close($r);

    }

} else {

    $form1->getInfo($vars, $info);

    $form2->getInfo($vars, $info);

    echo '<pre>';

    print_r($info);

    echo '</pre>';

}

</code>