Skip to content
Everett Griffiths edited this page May 20, 2014 · 2 revisions

Dropdown

A dropdown implements a <select> field and allows you to select a single value.

Syntax: dropdown(string $name, array $options=array(), string $default='', array $args=array(), string $tpl=null)

Simple Options

Simple options can be supplied via a simple array:

<?php
print \Formbuilder\Form::dropdown('mydropdown',array('Yes','No','Maybe'));
?>

If you require distinct options/values, then use an associative array:

<?php
print \Formbuilder\Form::dropdown('mydropdown',array('y'=>'Yes','n'=>'No','m'=>'Maybe'));
?>

When using an associative array, the array key is what is passed as the field value and the array value is used as the option label. E.g. in the above example, print $_POST['mydropdown'] would print "y" if "Yes" had been selected.

Example: Creating a range

Use the range function to generate numbers for you, e.g. 1 to 100 in increments of 5:

<?php
print \Formbuilder\Form::dropdown('mydropdown',range(1,100,5));
?>

Example: Option Groups

By supplying a nested array as your options, you can generate option groups:

<?php
$options = array(
    'Birds' => array(
        'bluebird'  => 'Sad Bluebird',
        'crow'      => 'Black Crow',
    ),
    'Mammals' => array(
        'cow'   => 'Mute Cow',
        'dog'   => 'Good Dog',
    )
    'Reptiles' => array(
        'croc'  => 'Crocodile',
        'turtle' => 'Slow Turtle',
    )
);
print \Formbuilder\Form::dropdown('mydropdown',$options);
?>