ItGo.me Focus on IT Recommend

Home > php - CakePHP select default value in SELECT input

php - CakePHP select default value in SELECT input

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

up vote 7 down vote favorite 3 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? php cakephp relationships helpers cakephp-2.x
  |
  this question edited Jul 9 '16 at 15:27 asked Oct 9 '09 at 19:57 erjiang 27.5k 5 45 83



 |  8 Answers

up vote 19 down vote ---Accepted---Accepted---Accepted---

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;


  |
  this answer edited Sep 28 '11 at 18:41 Jason Plank 2,123 4 24 38 answered Oct 10 '09 at 6:58 Miles Johnson 239 1 2 28   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 1 '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



 |  up vote 43 down vote 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'));


  |
  this answer edited Sep 28 '11 at 19:23 Jason Plank 2,123 4 24 38 answered Aug 5 '11 at 17:17 Ryan 3,834 7 27 36 9   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!) –  Fire-Dragon-DoL Apr 6 '12 at 18:56      Take into consideration the latency time due to the cache of cakephp... –  MUY Belgium Aug 13 '12 at 13:42 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      this also works in version 2.2 –  Sp0T Sep 3 '14 at 11:48



 

Recommend:php - CakePHP nesting two select queries

---------idcompetence_iduser_idrating I need a way to write the following query in the cake way: SELECT * FROM competencies WHERE id NOT IN (SELECT competence_id FROM competence_ratings WHERE employee_id = $userId) Someone please help

|  up vote 8 down vote the third parameter should be like array('selected' =>value)
  |
  this answer answered Jul 7 '10 at 12:42 Renjith Chacko 81 1 1      This is only if you want that value to ALWAYS be selected –  dfmiller Jun 24 '14 at 22:40

 |  up vote 3 down vote

$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.
  |
  this answer answered Dec 26 '13 at 11:43 Sadikhasan 12.1k 8 36 76



 |  up vote 1 down vote 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.
  |
  this answer edited Sep 28 '11 at 18:41 Jason Plank 2,123 4 24 38 answered Oct 9 '09 at 21:02 Funky Dude 2,910 2 15 29      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 5: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



 |  up vote 0 down vote 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...
  |
  this answer edited Sep 28 '11 at 19:07 Jason Plank 2,123 4 24 38 answered Sep 28 '11 at 5:20 Haroon 9 1



 |  up vote 0 down vote 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)

); ?>


  |
  this answer edited Jun 23 '15 at 14:50 hg8 589 6 20 answered Apr 25 '13 at 12:29 Zimmo 11 1



 |  up vote 0 down vote 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']);


  |
  this answer edited Jul 8 '16 at 10:40 MegaTron 7,538 11 20 45 answered Jul 8 '16 at 10:11 rakeysharyal 1 1



 | 

Recommend:timezone - show default value in select box cakephp

mezone helper class in order to show time in my select box http://bakery.cakephp.org/articles/MarkAlanEvans/2009/12/17/updated-timezone-helper in my view i am echoing timezone in select box like this echo $this->Timezone->select('timezone

up vote 7 down vote favorite 3 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

------splitte line----------------------------