Skip to content

Commit

Permalink
Merge pull request #373 from ndm2/fix-missing-form-classes
Browse files Browse the repository at this point in the history
Fix missing/incorrect `form-*` classes for `checkbox()` and `select()`.
  • Loading branch information
dereuromark authored May 5, 2022
2 parents 6924ce4 + 1be2f9e commit 86fc099
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
27 changes: 21 additions & 6 deletions src/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,17 @@ public function submit(?string $caption = null, array $options = []): string
return $this->_postProcessElement($result, null, $options);
}

/**
* @inheritDoc
*/
public function select(string $fieldName, iterable $options = [], array $attributes = []): string
{
$attributes['injectFormControl'] = false;
$attributes = $this->injectClasses('form-select', $attributes);

return parent::select($fieldName, $options, $attributes);
}

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -704,7 +715,6 @@ protected function _checkboxOptions(string $fieldName, array $options): array
if ($options['label'] !== false) {
$options['label'] = $this->injectClasses('form-check-label', (array)$options['label']);
}
$options = $this->injectClasses('form-check-input', $options);

if ($this->_align === static::ALIGN_HORIZONTAL) {
$options['inline'] = false;
Expand Down Expand Up @@ -850,11 +860,6 @@ protected function _selectOptions(string $fieldName, array $options): array
$options['label'] = $this->injectClasses($labelClasses, (array)$options['label']);
}

if ($options['type'] !== 'multicheckbox') {
$options['injectFormControl'] = false;
$options = $this->injectClasses('form-select', $options);
}

return $options;
}

Expand Down Expand Up @@ -1166,6 +1171,16 @@ protected function _postProcessElement(string $html, ?string $fieldName, array $
return $html;
}

/**
* @inheritDoc
*/
public function checkbox(string $fieldName, array $options = [])
{
$options = $this->injectClasses('form-check-input', $options);

return parent::checkbox($fieldName, $options);
}

/**
* @inheritDoc
*/
Expand Down
9 changes: 6 additions & 3 deletions tests/TestCase/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,11 +851,11 @@ public function testTooltipWithDisabledLabel()
}

/**
* Test that "form-control" class is added when using methods for specific input.
* Test that "form-*" classes are added when using methods for specific input.
*
* @return void
*/
public function testFormControlClassInjection()
public function testFormClassInjection()
{
$result = $this->Form->text('foo');
$this->assertStringContainsString('class="form-control"', $result);
Expand All @@ -864,7 +864,8 @@ public function testFormControlClassInjection()
$this->assertStringContainsString('class="custom form-control"', $result);

$result = $this->Form->select('foo');
$this->assertStringContainsString('class="form-control"', $result);
$this->assertStringNotContainsString('"form-control"', $result);
$this->assertStringContainsString('class="form-select"', $result);

$result = $this->Form->textarea('foo');
$this->assertStringContainsString('class="form-control"', $result);
Expand All @@ -877,9 +878,11 @@ public function testFormControlClassInjection()

$result = $this->Form->checkbox('foo');
$this->assertStringNotContainsString('"form-control"', $result);
$this->assertStringContainsString('class="form-check-input"', $result);

$result = $this->Form->radio('foo', ['1' => 'Opt 1', '2' => 'Opt 2']);
$this->assertStringNotContainsString('"form-control"', $result);
$this->assertStringContainsString('class="form-check-input"', $result);

$result = $this->Form->color('foo');
$this->assertStringContainsString('class="form-control form-control-color"', $result);
Expand Down

0 comments on commit 86fc099

Please sign in to comment.