From e884c924b0b0ab270914afe46a3189893a3cdc95 Mon Sep 17 00:00:00 2001 From: ndm2 Date: Sun, 12 Nov 2023 13:48:02 +0100 Subject: [PATCH] Fix type error when using `control()` outside of an open form. --- src/View/Helper/FormHelper.php | 1 + tests/TestCase/View/Helper/FormHelperTest.php | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 7c29bb0d..b7d5c6ab 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -644,6 +644,7 @@ protected function _containerOptions(?string $fieldName, array $options): array if ( $this->_align !== static::ALIGN_INLINE && isset($options['type']) && + isset($options['spacing']) && $options['spacing'] !== false ) { $options['container'] = $this->injectClasses($options['spacing'], (array)($options['container'] ?? [])); diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index 92ff4bfa..0996be03 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -929,6 +929,49 @@ public function testTooltipWithDisabledLabel() $this->assertHtml($expected, $result); } + /** + * Tests that the `control()` method can be used outside of an open form. + * + * This should result in controls being created with the default templates, + * and without alignment/grid, and spacing related classes and structures. + * + * @return void + */ + public function testFormControlWithoutOpenForm() + { + $Form = new FormHelper($this->View); + + $result = $Form->control('title'); + $expected = [ + ['div' => ['class' => 'form-group text']], + ['label' => ['class' => 'form-label', 'for' => 'title']], + 'Title', + '/label', + 'input' => [ + 'type' => 'text', + 'name' => 'title', + 'id' => 'title', + 'class' => 'form-control', + ], + '/div', + ]; + $this->assertHtml($expected, $result); + + $Form->create($this->article, [ + 'align' => [ + 'sm' => [ + FormHelper::GRID_COLUMN_ONE => 5, + FormHelper::GRID_COLUMN_TWO => 7, + ], + ], + 'spacing' => 'mb-1', + ]); + $Form->end(); + + $result = $Form->control('title'); + $this->assertHtml($expected, $result); + } + /** * Test that "form-*" classes are added when using methods for specific input. *