CakePHP - populating select form - cakephp

I'm trying to populate a drop down select form with values from a database.
Here is what I have currently.
$modes = Set::combine($this->Setting->find('all', array('conditions' => array('setting_name LIKE' => 'mode_%'))), '{n}.Setting.id','{n}.Setting.setting_name');
$this->set('modes', $modes);
Then in the view, this is what I have
echo $form->select('current_mode',$modes);
That output
<select name="data[Setting][current_mode]" id="SettingCategoryId">
<option value=""></option>
<option value="2">mode_2</option>
<option value="1">mode_1</option>
<option value="3">mode_3</option>
</select>
The output that I have right now almost work but how can I make the output to be like this?
<select name="data[Setting][current_mode]" id="SettingCategoryId">
<option value="mode_2">Title 2</option>
<option value="mode_1">Title 1</option>
<option value="mode_3">Title 3</option>
</select>
Note:
1. no default option with empty value
2. Option's value isn't the id and titles comes from a "title" field in the table
Thanks,
Tee

See http://book.cakephp.org/view/1022/find-list and http://book.cakephp.org/view/1062/displayField.
$settings = $this->Setting->find('list', array(
'conditions' => array('Setting.setting_name LIKE' => 'mode_%'),
'fields' => array('Setting.id', 'Setting.title')
));
$this->set(compact('settings'));
// view
echo $this->Form->input('current_mode', array(
'type' => 'select',
'options' => $settings,
'empty' => false
));

You want to loop through modes and create an option for each mode, like so:
$options = array();
foreach($modes as $mode)
{
$options[$mode] = "Title " . $mode;
}
echo $form->select('current_mode', $options);
You can either put the above logic in your view, or you can do it in your controller, and then set the variable like this:
$this->set("options", $options);
The docs here explain the select element method pretty well:
http://book.cakephp.org/view/1430/select

Related

Cakephp 2.5.1 form input order of values

$getCities = $this->requestAction('objekts/getCities');
This returns me a list with ids and names of cities in alphabetical order.
When I debug $getCities I can see the alphabetical order.
This is the way how I select the data:
$results = $this->find('list', array(
'conditions' => array($and, $andZone),
'fields' => 'REG_ID, REG_NAME',
'group' => 'REG_ID',
'order' => array('REG_NAME ASC')
));
Again, the debug data is in alphabetical order.
But!
echo $this->Form->input('city', array('label' => __('Nach Ort suchen'), 'empty' => __('Bitte wählen'), 'options' => $getCities, 'id' => 'city', 'class' => 'styled-select'));
This is changing the order to it's id's and does not keep my alphabetical order on the city names.
Is there anything I can add to the $this->Form->input to set my order to names instead of ids?
Please advice!
Thanks!!
edit:
this is the debug array
'{"4":"Alar\u00f3","67":"Algaida","99":"Alqueria Blanca","5":"Andratx","6":"Art\u00e1","8":"Bendinat","105":"Biniali","9":"Binissalem","70":"Bunyola",.....
this is the html output
<select id="city" class="styled-select" name="data[Objekt][city]">
<option value="">Bitte wählen</option>
<option value="4">Alaró</option>
<option value="5">Andratx</option>
<option value="6">Artá</option>
....
<option value="67">Algaida</option>
...
<option value="99">Alqueria Blanca</option>
....

Cakephp option tag attributes

