Skip to content

Commit

Permalink
radio bugfixes & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
steefaan committed Jun 14, 2015
1 parent 308cb90 commit ac50f73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class FormHelper extends Helper
'inputContainer' => '<div class="form-group{{required}}">{{content}}{{help}}</div>',
'inputContainerError' => '<div class="form-group{{required}} has-error">{{content}}{{error}}{{help}}</div>',
'checkboxWrapper' => '<div class="checkbox"><label>{{input}}{{label}}</label></div>',
'radioFormGroup' => '{{input}}',
'staticControl' => '<p class="form-control-static">{{content}}</p>'
];

Expand All @@ -52,7 +51,6 @@ class FormHelper extends Helper
'label' => '<label class="control-label %s"{{attrs}}>{{text}}</label>',
'formGroup' => '{{label}}<div class="%s">{{input}}{{error}}{{help}}</div>',
'checkboxFormGroup' => '<div class="%s"><div class="checkbox">{{label}}</div>{{error}}{{help}}</div>',
'radioFormGroup' => '<div class="%s"><div class="radio">{{label}}</div>{{error}}{{help}}</div>',
'submitContainer' => '<div class="%s">{{content}}</div>',
'inputContainer' => '<div class="form-group{{required}}">{{content}}</div>',
'inputContainerError' => '<div class="form-group{{required}} has-error">{{content}}</div>',
Expand Down Expand Up @@ -161,7 +159,6 @@ public function input($fieldName, array $options = [])

switch ($options['type']) {
case 'checkbox':
case 'radio':
if (!isset($options['inline'])) {
$options['inline'] = $this->checkClasses($options['type'] . '-inline', (array)$options['label']);
}
Expand All @@ -177,6 +174,7 @@ public function input($fieldName, array $options = [])
$options['type'] = 'multicheckbox';
}
break;
case 'radio':
case 'multiselect':
case 'textarea':
break;
Expand Down Expand Up @@ -385,7 +383,7 @@ protected function _formAlignment($options)

$templates['label'] = sprintf($templates['label'], $this->_gridClass('left'));
$templates['formGroup'] = sprintf($templates['formGroup'], $this->_gridClass('middle'));
foreach (['checkboxFormGroup', 'radioFormGroup', 'submitContainer'] as $value) {
foreach (['checkboxFormGroup', 'submitContainer'] as $value) {
$templates[$value] = sprintf($templates[$value], $offsetedGridClass);
}

Expand Down
48 changes: 43 additions & 5 deletions src/View/Widget/RadioWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

class RadioWidget extends \Cake\View\Widget\RadioWidget
{

/**
* Set on `RadioWidget::render()` to tell `RadioWidget::_renderLabel()`
* that we want to have inline aligned radios.
*
* @var bool
*/
protected $_inline = false;

/**
* Render a set of radio buttons.
*
Expand Down Expand Up @@ -32,20 +41,49 @@ public function render(array $data, ContextInterface $context)
'inline' => false,
];

$templates = $this->_templates->get();
$customize = [];

if ($data['inline']) {
$this->_templates->add(['radioContainer' => '{{content}}']);
$this->_inline = $data['inline'];
if ($templates['formGroup'] === '{{label}}{{input}}') {
$this->_templates->add(['radioFormGroup' => '{{label}}<div class="radio-inline-wrapper">{{input}}</div>']);
}
}


$templates = $this->_templates->get();
$customize = [];
if ($templates['nestingLabel'] === '{{hidden}}<label{{attrs}}>{{input}}{{text}}</label>') {
if (!$data['inline'] && $templates['nestingLabel'] === '{{hidden}}<label{{attrs}}>{{input}}{{text}}</label>') {
$customize['nestingLabel'] = '<div class="radio">' . $templates['nestingLabel'] . '</div>';
}

$this->_templates->add($customize);

unset($data['inline']);

return parent::render($data, $context);
}

/**
* Renders a label element for a given radio button.
*
* In the future this might be refactored into a separate widget as other
* input types (multi-checkboxes) will also need labels generated.
*
* @param array $radio The input properties.
* @param false|string|array $label The properties for a label.
* @param string $input The input widget.
* @param \Cake\View\Form\ContextInterface $context The form context.
* @param bool $escape Whether or not to HTML escape the label.
* @return string Generated label.
*/
protected function _renderLabel($radio, $label, $input, $context, $escape)
{
if ($this->_inline) {
$label = [
'text' => $radio['text'],
'class' => 'radio-inline'
];
}
return parent::_renderLabel($radio, $label, $input, $context, $escape);
}

}

0 comments on commit ac50f73

Please sign in to comment.