Skip to content
Permalink
Browse files

Allow FormHelper::input to pass options to ControlLabels (labels arou…

…nd Radio and MultiCheckbox)

    Added `optionsLabel`
Added tests
Fixes any tests broken by this

Only slight functionality change is if class was set via the direct methods (FormHelper::multiCheckbox or FormHelper::radio)
and the item is checked then 'selected' gets added. Previously it was skipped
  • Loading branch information...
lilHermit committed Jan 30, 2017
1 parent 2b81051 commit 9a7b8cf6b1584269ce9ee11749c3922466edb217
@@ -1008,7 +1008,8 @@ public function input($fieldName, array $options = [])
'required' => null,
'options' => null,
'templates' => [],
'templateVars' => []
'templateVars' => [],
'optionsLabel' => true
];
$options = $this->_parseOptions($fieldName, $options);
$options += ['id' => $this->_domId($fieldName)];
@@ -1038,6 +1039,9 @@ public function input($fieldName, array $options = [])
$label = $options['label'];
unset($options['label']);
$optionsLabel = $options['optionsLabel'];
unset($options['optionsLabel']);
$nestedInput = false;
if ($options['type'] === 'checkbox') {
$nestedInput = true;
@@ -1049,7 +1053,7 @@ public function input($fieldName, array $options = [])
$options['hiddenField'] = '_split';
}
$input = $this->_getInput($fieldName, $options);
$input = $this->_getInput($fieldName, $options + [ 'label' => $optionsLabel] );
if ($options['type'] === 'hidden' || $options['type'] === 'submit') {
if ($newTemplates) {
$templater->pop();
@@ -1850,7 +1854,7 @@ public function submit($caption = null, array $options = [])
$input = $this->formatTemplate('inputSubmit', [
'type' => $type,
'attrs' => $this->templater()->formatAttributes($options),
'attrs' => $this->templater()->formatAttributes($options, ['label']),
'templateVars' => $options['templateVars']
]);
@@ -76,7 +76,7 @@ public function render(array $data, ContextInterface $context)
'templateVars' => $data['templateVars'],
'attrs' => $this->_templates->formatAttributes(
$data,
['name', 'type']
['name', 'type', 'label']
),
]);
}
@@ -55,7 +55,7 @@ public function render(array $data, ContextInterface $context)
$attrs = $this->_templates->formatAttributes(
$data,
['name', 'value']
['name', 'value', 'label']
);
return $this->_templates->format('checkbox', [
@@ -65,7 +65,7 @@ public function render(array $data, ContextInterface $context)
'templateVars' => $data['templateVars'],
'attrs' => $this->_templates->formatAttributes(
$data,
['name']
['name', 'label']
)
]);
}
@@ -112,7 +112,8 @@ public function render(array $data, ContextInterface $context)
'disabled' => null,
'val' => null,
'idPrefix' => null,
'templateVars' => []
'templateVars' => [],
'label' => true
];
$this->_idPrefix = $data['idPrefix'];
$this->_clearIds();
@@ -152,6 +153,9 @@ protected function _renderInputs($data, $context)
if (!isset($checkbox['templateVars'])) {
$checkbox['templateVars'] = $data['templateVars'];
}
if (!isset($checkbox['label'])) {
$checkbox['label'] = $data['label'];
}
if (!empty($data['templateVars'])) {
$checkbox['templateVars'] = array_merge($data['templateVars'], $checkbox['templateVars']);
}
@@ -188,22 +192,39 @@ protected function _renderInput($checkbox, $context)
'templateVars' => $checkbox['templateVars'],
'attrs' => $this->_templates->formatAttributes(
$checkbox,
['name', 'value', 'text']
['name', 'value', 'text', 'label']
)
]);
$labelAttrs = [
'for' => $checkbox['id'],
'escape' => $checkbox['escape'],
'text' => $checkbox['text'],
'templateVars' => $checkbox['templateVars'],
'input' => $input,
];
if (!empty($checkbox['checked']) && empty($labelAttrs['class'])) {
$labelAttrs['class'] = 'selected';
}
$label = $this->_label->render($labelAttrs, $context);
if ($checkbox['label'] === false && strpos($this->_templates->get('radioWrapper'), '{{input}}') === false) {
$label = $input;
} else {
$labelAttrs = [
'for' => $checkbox['id'],
'escape' => $checkbox['escape'],
'text' => $checkbox['text'],
'templateVars' => $checkbox['templateVars'],
'input' => $input
];
if(is_array($checkbox['label'])) {
$labelAttrs += $checkbox['label'];
}
if (isset($checkbox['checked']) && $checkbox['checked']) {
if(isset($labelAttrs['class'])) {
if (is_array($labelAttrs['class'])) {
$labelAttrs['class'][] = 'selected';
} else if(is_string($labelAttrs['class'])) {
$labelAttrs['class'] .= ' selected';
}
} else {
$labelAttrs['class'] = 'selected';
}
}
$label = $this->_label->render($labelAttrs, $context);
}
return $this->_templates->format('checkboxWrapper', [
'templateVars' => $checkbox['templateVars'],
'label' => $label,
@@ -173,6 +173,28 @@ protected function _renderInput($val, $text, $data, $context)
if (isset($data['val']) && (string)$data['val'] === (string)$radio['value']) {
$radio['checked'] = true;
}
if (!is_bool($data['label'])) {
if (isset($data['label']['class']) && is_string($data['label']['class'])) {
$data['label']['class'] = explode(' ', $data['label']['class']);
}
if (!isset($data['label']['class']) || !is_array($data['label']['class'])) {
$data['label']['class'] = [];
}
if (isset($radio['checked']) && $radio['checked']) {
if(isset($data['label']['class'])) {
if (is_array($data['label']['class'])) {
$data['label']['class'][] = 'selected';
} else if(is_string($data['label']['class'])) {
$data['label']['class'] .= ' selected';
}
} else {
$data['label']['class'] = 'selected';
}
}
}
if ($this->_isDisabled($radio, $data['disabled'])) {
$radio['disabled'] = true;
}
@@ -125,7 +125,7 @@ public function render(array $data, ContextInterface $context)
$template = 'selectMultiple';
unset($data['multiple']);
}
$attrs = $this->_templates->formatAttributes($data);
$attrs = $this->_templates->formatAttributes($data, ['label']);
return $this->_templates->format($template, [
'name' => $name,
@@ -55,7 +55,7 @@ public function render(array $data, ContextInterface $context)
'templateVars' => $data['templateVars'],
'attrs' => $this->_templates->formatAttributes(
$data,
['name', 'val']
['name', 'val', 'label']
)
]);
}
Oops, something went wrong.

0 comments on commit 9a7b8cf

Please sign in to comment.
You can’t perform that action at this time.