Cakephp 3.0 change or remove wrapping div on input form

jpruizs picture jpruizs · Apr 16, 2015 · Viewed 18.1k times · Source

I am trying to remove or change the wrapping div that CakePHP uses on its form helper.

When I use this code:

 echo $this->Form->input('contact', ['label' => false]);

The output is:

<div class="input text">
  <input type="text" id="contact" maxlength="255" name="contact">
</div>

And what I want is:

<div class="myOwnClass">
  <input type="text" id="contact" maxlength="255" name="contact">
</div>

I used to do that on CakePHP 2 adding more options to the input method, however on the latest CakePHP version this isn't working. Any clues?

Thanks

Answer

user1418909 picture user1418909 · Apr 18, 2015

Use FormHelper Templates

To change wrapping for all inputs in form use:

$this->Form->templates([
    'inputContainer' => '<div class="myOwnClass">{{content}}</div>'
]);
// or remove completely
$this->Form->templates([
    'inputContainer' => '{{content}}'
]);
// now get input with desired wrapping
echo $this->Form->input('contact', [
    'label' => false
]);

To change wrapping for single input use:

echo $this->Form->input('contact', [
    'templates' => [
        'inputContainer' => '<div class="myOwnClass">{{content}}</div>'
    ],
    'label' => false
]);

For complete reference on templates read: Customizing the Templates FormHelper Uses

CakePHP 2 style of customizing the wrappings is not supported anymore in version 3. From migration guide:

The div, before, after, between and errorMessage options have been removed from input(). You can use templates to update the wrapping HTML. The templates option allows you to override the loaded templates for one input.