Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bootstrap 3 FormHelper for CakePHP 2.x
<?php
App::uses('FormHelper', 'View/Helper');
/**
* BootstrapFormHelper.
*
* Applies styling-rules for Bootstrap 3
*
* To use it, just save this file in /app/View/Helper/BootstrapFormHelper.php
* and add the following code to your AppController:
* public $helpers = array(
* 'Form' => array(
* 'className' => 'BootstrapForm'
* )
* );
*
* @link https://gist.github.com/Suven/6325905
*/
class BootstrapFormHelper extends FormHelper {
public function create($model = null, $options = array()) {
$defaultOptions = array(
'inputDefaults' => array(
'div' => array(
'class' => 'form-group'
),
'label' => array(
'class' => 'col-lg-2 control-label'
),
'between' => '<div class="col-lg-10">',
'seperator' => '</div>',
'after' => '</div>',
'class' => 'form-control',
),
'class' => 'form-horizontal',
'role' => 'form',
);
if(!empty($options['inputDefaults'])) {
$options = array_merge($defaultOptions['inputDefaults'], $options['inputDefaults']);
} else {
$options = array_merge($defaultOptions, $options);
}
return parent::create($model, $options);
}
// Remove this function to show the fieldset & language again
public function inputs($fields = null, $blacklist = null, $options = array()) {
$options = array_merge(array('fieldset' => false), $options);
return parent::inputs($fields, $blacklist, $options);
}
public function submit($caption = null, $options = array()) {
$defaultOptions = array(
'class' => 'btn btn-primary',
'div' => 'form-group',
'before' => '<div class="col-lg-offset-2 col-lg-10">',
'after' => '</div>',
);
$options = array_merge($defaultOptions, $options);
return parent::submit($caption, $options);
}
}
@ghost

This comment has been minimized.

Copy link

ghost commented Nov 8, 2013

Great little Helper and although i'm (very) new to Cake, shouldn't line 31's 'seperator' => '', be 'separator? This is currently being placed in the opening tag of my inputs but if i were to remove zed line, what impact would that have?

@Casmo

This comment has been minimized.

Copy link

Casmo commented Jan 10, 2014

Nice helper. I added a small patch that keeps the default class for labels if this is overwritten in Form->input();.
See https://gist.github.com/Casmo/8349245#file-bootstrapformhelper-php-L64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.