Z-Mehn
Z-Mehn

Reputation: 268

CakePHP Form Helper - Select - How do I add a Placeholder for a select element?

How do I get a placeholder like default option in a select (drop down) using CakePHP 2?

Right now I have the following

<?php echo $this->Form->input('gender', array('options' => array('male' => 'Male', 'female' => 'Female'), 'empty' => '','label' => '','class'=>'scale')); ?>

I want to have a greyed out default value of 'Gender' so the user actually knows what the drop down does. I also dont want the form to send that value.

If its raw HTML then I guess it would just be

selected="selected" disabled="disabled"

Upvotes: 1

Views: 12897

Answers (4)

jakubplus
jakubplus

Reputation: 317

The easiest way to put a placeholder:

echo $this->Form->input('User.role_id', array(
     'options' => $roles,
     'empty' => 'Choose', 
));

Upvotes: 0

Anil
Anil

Reputation: 21910

Your looking for empty, You can pass this as the third parameter to $this->Form->select().

array('empty' => array(0 => '-- Select --'))


Docs

View the docs here: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs

/**
 * Method Signature
 *
 * public function select($fieldName, $options = array(), $attributes = array())
 *
 * ### Attributes:
 *
 * - `showParents` - If included in the array and set to true, an additional option element
 *   will be added for the parent of each option group. You can set an option with the same name
 *   and it's key will be used for the value of the option.
 * - `multiple` - show a multiple select box. If set to 'checkbox' multiple checkboxes will be
 *   created instead.
 * - `empty` - If true, the empty select option is shown. If a string,
 *   that string is displayed as the empty element.
 * - `escape` - If true contents of options will be HTML entity encoded. Defaults to true.
 * - `value` The selected value of the input.
 * - `class` - When using multiple = checkbox the class name to apply to the divs. Defaults to 'checkbox'.
 * - `disabled` - Control the disabled attribute. When creating a select box, set to true to disable the
 *   select box. When creating checkboxes, `true` will disable all checkboxes. You can also set disabled
 *   to a list of values you want to disable when creating checkboxes.
 *
 */

PHP:

// Example..
$this->Form->select(
    'model', // First param = fieldName
    $options, // Second param = options
    array('empty' => array(0 => '-- Select --')) // Third param = attributes
); 

HTML: (Rendered)

// Renders
<select>
    <option value="0">-- Select --</option>
</select>

You can remove the key and just pass a string value if you don't require a value in the option.

Upvotes: 0

Vikash Pathak
Vikash Pathak

Reputation: 3562

It should be.

$this->Form->input(
    'gender', 
    array(
        'options' => array('Gender' => array('male' => 'Male', 'female' => 'Female')),           
        'empty' => 'Your placeholder will goes here',
        'label' => '',
        'class'=>'scale'
    )
);

Upvotes: 3

arilia
arilia

Reputation: 9398

I'd rather do something like this

$this->Form->input(
    'gender', 
    array(
        'options' => array('Gender' => array('male' => 'Male', 'female' => 'Female')),           
        'empty' => '',
        'label' => '',
        'class'=>'scale'
    )
);

Upvotes: 2

Related Questions