Skip to content

Instantly share code, notes, and snippets.

@Casmo
Forked from Suven/BootstrapFormHelper.php
Last active November 25, 2018 22:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Casmo/8349245 to your computer and use it in GitHub Desktop.
Save Casmo/8349245 to your computer and use it in GitHub Desktop.
Keep default class for labels.
<?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);
}
public function input($fieldName, $options = array()) {
if (isset($options['label']) && is_string($options['label'])) {
$option['text'] = $options['label'];
$options['label'] = array_merge($option, $this->_inputDefaults['label']);
}
else if (isset($options['label']['text']) && !isset($options['label']['class'])) {
$options['label'] = array_merge($options['label'], $this->_inputDefaults['label']);
}
return parent::input($fieldName, $options);
}
}
@samayo
Copy link

samayo commented Feb 23, 2016

I am just starting with cakephp2.8. Is there any resource that explains how to integrate/use this? All I did was drop your file with .php ext and a folder with the same name in app/plugin as app/plugin/bootstrapformhelper/bootstrapformhelper.php and I am getting error saying there is not view for this. I don't know why it needs a view though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment