shepherdweb.com

Using the CakePHP selectTag HtmlHelper

January 7th, 2007

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.

Technorati Tags: ,

6 Comments »

  1. NoobBaker wrote,

    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

  2. PRF! wrote,

    and the controller /agents/select ??

    i’ve a problem because $agentArray comes null..

    tnks

    Comment on October 21, 2007 @ 7:18 pm

  3. Shane wrote,

    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

  4. asipo wrote,

    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

  5. asipo wrote,

    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

  6. slimcady wrote,

    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

Leave a comment

RSS feed for comments on this post.

TrackBack URI

Bad Behavior has blocked 679 access attempts in the last 7 days.