Search code examples
phphtmlcakephpform-helpers

FormHelper::input() creates a drop-down select if fielname has the "_id" suffix


I have the following line of code on a CakePHP view:

<?php
echo $this->Form->input(
  'person_id',
  array(
    'label' => false,
    'div' => false,
    'class' => 'form-control search-person'
  )
);
?>

I want to create a text input with this line of code, but if the field name has the suffix _id, the rendered HTML changes from a text field to a drop-down select.

If I change the prefix to anything else, for example person_idd or abc_idd, it renders a text input, but if the field name ends with _id suffix, it renders a drop-down select, which doesn't allow me to write anything.

Is this some CakePHP feature? How can I avoid this behavior and produce a text input with a field ending with the _id suffix?


Solution

  • It's a CakePHP feature:

    This method will automatically inspect the model field it has been supplied in order to create an appropriate input for that field.

    Taken from Cookbook 2.x: FormHelper: Creating form elements.

    To get a text input, add 'type' => 'text' to the options array:

    <?php echo $this->Form->input('person_id', array(
        'type' => 'text',
        'label' => false, 
        'div' => false, 
        'class' => 'form-control search-person'
    )); ?>