17

Using CakePHP:

I have a many-to-one relationship, let's pretend it's many Leafs to Trees. Of course, I baked a form to add a Leaf to a Tree, and you can specify which Tree it is with a drop-down box ( tag) created by the form helper.

The only thing is, the SELECT box always defaults to Tree #1, but I would like it to default to the Tree it's being added to:

For example, calling example.com/leaf/add/5 would bring up the interface to add a new Leaf to Tree #5. The dropdown box for Leaf.tree_id would default to "Tree 5", instead of "Tree 1" that it currently defaults to.

What do I need to put in my Leaf controller and Leaf view/add.ctp to do this?

erjiang
  • 44,417
  • 10
  • 64
  • 100

11 Answers11

54

In CakePHP 1.3, use 'default'=>value to select the default value in a select input:

$this->Form->input('Leaf.id', array('type'=>'select', 'label'=>'Leaf', 'options'=>$leafs, 'default'=>'3'));
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Ryan
  • 10,798
  • 11
  • 46
  • 60
  • 10
    Why this is not marked as answer? This is the correct one (the other could be a suggestion, but this is the real answer about the question!) – Francesco Belladonna Apr 06 '12 at 18:56
  • 3
    This is correct in cake 1.2 as well. To stop yourself from going mad - don't just refresh the page to see if the default works (it will stay with which ever value is selected), you need to go back a page and re-click on the page again (or go click in the address bar and hit enter) – icc97 Dec 31 '12 at 22:17
21

You should never use select(), or text(), or radio() etc.; it's terrible practice. You should use input():

$form->input('tree_id', array('options' => $trees));

Then in the controller:

$this->data['Leaf']['tree_id'] = $id;
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Miles Johnson
  • 259
  • 1
  • 2
  • 31
    I'm not sure about "terrible practice" – SeanDowney Apr 22 '10 at 23:30
  • 2
    Can you please explain this a bit better? Where is that variable `$tree` defined? Isn't `$options` supposed to be an array with key the type of the option? – mgPePe Sep 24 '11 at 12:17
  • 3
    Believes what he says Miles, is a TERRIBLE practice, i've spent 2:00 hours of my life traying to set the "after" attribute of Cake with a $this->Form->Select() and it doesn't work fine. In order to save valuable time use $this->Form->input as a Select. – BruneX Apr 18 '13 at 14:42
  • 1
    Agreed; for whatever reason, cakePHP totally doesn't care for the select(). – Vael Victus Oct 01 '13 at 17:50
  • 1
    There is a time to use input and a time to use select etc. – dfmiller Jun 24 '14 at 22:39
9
 $this->Form->input('Leaf.id', array(
'type'=>'select',
'label'=>'Leaf',
'options'=>$leafs,
'value'=>2
));

This will select default second index position value from list of option in $leafs.

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
8

the third parameter should be like array('selected' =>value)

1

Assuming you are using form helper to generate the form:

select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)

Set the third parameter to set the selected option.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Funky Dude
  • 3,867
  • 2
  • 23
  • 33
  • Hey, I have the following code: $options=array('1'=>'opt1','2'=>'opt2','3'=>'opt3'); echo $form->select('Fieldname',$options , ??? ,array(),false); whats the format for the 3rd parameter to set the selected item? I can't seem to get it right. – cardflopper Oct 12 '09 at 05:58
  • it should be the value of the selected option – Funky Dude Oct 12 '09 at 19:58
  • In which version of CakePHP is this - I can't find documentation for this in either 1.1, 1.2 or 1.3? – icc97 Dec 31 '12 at 22:19
1

cakephp version >= 3.6

echo $this->Form->control('field_name', ['type' => 'select', 'options' => $departments, 'default' => 'your value']);
0
FormHelper::select(string $fieldName, array $options, 
array $attributes)

$attributes['value'] to set which value should be selected default

<?php echo $this->Form->select('status', $list, array(
    'empty' => false, 
    'value' => 1)
); ?>
hg8
  • 1,082
  • 2
  • 15
  • 28
Viktor Br
  • 11
  • 1
0

If you are using cakephp version 3.0 and above, then you can add default value in select input using empty attribute as given in below example.

echo $this->Form->input('category_id', ['options'=>$categories,'empty'=>'Choose']);
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
0

The best answer to this could be

Don't use selct for this job use input instead

like this

echo  $this->Form->input('field_name', array(
          'type' => 'select',
            'options' => $options_arr, 
            'label' => 'label here',
            'value' => $id,  // default value
            'escape' => false,  // prevent HTML being automatically escaped
            'error' => false,
            'class' => 'form-control' // custom class you want to enter
        ));

Hope it helps.

M.suleman Khan
  • 576
  • 6
  • 17
0

As in CakePHP 4.2 the correct syntax for a select form element with a default value is quite simple:

echo $this->Form->select(
                'fieldname',
                ['value1', 
                'value2', 
                'value3'], 
                ['empty' => '(auswählen)','default'=>1]
            );

If hopefully don't need to explain the default=1 means the second value and default=0 means the first value. ;)

Be very careful with select values as it can get a little tricky. The example above is without specific values for the select fields, so its values get numerated automatically. If you set a specific value for each select list entry, and you want a default one, set its specific value:

$sizes = ['s' => 'Small', 
'm' => 'Medium', 
'l' => 'Large'];
echo $this->Form->select('size', $sizes, ['default' => 'm']);

This example is from the official 4.x Strawberry Cookbook. https://book.cakephp.org/4/en/views/helpers/form.html#options-for-control

0

To make a text default in a select box use the $form->select() method. Here is how you do it.

$options = array('m'=>'Male','f'=>'Female','n'=>'neutral');

$form->select('Model.name',$options,'f');

The above code will select Female in the list box by default.

Keep baking...

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Haroon
  • 9
  • 1