Using the CakePHP selectTag HtmlHelper
The use of the selectTag HtmlHelper seems to be confusing to a lot of people (judging from the popularity of questions about it in the CakePHP Google Group). Hopefully I can clear up a few things about it.
Parameters
selectTag ($fieldName, $optionElements, $selected=null, $selectAttr=array(), $optionAttr=null, $showEmpty=true, $return=false)
$fieldName will typically be the name of your model in the singular slash (’/') the primary key: 'Post/post_id'.
$optionElements is an array of ‘value’ =>’text’ pairs. The generateList method is very useful for creating this array.
$selected defaults to null and can be used to set the selected option.
$selectAttr is an array. It is used to define HTML attributes for the opening select element: array('onclick' => 'myfunction();', 'class' => 'cssclassname')
$optionAttr serves the same purpose that $selectAttr does, except it applies to the option elements.
$showEmpty is used to set whether or not the list has an empty option at the top. The default is true. If changed to false, the first option element will be the first value/text pair in your $optionElements array.
$return default is false and it defines whether or not this method should return a value. I don’t know of any reason why this would ever be set to true. If someone knows of a scenario where this is useful, please share!
Application
Like other HtmlHelpers, the selectTag can be used in a View within the CakePHP MVC framework including Layouts and Helpers. Typically you would load some data to an array in a 'value' =>'text' pair arrangement before calling the selectTag method (example 1). However, the array can be built “on the fly” as well (example 2).
Example 1
<?php $agentArray = $this->requestAction('/agents/select'); ?>
<?=$html->selectTag('Agent/agent_id', $agentArray, null, array(), null, true, false);?>
Example 2
<?=$html->selectTag('Listing/price',
array(
'0' => 'no min',
'50000' => '50,000',
'100000' => '100,000',
'125000' => '125,000',
'150000' => '150,000',
'175000' => '175,000',
'200000' => '200,000'), '0', array(), null, false, false); ?>
This is by no means comprehensive, but hopefully useful. The CakePHP Google Group and CakePHP IRC Channel are super when trying to wrap you head around something new.
Thanks for spelling this out
It was kicking my butt, however thanks to this here I figured it out.
Comment on March 20, 2007 @ 1:52 pm
and the controller /agents/select ??
i’ve a problem because $agentArray comes null..
tnks
Comment on October 21, 2007 @ 7:18 pm
In this example you must have a function called “select” in agents_controller.php that returns $agentArray. I hope this helps!
Comment on October 22, 2007 @ 7:40 am
i have a problem with saving a value from selectTag
the array looks like this:
Array ( [15] => MALE [16] => FEMALE )
its display correctly in the combo box
but when i press the save button
the result in the database is “15″ or “16″
my expectation it shud be “MALE” or “FEMALE”
how to overcome this problem?
Comment on August 12, 2008 @ 5:18 am
uh oh i manage to solve already
( sry for spamming )
its because of the array result from by ganeratelist
it shud be:
Array ( [MALE] => MALE [FEMALE] => FEMALE )
instead of:
Array ( [15] => MALE [16] => FEMALE )
Comment on August 12, 2008 @ 5:43 am
i’m trying to just create a “selected” option and currently this is what i have:
$html->SelectTag(’Event/is_private’, array(”Y” => “Yes”, “N” => “No” ), array(”tabindex” => 4));
basically i just want the “Yes” option to be the default, and i can’t seem to wrap my mind around the idea of $optionAttr array to feed it the proper value.
sry i’m a noob.
thanks!
Comment on September 22, 2008 @ 11:07 pm
[...] Hacía poco había utilizado el ayudante HTML selectTag para rellenar la lista desplegable de una vista y mi tendencia instintiva -por así decirlo- fue razonar por analogía, sin darme cuenta. En otras palabras, si la lista desplegable de una vista se rellenaba con un ayudante HTML, ¿qué impedía utilizar otro, para imprimir cualquier campo de un registro relacionado, y no su identificador? Es decir, me pasó lo mismo que a Fran. Por cierto, también hay que decir que son varios los principiantes del Grupo Google CakePHP -en inglés- que encuentran algo confuso este ayudante HTML, como hacen aquí. [...]
Pingback on November 14, 2008 @ 5:16 am
I tried your example which helped, but eventually used the following method:
http://book.cakephp.org/view/201/options-empty
Comment on December 9, 2008 @ 9:26 am