Created
July 28, 2014 12:46
-
-
Save commercial-hippie/4b17b972d3bc969014b6 to your computer and use it in GitHub Desktop.
Chocolate
This file contains hidden or 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 | |
| namespace Chocolate\View\Helper; | |
| use \Cake\View\Helper\FormHelper; | |
| use \Cake\View\View; | |
| class BootstrapFormHelper extends FormHelper { | |
| protected $_defaultConfig = [ | |
| 'errorClass' => 'form-error', | |
| 'typeMap' => [ | |
| 'string' => 'text', 'datetime' => 'datetime', 'boolean' => 'checkbox', | |
| 'timestamp' => 'datetime', 'text' => 'textarea', 'time' => 'time', | |
| 'date' => 'date', 'float' => 'number', 'integer' => 'number', | |
| 'decimal' => 'number', 'binary' => 'file', 'uuid' => 'string' | |
| ], | |
| 'widgets' => [], | |
| 'registry' => null, | |
| 'templates' => [ | |
| 'button' => '<button{{attrs}}>{{text}}</button>', | |
| 'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>', | |
| 'checkboxFormGroup' => '{{input}}{{label}}', | |
| 'checkboxWrapper' => '<div class="checkbox">{{input}}{{label}}</div>', | |
| 'errorList' => '<ul>{{content}}</ul>', | |
| 'errorItem' => '<li>{{text}}</li>', | |
| 'file' => '<input type="file" name="{{name}}"{{attrs}}>', | |
| 'fieldset' => '<fieldset>{{content}}</fieldset>', | |
| 'formstart' => '<form{{attrs}}>', | |
| 'formend' => '</form>', | |
| 'formGroup' => '{{label}}{{input}}', | |
| 'hiddenblock' => '<div style="display:none;">{{content}}</div>', | |
| 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>', | |
| 'inputsubmit' => '<input type="{{type}}"{{attrs}}>', | |
| 'label' => '<label{{attrs}}>{{text}}</label>', | |
| 'legend' => '<legend>{{text}}</legend>', | |
| 'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>', | |
| 'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>', | |
| 'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>', | |
| 'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>', | |
| 'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>', | |
| 'radioWrapper' => '{{input}}{{label}}', | |
| 'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>', | |
| 'dateWidget' => '<div class="row"> | |
| <div class="col-sm-3">{{year}}</div> | |
| <div class="col-sm-3">{{month}}</div> | |
| <div class="col-sm-3">{{day}}</div> | |
| <div class="col-sm-3">{{hour}}</div> | |
| <div class="col-sm-3">{{minute}}</div> | |
| <div class="col-sm-3">{{second}}</div> | |
| <div class="col-sm-3">{{meridian}}</div> | |
| </div>', | |
| 'error' => '<div class="help-block">{{content}}</div>', | |
| 'submitContainer' => '{{content}}', | |
| 'inputContainer' => '<div class="form-group {{type}}{{required}}">{{content}}</div>', | |
| 'inputContainerError' => '<div class="form-group has-error has-feedback {{type}}{{required}}">{{content}}<span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>{{error}}</div>', | |
| ] | |
| ]; | |
| public function __construct(View $view, $config = []) { | |
| parent::__construct($view, $config); | |
| } | |
| public function label($fieldName, $text = null, array $options = []) { | |
| $options = $this->addClass($options, 'control-label'); | |
| return parent::label($fieldName, $text, $options); | |
| } | |
| protected function _getInput($fieldName, $options) | |
| { | |
| if (isset($options['type']) && !in_array($options['type'], ['radio', 'checkbox', 'datetime'])) | |
| { | |
| $options = $this->addClass($options, 'form-control'); | |
| } | |
| return parent::_getInput($fieldName, $options); | |
| } | |
| public function submit($caption = null, array $options = []) | |
| { | |
| $options = $this->addClass($options, 'btn btn-default'); | |
| return parent::submit($caption, $options); | |
| } | |
| public function button($title, array $options = array()) | |
| { | |
| $options = $this->addClass($options, 'btn btn-default'); | |
| return parent::button($title, $options); | |
| } | |
| } |
This file contains hidden or 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
| // AppController | |
| public $helpers = [ | |
| 'Session', | |
| 'Tools', | |
| 'Form' => [ | |
| 'className' => 'Chocolate.BootstrapForm' | |
| ], | |
| 'Html', | |
| 'Flash', | |
| ]; | |
| // Bootstrap | |
| Plugin::load('Chocolate'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment