Skip to content

Commit

Permalink
allow attributes for <fieldset> wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Müller <mimmi20@live.de>
  • Loading branch information
mimmi20 committed Jan 3, 2024
1 parent 6f6c60a commit 3ecb00d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/View/Helper/FormRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Laminas\Form\LabelAwareInterface;

use function in_array;
use function is_array;
use function method_exists;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -186,8 +187,24 @@ public function render(ElementInterface $element, ?string $labelPosition = null)
|| $element instanceof MonthSelect
|| $element instanceof Captcha
) {
$legendAttributesData = $element->getOption('legend_attributes');
$wrapperAttributesData = $element->getOption('wrapper_attributes');
$wrapperAttributesString = '';
$legendAttributesString = '';

if (is_array($legendAttributesData) && $legendAttributesData !== []) {
$legendAttributesString = ' ' . (new FormLabel())->createAttributesString($legendAttributesData);
}

if (is_array($wrapperAttributesData) && $wrapperAttributesData !== []) {
$wrapperAttributesString = ' '
. (new FormCollection())->createAttributesString($wrapperAttributesData);
}

$markup = sprintf(
'<fieldset><legend>%s</legend>%s</fieldset>',
'<fieldset%s><legend%s>%s</legend>%s</fieldset>',
$wrapperAttributesString,
$legendAttributesString,
$label,
$elementString
);
Expand Down
73 changes: 73 additions & 0 deletions test/View/Helper/FormRowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laminas\Form\Element;
use Laminas\Form\Element\Captcha;
use Laminas\Form\Exception\InvalidArgumentException;
use Laminas\Form\Fieldset;

Check failure on line 10 in test/View/Helper/FormRowTest.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPCodeSniffer [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Type Laminas\Form\Fieldset is not used in this file.
use Laminas\Form\View\Helper\FormRow as FormRowHelper;
use Laminas\I18n\Translator\Translator;
use Laminas\I18n\Translator\TranslatorInterface;
Expand Down Expand Up @@ -598,4 +599,76 @@ public function testWrapFieldsetAroundCaptchaWithLabel(): void
]))
);
}

public function testCanSetOptionToSetAttributesToLegend(): void
{
$element = new Element\Radio('foo', [
'value_options' => [
'yes' => 'yes',
'no' => 'no',
],
'legend_attributes' => [
'class' => 'col-form-label',
],
]);
$element->setAttribute('id', 'bar');
$element->setLabel('baz');

$markup = $this->helper->render($element);
self::assertMatchesRegularExpression(
'#^<fieldset><legend class="col-form-label">baz</legend>'
. '<label><input type="radio" name="foo" id="bar" value="yes">yes</label>'
. '<label><input type="radio" name="foo" value="no">no</label></fieldset>$#',
$markup
);
}

public function testCanSetOptionToSetAttributesToWrapper(): void
{
$element = new Element\Radio('foo', [
'value_options' => [
'yes' => 'yes',
'no' => 'no',
],
'wrapper_attributes' => [
'class' => 'row',
],
]);
$element->setAttribute('id', 'bar');
$element->setLabel('baz');

$markup = $this->helper->render($element);
self::assertMatchesRegularExpression(
'#^<fieldset class="row"><legend>baz</legend>'
. '<label><input type="radio" name="foo" id="bar" value="yes">yes</label>'
. '<label><input type="radio" name="foo" value="no">no</label></fieldset>$#',
$markup
);
}

public function testCanSetOptionToSetAttributesToWrapperAndLegend(): void
{
$element = new Element\Radio('foo', [
'value_options' => [
'yes' => 'yes',
'no' => 'no',
],
'wrapper_attributes' => [
'class' => 'row',
],
'legend_attributes' => [
'class' => 'col-form-label',
],
]);
$element->setAttribute('id', 'bar');
$element->setLabel('baz');

$markup = $this->helper->render($element);
self::assertMatchesRegularExpression(
'#^<fieldset class="row"><legend class="col-form-label">baz</legend>'
. '<label><input type="radio" name="foo" id="bar" value="yes">yes</label>'
. '<label><input type="radio" name="foo" value="no">no</label></fieldset>$#',
$markup
);
}
}

0 comments on commit 3ecb00d

Please sign in to comment.