Last active
July 3, 2018 00:12
-
-
Save Suven/6325905 to your computer and use it in GitHub Desktop.
Bootstrap 3 FormHelper for CakePHP 2.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
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
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?