From 8489f2ef91539cd62b277f0d9e3aafcbeeca2921 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Sun, 17 Nov 2024 03:00:05 +0300 Subject: [PATCH 1/4] implements container.formGroupClass parameter to pass or disable class different from form-group to containers --- src/View/Helper/FormHelper.php | 69 +++++++++++++++++----------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index b7d5c6ab..bbc2f0e8 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -148,28 +148,27 @@ class FormHelper extends CoreFormHelper '{{input}}{{label}}', 'datetimeContainer' => '{{content}}{{help}}', + 'class="{{containerClass}}{{formGroupClass}} {{type}}{{required}}">{{content}}{{help}}', 'datetimeContainerError' => '' . + 'class="{{containerClass}}{{formGroupClass}} {{formGroupPosition}}{{type}}{{required}} is-invalid">' . '{{content}}{{error}}{{help}}', 'datetimeLabel' => '{{text}}{{tooltip}}', 'inputContainer' => '{{content}}{{help}}', + 'class="{{containerClass}}{{formGroupClass}} {{type}}{{required}}">{{content}}{{help}}', 'inputContainerError' => '' . + 'class="{{containerClass}}{{formGroupClass}} {{formGroupPosition}}{{type}}{{required}} is-invalid">' . '{{content}}{{error}}{{help}}', 'checkboxContainer' => '{{content}}{{help}}', + 'class="{{containerClass}}{{formGroupClass}} form-check{{variant}} {{type}}{{required}}">{{content}}{{help}}', 'checkboxContainerError' => '{{content}}{{error}}{{help}}', + 'class="{{containerClass}}{{formGroupClass}} form-check{{variant}} {{formGroupPosition}}{{type}}{{required}} is-invalid">' . + '{{content}}{{error}}{{help}}', 'checkboxInlineContainer' => '' . @@ -185,11 +184,11 @@ class FormHelper extends CoreFormHelper 'checkboxInlineWrapper' => '
{{label}}
', 'radioContainer' => - '{{content}}{{help}}', 'radioContainerError' => '{{content}}{{error}}{{help}}', 'radioLabel' => '{{text}}{{tooltip}}', @@ -204,11 +203,11 @@ class FormHelper extends CoreFormHelper 'inputGroupText' => '{{content}}', 'multicheckboxContainer' => - '{{content}}{{help}}', 'multicheckboxContainerError' => '{{content}}{{error}}{{help}}', 'multicheckboxLabel' => '{{text}}{{tooltip}}', @@ -244,31 +243,31 @@ class FormHelper extends CoreFormHelper '{{formGroupPosition}}{{type}}{{required}} is-invalid">{{content}}{{error}}{{help}}', 'datetimeContainer' => '' . + 'class="{{containerClass}}{{formGroupClass}} {{formGroupPosition}}{{type}}{{required}}">' . '{{content}}{{help}}', 'datetimeContainerError' => '' . + 'class="{{containerClass}}{{formGroupClass}} {{formGroupPosition}}{{type}}{{required}} is-invalid">' . '{{content}}{{error}}{{help}}', 'datetimeLabel' => '{{text}}{{tooltip}}', 'radioContainer' => '{{content}}{{help}}', 'radioContainerError' => '{{content}}{{error}}{{help}}', 'radioLabel' => '{{text}}{{tooltip}}', 'multicheckboxContainer' => '{{content}}{{help}}', 'multicheckboxContainerError' => '{{content}}{{error}}{{help}}', 'multicheckboxLabel' => @@ -289,47 +288,47 @@ class FormHelper extends CoreFormHelper '
{{input}}{{label}}{{error}}{{help}}
', 'datetimeContainer' => '{{content}}', + 'class="{{containerClass}}{{formGroupClass}} row {{type}}{{required}}">{{content}}', 'datetimeContainerError' => '' . + 'class="{{containerClass}}{{formGroupClass}} row {{formGroupPosition}}{{type}}{{required}} is-invalid">' . '{{content}}', 'datetimeLabel' => '{{text}}{{tooltip}}', 'checkboxInlineFormGroup' => '
{{input}}{{label}}
', 'submitContainer' => - '' . + '' . '
{{content}}
', 'inputContainer' => - '' . + '' . '{{content}}', 'inputContainerError' => '' . + 'class="{{containerClass}}{{formGroupClass}} row {{formGroupPosition}}{{type}}{{required}} is-invalid">' . '{{content}}', 'checkboxContainer' => - '' . + '' . '{{content}}', 'checkboxContainerError' => '' . + 'class="{{containerClass}}{{formGroupClass}} row {{formGroupPosition}}{{type}}{{required}} is-invalid">' . '{{content}}', 'radioContainer' => - '{{content}}', 'radioContainerError' => '{{content}}', 'radioLabel' => '{{text}}{{tooltip}}', 'multicheckboxContainer' => - '{{content}}', 'multicheckboxContainerError' => '{{content}}', 'multicheckboxLabel' => '{{text}}{{tooltip}}', @@ -658,13 +657,15 @@ protected function _containerOptions(?string $fieldName, array $options): array $options['container'] = $this->injectClasses('form-floating', (array)($options['container'] ?? [])); } - if (!isset($options['container'])) { - return $options; - } - - $containerOptions = $options['container']; + $containerOptions = $options['container'] ?? []; unset($options['container']); + $options['templateVars']['formGroupClass'] = 'form-group'; + if (isset($containerOptions['formGroupClass'])) { + $options['templateVars']['formGroupClass'] = $containerOptions['formGroupClass']; + unset($containerOptions['formGroupClass']); + } + if (isset($containerOptions['class'])) { $options['templateVars']['containerClass'] = $containerOptions['class'] . ' '; unset($containerOptions['class']); From 800d6adb22b6bf3a06f63652a32e54a058ed7956 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Sun, 17 Nov 2024 03:36:43 +0300 Subject: [PATCH 2/4] fix line length issue --- src/View/Helper/FormHelper.php | 67 ++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index bbc2f0e8..0c286d16 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -164,11 +164,13 @@ class FormHelper extends CoreFormHelper '{{content}}{{error}}{{help}}', 'checkboxContainer' => '{{content}}{{help}}', + 'class="{{containerClass}}{{formGroupClass}} form-check{{variant}} ' . + '{{type}}{{required}}">{{content}}{{help}}', 'checkboxContainerError' => '' . - '{{content}}{{error}}{{help}}', + 'class="{{containerClass}}{{formGroupClass}} form-check{{variant}} ' . + '{{formGroupPosition}}{{type}}{{required}} is-invalid">' . + '{{content}}{{error}}{{help}}', 'checkboxInlineContainer' => '' . @@ -247,18 +249,23 @@ class FormHelper extends CoreFormHelper '{{content}}{{help}}', 'datetimeContainerError' => '' . - '{{content}}{{error}}{{help}}', + 'class="{{containerClass}}{{formGroupClass}} ' . + '{{formGroupPosition}}{{type}}{{required}} is-invalid">' . + '{{content}}{{error}}{{help}}', 'datetimeLabel' => '{{text}}{{tooltip}}', 'radioContainer' => '{{content}}{{help}}', + 'class="{{containerClass}}{{formGroupClass}} ' . + '{{formGroupPosition}}{{type}}{{required}}" ' . + 'role="group" aria-labelledby="{{groupId}}">' . + '{{content}}{{help}}', 'radioContainerError' => '{{content}}{{error}}{{help}}', + 'class="{{containerClass}}{{formGroupClass}} ' . + '{{formGroupPosition}}{{type}}{{required}} is-invalid" ' . + 'role="group" aria-labelledby="{{groupId}}">' . + '{{content}}{{error}}{{help}}', 'radioLabel' => '{{text}}{{tooltip}}', 'multicheckboxContainer' => @@ -291,8 +298,9 @@ class FormHelper extends CoreFormHelper 'class="{{containerClass}}{{formGroupClass}} row {{type}}{{required}}">{{content}}', 'datetimeContainerError' => '' . - '{{content}}', + 'class="{{containerClass}}{{formGroupClass}} row ' . + '{{formGroupPosition}}{{type}}{{required}} is-invalid">' . + '{{content}}', 'datetimeLabel' => '{{text}}{{tooltip}}', 'checkboxInlineFormGroup' => @@ -305,31 +313,42 @@ class FormHelper extends CoreFormHelper '{{content}}', 'inputContainerError' => '' . - '{{content}}', + 'class="{{containerClass}}{{formGroupClass}} row ' . + '{{formGroupPosition}}{{type}}{{required}} is-invalid">' . + '{{content}}', 'checkboxContainer' => - '' . + '' . '{{content}}', 'checkboxContainerError' => '' . - '{{content}}', + 'class="{{containerClass}}{{formGroupClass}} row ' . + '{{formGroupPosition}}{{type}}{{required}} is-invalid">' . + '{{content}}', 'radioContainer' => - '{{content}}', + '' . + '{{content}}', 'radioContainerError' => '{{content}}', + 'class="{{containerClass}}{{formGroupClass}} row ' . + '{{formGroupPosition}}{{type}}{{required}} is-invalid" ' . + 'role="group" aria-labelledby="{{groupId}}">' . + '{{content}}', 'radioLabel' => '{{text}}{{tooltip}}', 'multicheckboxContainer' => - '{{content}}', + '' . + '{{content}}', 'multicheckboxContainerError' => '{{content}}', + 'class="{{containerClass}}{{formGroupClass}} row ' . + '{{formGroupPosition}}{{type}}{{required}} is-invalid" ' . + 'role="group" aria-labelledby="{{groupId}}">' . + '{{content}}', 'multicheckboxLabel' => '{{text}}{{tooltip}}', ], From 074628e77d45f7c070575b0b5e56b33d66a96e96 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Sun, 17 Nov 2024 03:48:00 +0300 Subject: [PATCH 3/4] added tests cases covering custom form-group --- tests/TestCase/View/Helper/FormHelperTest.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index 0996be03..aa01ab5a 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -1822,4 +1822,56 @@ public function testFloatingLabelPlaceholderGenerationForIdField() ]; $this->assertHtml($expected, $result); } + + public function testFormGroupClassOverride() + { + $this->article['required']['title'] = false; + $this->Form->create($this->article); + + $result = $this->Form->control('title', [ + 'container' => [ + 'formGroupClass' => 'custom-form-group', + ], + ]); + $expected = [ + ['div' => ['class' => 'mb-3 custom-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); + } + + public function testFormGroupClassEmpty() + { + $this->article['required']['title'] = false; + $this->Form->create($this->article); + + $result = $this->Form->control('title', [ + 'container' => [ + 'formGroupClass' => '', + ], + ]); + $expected = [ + ['div' => ['class' => 'mb-3 text']], + ['label' => ['class' => 'form-label', 'for' => 'title']], + 'Title', + '/label', + 'input' => [ + 'type' => 'text', + 'name' => 'title', + 'id' => 'title', + 'class' => 'form-control', + ], + '/div', + ]; + $this->assertHtml($expected, $result); + } } From 810d6eb5c60080be87c39ecfa105af9140f66e44 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Wed, 20 Nov 2024 07:52:40 +0300 Subject: [PATCH 4/4] simplify form-group detection logic --- src/View/Helper/FormHelper.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 7824b335..fa58ef3b 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -601,11 +601,8 @@ protected function _containerOptions(?string $fieldName, array $options): array $containerOptions = $options['container'] ?? []; unset($options['container']); - $options['templateVars']['formGroupClass'] = 'form-group'; - if (isset($containerOptions['formGroupClass'])) { - $options['templateVars']['formGroupClass'] = $containerOptions['formGroupClass']; - unset($containerOptions['formGroupClass']); - } + $options['templateVars']['formGroupClass'] = $containerOptions['formGroupClass'] ?? 'form-group'; + unset($containerOptions['formGroupClass']); if (isset($containerOptions['class'])) { $options['templateVars']['containerClass'] = $containerOptions['class'] . ' ';