Skip to content

Commit aa5c548

Browse files
author
Mikaël Capelle
committed
Update FormHelper to use additional template variables for horizontal form instead of custom templates. Remove useless space from templates.
1 parent 45d6989 commit aa5c548

File tree

3 files changed

+67
-52
lines changed

3 files changed

+67
-52
lines changed

src/View/BootstrapStringTemplate.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ public function format($name, array $data)
5252
&& in_array('attrs.'.substr($placeholder, 6), $placeholders)
5353
&& preg_match('#'.substr($placeholder, 6).'="([^"]+)"#', $data['attrs'], $matches) > 0) {
5454
$data['attrs'] = preg_replace('#'.substr($placeholder, 6).'="[^"]+"#', '', $data['attrs']);
55-
$data[$placeholder] = $matches[1];
55+
$data[$placeholder] = ' '.trim($matches[1]);
5656
}
5757
}
58+
$data['attrs'] = ' '.trim($data['attrs']);
5859
}
5960
if ($template === null) {
6061
return '';

src/View/Helper/BootstrapFormHelper.php

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,33 @@ class BootstrapFormHelper extends FormHelper {
5454
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
5555
'checkboxFormGroup' => '{{label}}',
5656
'checkboxWrapper' => '<div class="checkbox">{{label}}</div>',
57-
'checkboxContainer' => '<div class="checkbox">{{content}}</div>',
57+
'checkboxContainer' => '{{h_checkboxContainer_start}}<div class="checkbox">{{content}}</div>{{h_checkboxContainer_end}}',
5858
'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}',
59-
'error' => '<span class="help-block error-message">{{content}}</span>',
59+
'error' => '<span class="help-block error-message{{h_errorClass}}">{{content}}</span>',
6060
'errorList' => '<ul>{{content}}</ul>',
6161
'errorItem' => '<li>{{text}}</li>',
6262
'file' => '<input type="file" name="{{name}}" {{attrs}}>',
6363
'fieldset' => '<fieldset{{attrs}}>{{content}}</fieldset>',
6464
'formStart' => '<form{{attrs}}>',
6565
'formEnd' => '</form>',
66-
'formGroup' => '{{label}}{{prepend}}{{input}}{{append}}',
66+
'formGroup' => '{{label}}{{h_formGroup_start}}{{prepend}}{{input}}{{append}}{{h_formGroup_end}}',
6767
'hiddenBlock' => '<div style="display:none;">{{content}}</div>',
68-
'input' => '<input type="{{type}}" name="{{name}}" class="form-control {{attrs.class}}" {{attrs}} />',
68+
'input' => '<input type="{{type}}" name="{{name}}" class="form-control{{attrs.class}}" {{attrs}} />',
6969
'inputSubmit' => '<input type="{{type}}"{{attrs}}>',
7070
'inputContainer' => '<div class="form-group {{type}}{{required}}">{{content}}</div>',
7171
'inputContainerError' => '<div class="form-group has-error {{type}}{{required}}">{{content}}{{error}}</div>',
72-
'label' => '<label class="control-label {{attrs.class}}" {{attrs}}>{{text}}</label>',
72+
'label' => '<label class="{{s_labelClass}}{{h_labelClass}}{{attrs.class}}" {{attrs}}>{{text}}</label>',
7373
'nestingLabel' => '{{hidden}}<label{{attrs}}>{{input}}{{text}}</label>',
7474
'legend' => '<legend>{{text}}</legend>',
7575
'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
7676
'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
77-
'select' => '<select name="{{name}}" class="form-control {{attrs.class}}" {{attrs}}>{{content}}</select>',
78-
'selectMultiple' => '<select name="{{name}}[]" multiple="multiple" class="form-control {{attrs.class}}" {{attrs}}>{{content}}</select>',
77+
'select' => '<select name="{{name}}" class="form-control{{attrs.class}}" {{attrs}}>{{content}}</select>',
78+
'selectMultiple' => '<select name="{{name}}[]" multiple="multiple" class="form-control{{attrs.class}}" {{attrs}}>{{content}}</select>',
7979
'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
8080
'radioWrapper' => '<div class="radio">{{label}}</div>',
81-
'radioContainer' => '<div class="form-group">{{content}}</div>',
82-
'textarea' => '<textarea name="{{name}}" class="form-control {{attrs.class}}" {{attrs}}>{{value}}</textarea>',
83-
'submitContainer' => '<div class="form-group">{{content}}</div>',
81+
'radioContainer' => '{{h_radioContainer_start}}<div class="form-group">{{content}}</div>{{h_radioContainer_end}}',
82+
'textarea' => '<textarea name="{{name}}" class="form-control{{attrs.class}}" {{attrs}}>{{value}}</textarea>',
83+
'submitContainer' => '<div class="form-group">{{h_submitContainer_start}}{{content}}{{h_submitContainer_end}}</div>',
8484
]
8585
];
8686

