Skip to content

Commit 4d98010

Browse files
authored
Merge pull request #397 from FriendsOfCake/5.x-fix-standalone-control-call
5.x - Fix type error when using `control()` outside of an open form.
2 parents 32105f2 + e884c92 commit 4d98010

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/View/Helper/FormHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ protected function _containerOptions(?string $fieldName, array $options): array
644644
if (
645645
$this->_align !== static::ALIGN_INLINE &&
646646
isset($options['type']) &&
647+
isset($options['spacing']) &&
647648
$options['spacing'] !== false
648649
) {
649650
$options['container'] = $this->injectClasses($options['spacing'], (array)($options['container'] ?? []));

tests/TestCase/View/Helper/FormHelperTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,49 @@ public function testTooltipWithDisabledLabel()
929929
$this->assertHtml($expected, $result);
930930
}
931931

932+
/**
933+
* Tests that the `control()` method can be used outside of an open form.
934+
*
935+
* This should result in controls being created with the default templates,
936+
* and without alignment/grid, and spacing related classes and structures.
937+
*
938+
* @return void
939+
*/
940+
public function testFormControlWithoutOpenForm()
941+
{
942+
$Form = new FormHelper($this->View);
943+
944+
$result = $Form->control('title');
945+
$expected = [
946+
['div' => ['class' => 'form-group text']],
947+
['label' => ['class' => 'form-label', 'for' => 'title']],
948+
'Title',
949+
'/label',
950+
'input' => [
951+
'type' => 'text',
952+
'name' => 'title',
953+
'id' => 'title',
954+
'class' => 'form-control',
955+
],
956+
'/div',
957+
];
958+
$this->assertHtml($expected, $result);
959+
960+
$Form->create($this->article, [
961+
'align' => [
962+
'sm' => [
963+
FormHelper::GRID_COLUMN_ONE => 5,
964+
FormHelper::GRID_COLUMN_TWO => 7,
965+
],
966+
],
967+
'spacing' => 'mb-1',
968+
]);
969+
$Form->end();
970+
971+
$result = $Form->control('title');
972+
$this->assertHtml($expected, $result);
973+
}
974+
932975
/**
933976
* Test that "form-*" classes are added when using methods for specific input.
934977
*

0 commit comments

Comments
 (0)