# Forms dropdown load data from field

**URL:** <https://discourse.cakephp.org/t/forms-dropdown-load-data-from-field/7217>\
**Category:** Need Help\
**Created:** [February 3, 2020, 4:12pm UTC](https://discourse.cakephp.org/t/forms-dropdown-load-data-from-field/7217 "2020-02-03T16:12:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [February 3, 2020, 4:12pm UTC](https://discourse.cakephp.org/t/forms-dropdown-load-data-from-field/7217/1 "2020-02-03T16:12:41Z")

</div>

Hi,  
I’m having a few issues trying to figure out the following, even reading the documentation it makes little sense…  
I’ve creating the following 2 tables  
partners - id - partner\_type\_id (partner\_types id) FK

partner\_types - id - partner\_type

generated the code using cake bake all Partners  
generated the code using cake bake all PartnerTypes

on the add, edit form for the partner the dropdown only displays the ID from the partner\_type table how do I get it to display partner\_type with value of id??

Thanks

Mal

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [February 3, 2020, 5:20pm UTC](https://discourse.cakephp.org/t/forms-dropdown-load-data-from-field/7217/2 "2020-02-03T17:20:11Z")

</div>

It sounds like the array that is being provided to the Form-control() to render the partner\_type dropdown is in the form:

```auto
[id1 => id1, id2 => id2]

```

You’ll want it to be

```auto
[id1 => type1, id2 => type2]

```

You haven’t shown any code from your controller, but probably there is a find(‘list’) that is producing this array.

Looking at this should help fix your problem:

[https://book.cakephp.org/4/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs](https://book.cakephp.org/4/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs)

---

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [February 3, 2020, 8:34pm UTC](https://discourse.cakephp.org/t/forms-dropdown-load-data-from-field/7217/3 "2020-02-03T20:34:42Z")

</div>

Thanks @dreamingmind that worked  
I amended this line in my Controller  
` $partnerTypes = $this->Partners->PartnerTypes->find('list', ['keyField' => 'id', 'valueField' => 'partner_type']);`

so it is now displaying the options correctly, only thing is how do I add `<option> Select Option</option>` to the start of the drop down??

Regards

Mal

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 3, 2020, 11:57pm UTC](https://discourse.cakephp.org/t/forms-dropdown-load-data-from-field/7217/4 "2020-02-03T23:57:46Z")

</div>

`'empty' => 'Select Option'` in the array that you pass to the `input` (or `control` method). Not the array of choices that you pass in the `'options'` key… Like:

```auto
echo $this->Html->input('partner_type_id', [
    'options' => $partnerTypes,
    'empty' => 'Select Option',
]);
```

---

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [February 4, 2020, 10:08am UTC](https://discourse.cakephp.org/t/forms-dropdown-load-data-from-field/7217/5 "2020-02-04T10:08:28Z")

</div>

Thanks @Zuluru

I’m now struggling with another bit of code ☹ which I can’t get my head around how to write so it doesn’t throw an error…  
I have several tables using the id from the partner table as a FK I’ve managed to get slug working, but now I have done this the data isn’t displaying on the view page for the other table data how do I write the following to get them to display??

```
public function view($slug = null)
    {
        $partner = $this->Partners->findBySlug($slug)->firstOrFail(
		->get($id [
            'contain' => ['PartnerTypes', 'SubscriptionTypes', 'Users', 'PartnerFacilities', 'PartnerPegs', 'PartnerWatersSpecies', 'PartnersWaters'],
        ]));
		$this->viewBuilder()->setLayout('partners');
        $this->set('partner', $partner);
    }

```

Regards

Mal

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 4, 2020, 2:33pm UTC](https://discourse.cakephp.org/t/forms-dropdown-load-data-from-field/7217/6 "2020-02-04T14:33:53Z")

</div>

1. For a new problem, you should make a new post.
2. If you’re getting error messages, you should tell us what the error is.
3. If the problem is in generating output, you should show the code that generates the output.

---

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [February 4, 2020, 3:07pm UTC](https://discourse.cakephp.org/t/forms-dropdown-load-data-from-field/7217/7 "2020-02-04T15:07:59Z")

</div>

thanks @Zuluru will do
