# Form Helper, dropdown issue

**URL:** <https://discourse.cakephp.org/t/form-helper-dropdown-issue/331>\
**Category:** Need Help\
**Created:** [May 16, 2016, 1:31am UTC](https://discourse.cakephp.org/t/form-helper-dropdown-issue/331 "2016-05-16T01:31:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aagasi](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@aagasi](https://discourse.cakephp.org/u/aagasi)\
**Post date:** [May 16, 2016, 1:31am UTC](https://discourse.cakephp.org/t/form-helper-dropdown-issue/331/1 "2016-05-16T01:31:58Z")

</div>

Hello Community, i’m here to ask for help with the **Form Helper.**

**Context:**

- I have **3** associated models ( **UsersTable, ContactTypesTable** , and **UsersContactTypes** )
- The **add** method is working perfect. The data is being stored in the DB with the associated model information.
- The **edit.ctp** is showing the data of the user perfect too. Also is showing the data of the associated model, but only is being displayed the **value** field.
- Also the DEBUG is showing the data of the user and the associated model perfectly. So the view has all the information it needs to do it work.

The **PROBLEM** :

- The DROPDOWN i use to show the different ‘contact types’ is being displayed well, but it’s missing the **‘selected’** attribute. This make the dropdown not show the correct value that is stored in the DB.

I tried all day make this work but i’m failing. Need some ideas.

* * *

**UsersTable.php**

```
public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('users');
        $this->displayField('id');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');
        /** Acl plugin behavior. */
        $this->addBehavior('Acl.Acl', ['type' => 'requester']);

        /** Set the association between models. */
        $this->hasMany('UsersContactTypes', [
            'dependent' => true
        ]);
    }

```

**ContactTypesTable.php**

```
public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('contact_types');
        $this->displayField('name');
        $this->primaryKey('id');

        /** Set the association between models. */
        $this->hasMany('UsersContactTypes',[
            'dependent' => true
        ]);
    }

```

**UsersContactTypesTable.php**

```
public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('users_contact_types');
        $this->displayField('user_id');
        $this->primaryKey(['user_id', 'contact_type_id']);

        /** Set the association between models. */
        $this->belongsTo('Users');
        $this->belongsTo('ContactTypes');
    }

```

**edit.ctp**

```
//First pair of contact type => value
echo $this->Form->input('users_contact_types[0].contact_type_id');
echo $this->Form->input('users_contact_types.0.value');

//Second pair of contact type => value
echo $this->Form->input('users_contact_types[1].contact_type_id');
echo $this->Form->input('users_contact_types.1.value');

```

**UsersController.php**

```
public function edit($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => ['UsersContactTypes']
        ]);

        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            /** Save the User model with it associated model UsersContactTypes. */
            if ($this->Users->save($user,['associated' => ['UsersContactTypes']])) {
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The user could not be saved. Please, try again.'));
            }
        }
        /** Prepare variables that i use in the view. */
        $groups = TableRegistry::get('Groups')->find('list', ['limit' => 200]);
        $contactTypes = TableRegistry::get('ContactTypes')->find('list', ['limit' => 200])->all();

        /** Set the data so it becames available in the view. */
        $this->set(compact('user', 'contactTypes','groups'));
        $this->set('_serialize', ['user']);
    }

```

* * *

Debug of the **$user** in the **edit.ctp** file:

```
/plugins/UsersCrudAuth/src/Template/Users/edit.ctp (line 35)
object(UsersCrudAuth\Model\Entity\User) {

	'id' => (int) 1,
	'username' => 'aagasi',
	'name' => 'Adolfo',
	'surname' => 'Agasi',
	'gender' => 'Masculino',
	'password' => '$2y$10$1/syLa4VuX6inYyKPF7MZOlTilYO5B/aunxnyGE1wALJvq4nau6xa',
	'group_id' => (int) 1,
	'created' => object(Cake\I18n\FrozenTime) {

		'time' => '2016-05-09T18:30:42+00:00',
		'timezone' => 'UTC',
		'fixedNowTime' => false
	
	},
	'modified' => object(Cake\I18n\FrozenTime) {

		'time' => '2016-05-16T01:20:56+00:00',
		'timezone' => 'UTC',
		'fixedNowTime' => false
	
	},
	'status' => 'Active',
	'users_contact_types' => [
		(int) 0 => object(Cake\ORM\Entity) {

			'user_id' => (int) 1,
			'contact_type_id' => (int) 3,
			'value' => 'agasi.adolfo',
			'[new]' => false,
			'[accessible]' => [
				'*' => true
			],
			'[dirty]' => [],
			'[original]' => [],
			'[virtual]' => [],
			'[errors]' => [],
			'[invalid]' => [],
			'[repository]' => 'UsersContactTypes'
		
		},
		(int) 1 => object(Cake\ORM\Entity) {

			'user_id' => (int) 1,
			'contact_type_id' => (int) 5,
			'value' => 'agasiadolfo@gmail.com',
			'[new]' => false,
			'[accessible]' => [
				'*' => true
			],
			'[dirty]' => [],
			'[original]' => [],
			'[virtual]' => [],
			'[errors]' => [],
			'[invalid]' => [],
			'[repository]' => 'UsersContactTypes'
		
		}
	],
	'[new]' => false,
	'[accessible]' => [
		'*' => true
	],
	'[dirty]' => [],
	'[original]' => [],
	'[virtual]' => [],
	'[errors]' => [],
	'[invalid]' => [],
	'[repository]' => 'UsersCrudAuth.Users'

}

```

Snapshot of user **edit** form.

![](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/1X/9edeefa8f123973f68e8de21cc6e29075bcec8e1.png)

Hope you can help me figure out whats going on with my code.

Regards.

---

<div class="post-metadata">

**Author:** ![reuben.helms](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/reuben.helms/32/172_2.png) [@reuben.helms](https://discourse.cakephp.org/u/reuben.helms)\
**Post date:** [May 16, 2016, 6:20am UTC](https://discourse.cakephp.org/t/form-helper-dropdown-issue/331/2 "2016-05-16T06:20:41Z")

</div>

> [@aagasi](#):
>
> echo $this-\>Form-\>input(‘users\_contact\_types[0].contact\_type\_id’);

You might need to explicitly set the options.

```auto
echo $this->Form->input('users_contact_types[0].contact_type_id', ['options' => $contactTypes]);

```

---

<div class="post-metadata">

**Author:** ![aagasi](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@aagasi](https://discourse.cakephp.org/u/aagasi)\
**Post date:** [May 16, 2016, 12:05pm UTC](https://discourse.cakephp.org/t/form-helper-dropdown-issue/331/3 "2016-05-16T12:05:47Z")

</div>

Hi Reuben, i tried that but still not working.

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [May 16, 2016, 6:23pm UTC](https://discourse.cakephp.org/t/form-helper-dropdown-issue/331/4 "2016-05-16T18:23:58Z")

</div>

You need to use input names like `'users_contact_types.0.contact_type_id'`, `'users_contact_types.0.contact_type_id'` etc.

---

<div class="post-metadata">

**Author:** ![aagasi](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@aagasi](https://discourse.cakephp.org/u/aagasi)\
**Post date:** [May 16, 2016, 6:30pm UTC](https://discourse.cakephp.org/t/form-helper-dropdown-issue/331/5 "2016-05-16T18:30:42Z")

</div>

I just tried what you say, but still not working.

---

<div class="post-metadata">

**Author:** ![reuben.helms](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/reuben.helms/32/172_2.png) [@reuben.helms](https://discourse.cakephp.org/u/reuben.helms)\
**Post date:** [May 16, 2016, 10:27pm UTC](https://discourse.cakephp.org/t/form-helper-dropdown-issue/331/6 "2016-05-16T22:27:45Z")

</div>

That was going to be my next suggestion.

---

<div class="post-metadata">

**Author:** ![reuben.helms](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/reuben.helms/32/172_2.png) [@reuben.helms](https://discourse.cakephp.org/u/reuben.helms)\
**Post date:** [May 16, 2016, 10:47pm UTC](https://discourse.cakephp.org/t/form-helper-dropdown-issue/331/7 "2016-05-16T22:47:11Z")

</div>

Just to recap:

The select box widget is rendering fine, and has all the contactType options. We can see that the select box is rendering via the screenshot.

It would be good to see the HTML source for the select box, with all the options, just to make sure the `users_content_types.0.contact_type_id` value has a match in the list.

Your next step might be to do some debugging, specifically on `View\Widget\SelectBoxWidget.php` and the `_renderOptions` method, where the `_isSelected` method is called for a particular `select` `option`. Something in there is likely to highlight why the appropriate contact type is not actually getting marked as selected.
