Cakephp 3 multiple custom template formhelpers

What this fix does is allow you to have custom template forms (from cakephp 3!!!!) using bootstrap. If you want to set sizes using the form helper and all of it's goodness (security and what not).

Jose Zap of Cakephp told me to try bootstrap plugins and widgets and what not, but the real way to do this should have been this:

Step 1: Create config/templatesConfig.php and add your custom form stuff.

<?php
$config = [
    'Templates'=>[
        'shortForm' => [
            'formStart' => '<form class="" {{attrs}}>',
            'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>',
            'input' => '<div class="col-md-4"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>',
            'select' => '<div class="col-md-4"><select name="{{name}}"{{attrs}}>{{content}}</select></div>',
            'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>',
            'checkContainer' => '',],
        'longForm' => [
            'formStart' => '<form class="" {{attrs}}>',
            'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>',
            'input' => '<div class="col-md-6"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>',
            'select' => '<div class="col-md-6"><select name="{{name}}"{{attrs}}>{{content}}</select></div>',
            'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>',
            'checkContainer' => '',],
        'fullForm' => [
            'formStart' => '<form class="" {{attrs}}>',
            'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>',
            'input' => '<div class="col-md-10"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>',
            'select' => '<div class="col-md-10"><select name="{{name}}"{{attrs}}>{{content}}</select></div>',
            'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>',
            'checkContainer' => '',]
    ]
];

Step 2: From your controller inside the method for the correct view call this line.

Don't forget add this on the top of your controller

use Cake\Core\Configure;

$this->set('form_templates', Configure::read('Templates'));

Step 3: Add this within the bootstrap.php file

// Load an environment local configuration file.
// You can use this file to provide local overrides to your
// shared configuration.

Configure::load('templatesConfig','default'); //fixed

Step 4(finally): Add this line with the template name you want Bam!.

<?php $this->Form->templates($form_templates['shortForm']); ?>

Let's say you need all inputs to use a custom markup in a form to show the label after the input (default is before) and a different class for the hardcoded error-message for errors:

$this->Form->create($entity, ['templates' => [
    'formGroup' => '{{input}}{{label}}',
    'error' => '<div class="error">{{content}}</div>'
]]);

If you want to only customize a single input, pass the same 'templates' key to the FormHelper::input() options like so:

$this->Form->input('fieldname', ['templates' => [
    'formGroup' => '{{input}}{{label}}',
    'error' => '<div class="error">{{content}}</div>'
]]);

If you need to define multiple templates and re-use them whenever you want, here's something you can try (mind I am writing it here, never used it before):

// in bootstrap (as this is a config i believe
Configure::write('templates', [
    'foo' => [....],
    'bar' => [....]
]);

// in any view
$this->Form->templates(Configure::read('templates.foo'));
$this->Form->create(...);
....

You could also create your own helper and define the templates there, etc.

It really all depends on what you want to achieve but that should give you a good understanding of how templates work (not just in forms by the way).