Skip to content

Commit eb8f8f0

Browse files
authored
Merge pull request FriendsOfCake#175 from FriendsOfCake/master-fix-control
Fix new 3.4 control() to work properly.
2 parents 54274c0 + 71cdeda commit eb8f8f0

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/View/Helper/FormHelper.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ class FormHelper extends Helper
2020
*/
2121
protected $_align;
2222

23+
/**
24+
* The method to use when creating a control element for a form
25+
*
26+
* @var string
27+
*/
28+
protected $_controlMethod = 'input';
29+
2330
/**
2431
* Default Bootstrap string templates.
2532
*
@@ -102,6 +109,10 @@ public function __construct(View $View, array $config = [])
102109

103110
$this->_defaultWidgets = $this->_widgets + $this->_defaultWidgets;
104111

112+
if (method_exists('Cake\View\Helper\FormHelper', 'control')) {
113+
$this->_controlMethod = 'control';
114+
}
115+
105116
parent::__construct($View, $config);
106117
}
107118

@@ -169,8 +180,27 @@ public function submit($caption = null, array $options = [])
169180
* @param string $fieldName This should be "Modelname.fieldname".
170181
* @param array $options Each type of input takes different options.
171182
* @return string Completed form widget.
183+
* @deprecated Use control() instead.
172184
*/
173185
public function input($fieldName, array $options = [])
186+
{
187+
return $this->control($fieldName, $options);
188+
}
189+
190+
/**
191+
* Generates a form input element complete with label and wrapper div.
192+
*
193+
* Adds extra option besides the ones supported by parent class method:
194+
* - `append` - Append addon to input.
195+
* - `prepend` - Prepend addon to input.
196+
* - `inline` - Boolean for generating inline checkbox/radio.
197+
* - `help` - Help text of include in the input container.
198+
*
199+
* @param string $fieldName This should be "Modelname.fieldname".
200+
* @param array $options Each type of input takes different options.
201+
* @return string Completed form widget.
202+
*/
203+
public function control($fieldName, array $options = [])
174204
{
175205
$options += [
176206
'prepend' => null,
@@ -246,7 +276,9 @@ public function input($fieldName, array $options = [])
246276
);
247277
}
248278

249-
$result = parent::input($fieldName, $options);
279+
$controlMethod = $this->_controlMethod;
280+
$result = parent::$controlMethod($fieldName, $options);
281+
250282
if ($newTemplates) {
251283
$this->templater()->pop();
252284
}

tests/TestCase/View/Helper/FormHelperTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ public function testBasicTextInput()
9696
$this->assertHtml($expected, $result);
9797
}
9898

99+
public function testBasicTextControl()
100+
{
101+
unset($this->article['required']['title']);
102+
$this->Form->create($this->article);
103+
104+
$result = $this->Form->control('title');
105+
$expected = [
106+
'div' => ['class' => 'form-group text'],
107+
'label' => ['class' => 'control-label', 'for' => 'title'],
108+
'Title',
109+
'/label',
110+
'input' => [
111+
'type' => 'text',
112+
'name' => 'title',
113+
'id' => 'title',
114+
'class' => 'form-control',
115+
],
116+
'/div'
117+
];
118+
$this->assertHtml($expected, $result);
119+
}
120+
99121
public function testSelectInput()
100122
{
101123
$this->Form->create($this->article);

0 commit comments

Comments
 (0)