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

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:

    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:

and result in this data back to your program:

[
	'input' => ''
]

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

This code:

    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:

Would get you this data back to the program:

[
	'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.

1 Like