PHP Extension & Application RepositoryPyrus - A few PEAR Compatible Packages

HTML_QuickForm_altselect

This package is now a part of PEAR

A QuickForm element that extends the select element and turns its options into checkboxes or radio buttons depending on whether the multiple html attribute was set or not. For extra options not listed, you can also render an 'Other' textfield.

Each of these radio buttons could've easily been created individually and then grouped together, however it integrates well into the business logic layer, is easily scalable and when creating quite a few form elements of this format I find that it saves me a lot of time.

Please note that altselect.php must be included after QuickForm.php.

Regular Usage

require_once 'HTML/QuickForm.php';
require_once
'HTML/QuickForm/altselect.php';

$form =& new HTML_QuickForm;
$element =& $form->addElement('altselect','a_select','Which Option?',
                              array(
'a' => 'A', 'b' => 'B', 'c' => 'C'));
$element->setSelected('b');
$form->display();

Which Option?

Multiple

require_once 'HTML/QuickForm.php';
require_once
'HTML/QuickForm/altselect.php';

$form =& new HTML_QuickForm;
$element =& $form->addElement('altselect','a_select','Which Option?',
                              array(
'a' => 'A', 'b' => 'B', 'c' => 'C'));
$element->setMultiple(true);
$element->setSelected(array('b','c'));
$form->display();

Which Option?

Other Option

require_once 'HTML/QuickForm.php';
require_once
'HTML/QuickForm/altselect.php';

$form =& new HTML_QuickForm;
$element =& $form->addElement('altselect','a_select','Which Option?',
                              array(
'a' => 'A', 'b' => 'B', 'c' => 'C'));
$element->setIncludeOther(true);
$element->setSelected('Something else!');
$form->display();

Which Option?



Other with Multiple

require_once 'HTML/QuickForm.php';
require_once
'HTML/QuickForm/altselect.php';

$form =& new HTML_QuickForm;
$element =& $form->addElement('altselect','a_select','Which Option?',
                              array(
'a' => 'A', 'b' => 'B', 'c' => 'C'));
$element->setMultiple(true);
$element->setIncludeOther(true);
$element->setSelected(array('a','x'));
$form->display();

Which Option?


Applying HTML Attributes

HTML attributes are applied to the option elements via the final argument to altselect with an associative array of option names to either HTML attribute strings or another array of attributes (the same for which can be passed into HTML_Common::updateAttributes()). In addition, there are 3 special keys:

  • _qf_other - The 'other' radio button
  • _qf_other_text - The 'other' text field
  • _qf_all - Apply the attributes to all the option elements

require_once 'HTML/QuickForm.php';
require_once 
'HTML/QuickForm/altselect.php';

$form =& new HTML_QuickForm;
$form->addElement('altselect','a_select','Which Option?',
                  array(
'a' => 'A''b' => 'B''c' => 'C'),
                  array(
'b' => array('disabled' => 'disabled')));
$form->display();

Which Option?

require_once 'HTML/QuickForm.php';
require_once 
'HTML/QuickForm/altselect.php';

$form =& new HTML_QuickForm;
$form->addElement('altselect','a_select','Which Option?',
                  array(
'a' => 'A''b' => 'B''c' => 'C'),
                  array(
'_qf_all' => array('disabled' => 'disabled')));
$form->display();

Which Option?

Exclusive Options with Multiple

Here's an example of using javascript to provide an exclusive option amongst checkboxes. One might want to do this to force the user to think about the question and answer with a 'None' option, rather than just leaving the options blank. (Side note: the for...in didn't seem to work with Safari and the checkbox collection)

<?php

require_once '../config.php';

require_once 
'HTML/QuickForm.php';
require_once 
'HTML/QuickForm/altselect.php';

?>
<script type="text/javascript">
function exclusiveOption(source)
{
    // ignore if unchecking the source
    if (!source.checked) {
        return;
    }

    var a = source.form.elements[source.name];

    if (source.value != 'none') {
        for (var i = 0; i < a.length; i++) {
            if (a[i].value == 'none') {
                a[i].checked = false;
                return;
            }
        }
    } else {
        for (var i = 0; i < a.length; i++) {
            if (a[i].value != 'none') {
                a[i].checked = false;
            }
        }
    }
}
</script>
<?php

$form 
= new HTML_QuickForm('test');
$e $form->addElement('altselect''test''Test',
        array(
'none' => 'None''a' => 'A''b' => 'B''c' => 'C'),
        array(
'_qf_all' => array('onclick' => 'exclusiveOption(this)'))
        );
$e->setMultiple(true);
$form->display();

?>

Test


This can be further extended so that unchecking none or any of the options is handled. Unchecking none would revert back to a previous selection, whereas unchecking all the options would check result in the none option being checked.

<?php

require_once '../config.php';

require_once 
'HTML/QuickForm.php';
require_once 
'HTML/QuickForm/altselect.php';

?>
<script type="text/javascript">
var last_state = new Object;
function exclusiveOption2(source)
{
    var a = source.form.elements[source.name];

    // unchecking
    // just return here if you don't want to restore after this action
    if (!source.checked) {

        if (source.value == 'none') {
            // restore
            for (var i = 0; i < a.length; i++) {
                if (a[i].value != 'none' && last_state[a[i].value] != null) {
                    a[i].checked = last_state[a[i].value];
                }
            }
            return;
        } else {
            // check if last one unchecked
            var all_unchecked = true;
            for (var i = 0; i < a.length; i++) {
                if (a[i].value != 'none' && a[i].checked) {
                    all_unchecked = false;
                }
            }
            // then check none
            if (all_unchecked) {
                last_state = new Object;
                last_state[source.value] = true;
                for (var i = 0; i < a.length; i++) {
                    if (a[i].value == 'none') {
                        a[i].checked = true;
                        return;
                    }
                }
            }
        }
    }

    if (source.value == 'none') {
        for (var i = 0; i < a.length; i++) {
            if (a[i].value != 'none') {
                last_state[a[i].value] = a[i].checked;
                a[i].checked = false;
            }
        }
    } else {
        for (var i = 0; i < a.length; i++) {
            if (a[i].value == 'none') {
                a[i].checked = false;
                return;
            }
        }
    }
}
</script>
<?php

$form 
= new HTML_QuickForm('test');
$e $form->addElement('altselect''test''Test',
        array(
'none' => 'None''a' => 'A''b' => 'B''c' => 'C'),
        array(
'_qf_all' => array('onclick' => 'exclusiveOption2(this)'))
        );
$e->setMultiple(true);
$form->display();

?>

Test


 

 
SourceForge.net Logo