In the view:
echo $this->Form->input('Ingredient');
The above populate multiple select list that HTML output as:
<select name="data[Ingredient][Ingredient][]" option="hh" multiple="multiple" id="IngredientIngredient">
<option value="1" selected="selected">Tomato</option>
<option value="2">Spaghetti </option>
<option value="3" selected="selected">Salt</option>
</select>
What I need to know is how to add attributes to the generated <option> tag?
Use the controller to pass selected values:
if ($this->request->is('post') {
// save form
} else {
$this->request->data['Incredient']['Incredient'] = $ids;
}
See here
To add additional attributes like classes you just need to make it a deeper array and you can pass those:
$options = array(
1 => 'One',
2 => array('name' => 'Two', 'value' => 2, 'class' => 'extra'),
3 => 'Three');
echo $this->Form->input('test', array('type' => 'select', 'options' => $options));
The result:
<div class="input select">
<label for="ModelTest">Test</label>
<select name="data[Model][test]" id="ModelTest">
<option value="1">One</option>
<option value="2" class="extra">Two</option>
<option value="3">Three</option>
</select>
</div>
See this
My Example custom tag option in select, by (CakePHP 2 + VueJs 2) is easy.
<?php
$options = array(
array(
'value' => false,
'v-for' => 'obj in data_options_input_id_vuejs',
'name' => '{{obj.text}}',
'v-bind:value' => 'obj.value',
)
);
echo $this->Form->input('input_id', array(
'type' => 'select',
'required' => true,
'class' => 'input-block-level',
'options' => $options,
'v-model:' => 'productos_stocks_padre_combo_select',
));
?>
Hi all, In my case, i want to add code="code-value" to my select option and this code is working for me.
If you want to add extra data to your option, try this code:
//In controller
$chapters = $this->Course->Chapter->find('all', array('fields' => array('name', 'code', 'id')));
// Call the noop function $this->noop() on every element of chapters
$chapters = Hash::map($chapters, "{n}.Chapter", array($this, 'noop'));
// Callback noop function
function noop($option) {
$option['value'] = $option['id'];
unset($option['id']);
return $option;
}
You can see document about Hash:map function here. Good luck.

CakePHP : returning a non-id from a view

I have a form in cakephp, that implements a drop down, as follows:
echo $this->Form->input('city', array('type' => 'select','options' => $cities, 'label' => 'City'));
The $cities array passed to the view is similar to
Array
(
[0] => London
[1] => Tokyo
)
The form displays the names of the cities (London, Tokyo, ...).
On submission of the form, the form submits the id (key) (eg. '0' if the user selected London). Instead, I would like to get the value (London) as text. What changes do I need to make in the view, to return the string containing the value?
You can specify via the select method:
echo $this->Form->select('city', array(
'London' => 'London',
'New York' => 'New York',
'Tokyo' => 'Tokyo'
));
will generate:
<select name="data[Model][city]" id="ModelCity">
<option value="London">London</option>
<option value="New York">New York</option>
<option value="Tokyo">Tokyo</option>
</select>
See: CakePHP manual
Cake fills up a select with values you provide. If You need a name of a city, you need to pass an array where your key will be this name.
$foo = array('London'=>'London', 'Tokyo'=>'Tokyo');
Format the $cities array to an associative array, having both the 'keys' and the 'values' set to the name of the city;
$cities = array(
'London' => 'London',
'Paris' => 'Paris',
// etc.
);

how to delete the empty value option in $form->select

when i need to use select form i see the first value is empty..but i dont need this empty value option ..how to do this..thanks
<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options)
?>
Will output:
<select name="data[User][gender]" id="UserGender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
In Cake 2.x, you can just add the 'empty'=>false like this (tested and works):
<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, array('empty'=>false));
?>
In CakePHP 1.3.x (per this page in the book) you might have to add an additional null like this:
<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, null, array('empty'=>false));
?>

cakephp: I want a simple select box with options and no optgroup

The select box for forum_categories in the cupcake forum plugin doesn't allow me to select its options. When I rollover the options with my mouse, the highlight stays at 'Select a Forum'. The following is the original
<?php
echo $form->input('forum_category_id', array(
'options' => $forums,
'empty' => '-- '. __d('forum', 'Select a Forum', true) .' --',
'label' => __d('forum', 'Forum Category', true)
));
?>
And i modified it to:
<?php
echo $form->input('Topic.forum_category_id',array(
'empty' => 'Select a Forum',
'options' => $forums
));
?>
The following is the html code it is generating:
<select name="data[Topic][forum_category_id]" id="TopicForumCategoryId">
<option value="">Select a Forum</option>
<optgroup label="Summer Camp">
</optgroup>
</select>
The find stmt in the forumcategory model:
$forums = $this->Forum->find('list', array(
'conditions' => array(
'Forum.status' => 0,
'Forum.accessView <=' => $access,
'Forum.access_level_id' => $accessLevels
),
'order' => 'Forum.orderNo ASC'
));
How can I get rid of the optgroup in the html code above? I just want a simple select box with options and no optgroup like the following:
<select name="data[Topic][forum_category_id]" id="TopicForumCategoryId">
<option value="">Select a Forum</option>
<option value="1">Summer Camp</option>
</select>
thank you.
probably a bit late but
you get opt groups when
your options looks like this :
$arr = array(
'optgroup' => array(
'1','2','3'),
'optgroup2' => array(
'1',2,3)
);
echo $this->Form->input('some',array('options' => $arr));
Try:
echo $this->Form->input('Topic.forum_category_id', array('options' => $forums, 'empty' => 'Select a Forum'));

Resources