# Form with "dropdown of existing and add new entity" in one

**URL:** <https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729>\
**Category:** Need Help\
**Created:** [April 25, 2020, 11:29am UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729 "2020-04-25T11:29:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![HypJ3U50N](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/hypj3u50n/32/1803_2.png) [@HypJ3U50N](https://discourse.cakephp.org/u/HypJ3U50N)\
**Post date:** [April 25, 2020, 11:29am UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/1 "2020-04-25T11:29:48Z")

</div>

I want to combine the basic `Form->control('thing.id')` and` Form->control('thing_id',['options'=>$things])` in an add template.

Such that the user can select from the existing list of things through the foreign key `thing_id`, and can add a new `thing.id` into the database if the thing doesn’t exist yet.

Is this possible in one form layout?

I have looked at [https://book.cakephp.org/4/en/views/helpers/form.html#control-specific-options](https://book.cakephp.org/4/en/views/helpers/form.html#control-specific-options) and [https://book.cakephp.org/4/en/views/helpers/form.html#create-select-picker](https://book.cakephp.org/4/en/views/helpers/form.html#create-select-picker), resulting in  
` echo $this->Form->control('country_id', ['options' => $countries, 'country.id']);` which does not give a syntax error, but shows just like without the extra option ‘country.id’.  
Using select, I get syntax errors:  
`#echo $this->Form->select('country_id', [$countries, 'Enter New'=>'country.id']);`  
`#echo $this->Form->select('country_id', [[$countries], ['country.id']]);`

---

<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:** [April 25, 2020, 4:22pm UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/2 "2020-04-25T16:22:58Z")

</div>

You can do this with a little effort.

Make two controls in your form

```auto
echo $this->Form->control('new_thing');
echo $this->Form->control('thing', ['options' => $things];

```

In your Controller, just check the values of the two request data array nodes and process it as you see fit.

---

<div class="post-metadata">

**Author:** ![HypJ3U50N](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/hypj3u50n/32/1803_2.png) [@HypJ3U50N](https://discourse.cakephp.org/u/HypJ3U50N)\
**Post date:** [April 26, 2020, 10:10am UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/3 "2020-04-26T10:10:20Z")

</div>

So it is not possible to combine them into one form input?  
And what would be the variable of a particular control node or to distinguish between them, to say if Formcontrol1 is empty, insert value Formcontrol2?

---

<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:** [April 26, 2020, 7:16pm UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/4 "2020-04-26T19:16:37Z")

</div>

If you have the two inputs with the same name attribute, the browser can’t know which one to return to the server as the current value of the input

This code:

```auto
    echo $this->Form->create();
    echo $this->Form->control('input', ['type' => 'text']);
    echo $this->Form->control('input', ['options' => $inputs, 'empty' => 'choose one']);
    echo $this->Form->submit();
    echo $this->Form->end();

```

Would make this html:

 ![Screen Shot 2020-04-26 at 12.03.00 PM](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/3/3c63e3acc135fa4ea6e42465ca56bc16da429b9a.png)

and result in this data back to your program:

```auto
[
	'input' => ''
]

```

You could do something with javascript to coordinate the overlapping inputs. But I’m not seeing the gain in this approach.

This code:

```auto
    echo $this->Form->create();
    echo $this->Form->control('input_new', ['type' => 'text', 'placeholder' => 'new one']);
    echo $this->Form->control('input', ['options' => $inputs, 'empty' => 'choose one']);
    echo $this->Form->submit();
    echo $this->Form->end();

```

This would make these inputs which you could format for clarity:

 ![Screen Shot 2020-04-26 at 12.13.48 PM](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/f/f0ed7655a16c268f12e53d338067bf19ab3ae451.png)

Would get you this data back to the program:

```auto
[
	'input_new' => '',
	'input' => ''
]

```

You could either make rule in your code that any ‘input\_new’ data is used if present or use javascript to insure only one or the other input is populated.

---

<div class="post-metadata">

**Author:** ![HypJ3U50N](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/hypj3u50n/32/1803_2.png) [@HypJ3U50N](https://discourse.cakephp.org/u/HypJ3U50N)\
**Post date:** [May 6, 2020, 8:26pm UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/5 "2020-05-06T20:26:07Z")

</div>

Thanks for the (visual) help! I have trouble with the display field of the foreign id keys.  
I would be OK for now to have it like this:  
adding an order:

```
echo $this->Form->control('product_id', ['options' => $products, 'empty'=>'select product']);
echo $this->Form->control('product.name', ['type' => 'text', 'placeholder' => 'If item is not yet in the list, add here']);

```

This order can’t probably be saved since only product\_id is the foreign key and not the product\_name. I have set name as the product display field such that ‘options’=\>$products show the list of names instead of ids. How to add a new product entity by inputting a name while the foreign id has to be created and added to the order?

---

<div class="post-metadata">

**Author:** ![HypJ3U50N](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/hypj3u50n/32/1803_2.png) [@HypJ3U50N](https://discourse.cakephp.org/u/HypJ3U50N)\
**Post date:** [May 7, 2020, 10:22am UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/6 "2020-05-07T10:22:06Z")

</div>

From [https://book.cakephp.org/4/en/views/helpers/form.html#creating-inputs-for-associated-data](https://book.cakephp.org/4/en/views/helpers/form.html#creating-inputs-for-associated-data) I see

$this-\>Form-\>create($article);

// Article controls.  
echo $this-\>Form-\>control(‘title’);

// Author controls (belongsTo)  
echo $this-\>Form-\>control(‘author.id’);  
echo $this-\>Form-\>control(‘author.first\_name’);  
echo $this-\>Form-\>control(‘author.last\_name’);

What makes me wonder, if it is necessary to have author fist and last name as foreign keys in the articles DB.

I tried working around it, creating a custom Form field name in add.php:  
`echo $this->Form->control('new_product_name', ['type'=>'text','required' => false, 'placeholder' => 'If item is not yet in the list, add here']);`

and programming the bridge between product.id and product.name in the OrderController.

```
public function add()
    {
        $order = $this->Orders->newEmptyEntity();
        if ($this->request->is('post')) {
            $order = $this->Orders->patchEntity($order, $this->request->getData());
				// Changed: Set the user_id from the current user.
			$order->user_id = $this->request->getAttribute('identity')->getIdentifier();
			
			if($this->request->getData('new_product_name')){
			$product = $this->Products->newEmptyEntity();
			$product->product_name=$this->request->getData('new_product_name');
			$this->Products->save($product);
			$order->product_id=$product->product_id;
			}

```

However, `$product = $this->Products->newEmptyEntity();` gives the error newEmptyEntity can’t be called on null, even though the same is done correctly above with `$order = $this->Orders->newEmptyEntity();` . Has this to do with the fact I am in the OrderController at that point?

---

<div class="post-metadata">

**Author:** ![spadeX](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@spadeX](https://discourse.cakephp.org/u/spadeX)\
**Post date:** [May 7, 2020, 11:49am UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/7 "2020-05-07T11:49:22Z")

</div>

If I am correct then your problem solution is here

e.g. select category A to B if user need add new category in the dropdown the he enter into a input box before dropdown. and it appears into your dropdown box as selection. if it is ok then

well 1. **first form** submit save categories in category table with id  
2. **Second form** as per you requirement with your dropdown box and dropdown box show you category list as user enter values.

Note: **first form** submit by using JavaScript or ajax call form save the category so that it will give in category dropdown box value added.

Hope this solution help you.

---

<div class="post-metadata">

**Author:** ![HypJ3U50N](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/hypj3u50n/32/1803_2.png) [@HypJ3U50N](https://discourse.cakephp.org/u/HypJ3U50N)\
**Post date:** [May 7, 2020, 4:07pm UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/8 "2020-05-07T16:07:32Z")

</div>

I can come up with abstract solutions, but I have trouble with the explicit CakePHP coding to realize these logical reasonings.

---

<div class="post-metadata">

**Author:** ![dereuromark](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dereuromark/32/37_2.png) [@dereuromark](https://discourse.cakephp.org/u/dereuromark)\
**Post date:** [May 8, 2020, 8:09pm UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/9 "2020-05-08T20:09:47Z")

</div>

select2 and alike usually can over such a functionality.  
e.g. [https://sandbox.dereuromark.de/sandbox/tags/select](https://sandbox.dereuromark.de/sandbox/tags/select) (tags here are either existing or new)  
source code is open source.

using some select2 dropdown with the option of a manual string input should probably make this possible somehow also for your use case.

---

<div class="post-metadata">

**Author:** ![HypJ3U50N](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/hypj3u50n/32/1803_2.png) [@HypJ3U50N](https://discourse.cakephp.org/u/HypJ3U50N)\
**Post date:** [May 9, 2020, 4:02pm UTC](https://discourse.cakephp.org/t/form-with-dropdown-of-existing-and-add-new-entity-in-one/7729/10 "2020-05-09T16:02:42Z")

</div>

Seems promising! Sorry for basic dev questions, but I am lost what code to look for and where to implement it.

In your sandbox depos I get told to look in `/plugins/Sandbox/` : , but my best guest would be /[src](https://github.com/dereuromark/cakephp-sandbox/tree/master/plugins/Sandbox/src)/[View](https://github.com/dereuromark/cakephp-sandbox/tree/master/plugins/Sandbox/src/View)/[Helper](https://github.com/dereuromark/cakephp-sandbox/tree/master/plugins/Sandbox/src/View/Helper)/ **MediaEmbedBbcodeHelper.php** / and it is not what I am looking for.

At the other hand I hoped for either `app\vendor\cakephp\cakephp\src\Form.php` or `\tutorial_app\vendor\wyrihaximus\twig-view\templates\bake\element`, where the former should be the last place to edit anything, I suppose 🙂
