$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>
....
I'd like to add a select box for searching with the CakeDC Search Plugin. IE:
<select name="field">
<option value="email">Search By Email</option>
<option value="first_name">Search By First Name</option>
</select>
Currently what I have in my VIEW is:
echo $this->Form->create('User', array(
'url' => array_merge(array('action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('email', array('div' => false, 'empty' => true));
echo $this->Form->input('first_name', array('div' => false, 'empty' => true));
This works just fine this way, but I'd like to avoid the multiple input boxes and simplify it with a select box. I could hard it (take the value from the select box and combine it with the value from the input box), but there has to be another way to do it...
Here is my User Module:
public $filterArgs = array(
'email' => array('type' => 'like'),
'first_name' => array('type' => 'like')
);
And this is my Controller:
public function index() {
$this->Prg->commonProcess();
$this->paginate['conditions'] = $this->User->parseCriteria($this->passedArgs);
$this->set('users', $this->paginate());
}
i think you are looking for
echo $this->Form->input('search', array('div' => false, 'empty' => true));
and
public $filterArgs = array(
'search' => array('type' => 'like', 'field'=>array('email', 'first_name')),
);
and
public $presetVars = true;
but you would lose the "AND" of the two inputs in favor of an OR (this is less powerful).
if thats ok for you, this would be the way.
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'));
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
I am using
$form->input('Model.name', array('multiple'=>'checkbox');
I am trying to base on model data to set certain checkboxes to checked.
How can i do that?
cmptrgeekken's solution works for a single checkbox. I'm assuming you're generating a multiple checkboxes, for a HABTM relation or something similar.
You need to pass a array with the values of the elements that are going to be selected to the method, like this:
$options = array(1 => 'ONE', 'TWO', 'THREE');
$selected = array(1, 3);
echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' => $options, 'selected' => $selected));
is going to generate this:
<div class="input select">
<label for="ModelName">Name</label>
<input name="data[Model][name]" value="" type="hidden">
<div class="checkbox">
<input name="data[Model][name][]" checked="checked" value="1" id="ModelName1" type="checkbox">
<label for="ModelName1" class="selected">ONE</label>
</div>
<div class="checkbox">
<input name="data[Model][name][]" value="2" id="ModelName2" type="checkbox">
<label for="ModelName2">TWO</label>
</div>
<div class="checkbox">
<input name="data[Model][name][]" checked="checked" value="3" id="ModelName3" type="checkbox">
<label for="ModelName3" class="selected">THREE</label>
</div>
</div>
The first and third checkbox checked.
Just remember that you're actually working with a multiple select element that is just displayed as a bunch of checkboxes (Which is IMO better because of the usability).
I don't use CakePHP, but according to the docs, it appears as though you should be able to add the option 'checked'=>true:
$form->input('Model.name', array('type'=>'checkbox','checked'=>true));
since that's one of the options of the checkbox function.
$options = array(1 => 'ONE', 'TWO', 'THREE');
$selected = array(1, 3);
echo $form->input('Model.name',
array(
"name"=>$mnus['Aco']['id'],
"type"=>"select",
"multiple"=>"checkbox",
'options' => $options,
'selected' => $selected)
);
this is the correct way for multiple check box and checked option. I am using this in cake1.3 please recheck once on your code it must work.
echo $this->Form->input('Title', array('type'=>'checkbox', 'label'=>'Label', 'checked'=>'checked'));
The Marko solution still working in CakePHP 2.0+
-> https://stackoverflow.com/a/1962499/3197383
It just need to correct with the new syntax :
<?php
$options = array(1 => 'ONE', 'TWO', 'THREE');
$selected = array(1, 3);
echo $this->Form->input('ModelName',
array('multiple' => 'checkbox', 'options' => $options, 'selected' => $selected)
);
?>
Its Super Simple
$form->input('field_name', array('type'=>'checkbox','checked'=>true));
That's it.
Documentation: https://book.cakephp.org/3.0/en/views/helpers/form.html
Another way to have a checkbox checked with the "label" right next to it is.
$form->checkbox('Model.name', array('checked'=>'checked'))?> Label
Label can be what ever you want though. example: 21,000-3000, Tire, Human. I am sure you get the idea.
<?php
$subjects = array(1=>'Snow boarding',2=>'Surfing',3=>'Trekking',4=>'Swimming');
$selected_skills = array(0=>2,1=>4);
// For MutiSelect box with selected
$form->input('skills_list',array('label' => 'Skills','options' => $subjects,'class' =>'','multiple'=>true,'selected'=> $selected_skills));
//For Multiple checkbox with checked
$form->input('skills_list',array('label' => 'Skills','options' => $subjects,'class' =>'','multiple'=>'checkbox','selected'=> $selected_skills));
?>
Here is a small code snippet from one of my project-
$categories = $this->Site->Category->find('list', array('recursive' => -1));
$this->set(compact('categories'));
$this->Site->Category->bindModel(array('hasOne' => array('CategoriesSite')));
$selected = $this->Site->Category->find('list', array(
'fields' => array('id'),
'conditions' => array(
'CategoriesSite.site_id' => $this->data['Site']['id'],
),
'recursive' => 0,
));
$this->set(compact('selected'));
Main key is for selected is 'fields' => array('id')
$options = array("fixed","varry");
$selected = "0";
echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' => $options, 'value' => $selected));
Use the value attribute to make checked default.
'likes_preferences' =>array(
'type'=>'select','label' => 'Main likes/preferences',
'options' => $this->Ethos->toOptionsArray('preferences'),
'multiple' => 'checkbox',
'div'=>array('class'=>'input select checkbox-group clearfix'),
'hiddenField' => false,
),
the above code for adding the data, you need to change the field 'likes_preferences' from array to comma separated string before saving into database.
$preferences = implode(',',$this->request->data['Member']['likes_preferences']);
$this->request->data['Member']['likes_preferences'] = $preferences;
EDIT MODE
$likes = explode(',',$this->request->data['Member']['likes_preferences']);
'likes_preferences' =>array(
'type'=>'select','label' => 'Main likes/preferences',
'options' => $this->Ethos->toOptionsArray('preferences'),
'multiple' => 'checkbox',
'div'=>array('class'=>'input select checkbox-group clearfix'),
'hiddenField' => false,
'selected' => $likes
),
you are done, again you must convert the array to string while updating the database in edit action.
You can also try this for input with single option
$this->Form->input('name', array('type' => 'checkbox', 'default' => 1, 'checked' => 'checked'));