@@ -129,8 +129,8 @@ class BootstrapFormHelper extends FormHelper {
129129
*/
130130
protected $_defaultColumnSize = [
131131
'label' => 2,
132-
'input' => 6,
133-
'error' => 4
132+
'input' => 10,
133+
'error' => 0
134134
];
135135

136136
private $buttonTypes = ['default', 'primary', 'info', 'success', 'warning', 'danger', 'link'] ;
@@ -185,27 +185,50 @@ protected function _wrapTemplates ($templates, $callback, $params) {
185185
protected function _matchButton ($html) {
186186
return strpos($html, '<button') !== FALSE || strpos($html, 'type="submit"') !== FALSE ;
187187
}
188-
189-
/**
190-
*
191-
* Set the default templates according to the inner properties of the form ($this->horizontal and $this->inline).
192-
*
193-
**/
194-
protected function _setDefaultTemplates () {
195-
$this->templates([
196-
'formGroup' => '{{label}}'.($this->horizontal ? '<div class="'.$this->_getColClass('input').'">' : '').'{{prepend}}{{input}}{{append}}'.($this->horizontal ? '</div>' : ''),
197-
'checkboxContainer' => ($this->horizontal ? '<div class="form-group"><div class="'.$this->_getColClass('label', true).' '.$this->_getColClass('input').'">' : '')
198-
.'<div class="checkbox">{{content}}</div>'
199-
.($this->horizontal ? '</div></div>' : ''),
200-
'radioContainer' => ($this->horizontal ? '<div class="form-group"><div class="'.$this->_getColClass('label', true).' '.$this->_getColClass('input').'">' : '')
201-
.'{{content}}'
202-
.($this->horizontal ? '</div></div>' : ''),
203-
'label' => '<label class="'.($this->horizontal ? $this->_getColClass('label') : '').' '.($this->inline ? 'sr-only' : 'control-label').' {{attrs.class}}" {{attrs}}>{{text}}</label>',
204-
'error' => '<span class="help-block '.($this->horizontal ? $this->_getColClass('error') : '').' error-message">{{content}}</span>',
205-
'submitContainer' => '<div class="form-group">'.($this->horizontal ? '<div class="'.$this->_getColClass('label', true).' '.$this->_getColClass('input').'">' : '').'{{content}}'.($this->horizontal ? '</div>' : '').'</div>',
206-
]) ;
188+
189+
protected function _getDefaultTemplateVars (&$options) {
190+
$options += [
191+
'templateVars' => []
192+
];
193+
$options['templateVars'] += [
194+
's_labelClass' => 'control-label'
195+
];
196+
if ($this->horizontal) {
197+
$options['templateVars'] += [
198+
'h_formGroup_start' => '<div class="'.$this->_getColClass('input').'">',
199+
'h_formGroup_end' => '</div>',
200+
'h_checkboxContainer_start' => '<div class="form-group"><div class="'.$this->_getColClass('label', true)
201+
.' '.$this->_getColClass('input').'">',
202+
'h_checkboxContainer_end' => '</div></div>',
203+
'h_radioContainer_start' => '<div class="form-group"><div class="'.$this->_getColClass('label', true)
204+
.' '.$this->_getColClass('input').'">',
205+
'h_radioContainer_end' => '</div></div>',
206+
'h_submitContainer_start' => '<div class="'.$this->_getColClass('label', true).' '.$this->_getColClass('input').'">',
207+
'h_submitContainer_end' => '</div>',
208+
'h_labelClass' => ' '.$this->_getColClass('label'),
209+
'h_errorClass' => ' '.$this->_getColClass('error')
210+
];
211+
}
212+
if ($this->inline) {
213+
$options['templateVars']['s_labelClass'] = 'sr-only';
214+
}
215+
return $options;
207216
}
208-
217+
218+
public function formatTemplate($name, $data) {
219+
return $this->templater()->format($name, $this->_getDefaultTemplateVars($data));
220+
}
221+
222+
public function widget($name, array $data = []) {
223+
return parent::widget($name, $this->_getDefaultTemplateVars($data));
224+
}
225+
226+
protected function _inputContainerTemplate($options) {
227+
return parent::_inputContainerTemplate(array_merge($options, [
228+
'options' => $this->_getDefaultTemplateVars($options['options'])
229+
]));
230+
}
231+
209232
/**
210233
*
211234
* Create a Twitter Bootstrap like form.
@@ -248,27 +271,18 @@ public function create($model = null, Array $options = array()) {
248271
$options = $this->addClass($options, 'form-search') ;
249272
}
250273
$options['role'] = 'form' ;
251-
$this->_setDefaultTemplates () ;
252274
return parent::create($model, $options) ;
253275
}
254276

255-
/**
256-
*
257-
* Switch horizontal mode on or off.
258-
*
259-
**/
260-
public function setHorizontal ($horizontal) {
261-
$this->horizontal = $horizontal ;
262-
$this->_setDefaultTemplates () ;
263-
}
264-
265-
266277
/**
267278
*
268279
* Return the col size class for the specified column (label, input or error).
269280
*
270281
**/
271282
protected function _getColClass ($what, $offset = false) {
283+
if ($what === 'error' && isset($this->colSize['error']) && $this->colSize['error'] == 0) {
284+
return $this->_getColClass('label', true).' '.$this->_getColClass('input');
285+
}
272286
if (isset($this->colSize[$what])) {
273287
return 'col-md-'.($offset ? 'offset-' : '').$this->colSize[$what] ;
274288
}
@@ -331,7 +345,7 @@ protected function _wrap ($input, $prepend, $append) {
331345
*
332346
**/
333347
public function input($fieldName, array $options = array()) {
334-
348+
335349
$options = $this->_parseOptions($fieldName, $options);
336350

337351
$prepend = $this->_extractOption('prepend', $options, false) ;
@@ -356,7 +370,7 @@ public function input($fieldName, array $options = array()) {
356370
$options['templates'] = [] ;
357371
if ($inline) {
358372
$options['templates'] = [
359-
'label' => $this->templates('label').'<div></div>',
373+
'label' => $this->templates('label'),
360374
'radioWrapper' => '{{label}}',
361375
'nestingLabel' => '{{hidden}}<label{{attrs}} class="radio-inline">{{input}}{{text}}</label>'
362376
] ;
@@ -373,7 +387,7 @@ public function input($fieldName, array $options = array()) {
373387
'prepend' => $prepend,
374388
'append' => $append
375389
];
376-
390+
377391
return parent::input($fieldName, $options) ;
378392
}
379393

@@ -397,7 +411,7 @@ protected function _groupTemplate($options) {
397411
$data = array_merge($data, $options['options']['_data']);
398412
unset($options['options']['_data']);
399413
}
400-
return $this->templater()->format($groupTemplate, $data);
414+
return $this->formatTemplate($groupTemplate, $data);
401415
}
402416

403417
/**

src/View/Helper/BootstrapHtmlHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function progress ($widths, $options = []) {
251251
* Create & return a twitter bootstrap dropdown menu.
252252
*
253253
* @param $menu HTML tags corresponding to menu options (which will be wrapped
254-
* into <li> tag). To add separator, pass 'divider'.
254+
* into <li> tag). To add separator, pass 'divider'.
255255
* @param $options Attributes for the wrapper (change it with tag)
256256
*
257257
*/
@@ -281,8 +281,8 @@ public function dropdown (array $menu = [], array $options = []) {
281281
}
282282
}
283283
$options = $this->addClass($options, 'dropdown-menu');
284-
$options['role'] = 'menu' ;
285-
$options += ['tag' => 'div'];
284+
$options['role'] = 'menu';
285+
$options += ['tag' => 'ul'];
286286
$tag = $options['tag'];
287287
unset($options['tag']);
288288
return $this->tag($tag, $output, $options) ;

0 commit comments

Comments
 (0)