Skip to content

Commit

Permalink
Merge pull request #255 from mimmi20/feature-disable-fieldsets
Browse files Browse the repository at this point in the history
allow disabled- and form-Attribute on fieldsets
  • Loading branch information
Slamdunk authored Jan 9, 2024
2 parents d42fd69 + 227047d commit 39262de
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/View/Helper/FormCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Laminas\Form\ElementInterface;
use Laminas\Form\FieldsetInterface;
use Laminas\Form\LabelAwareInterface;
use Laminas\View\Helper\Doctype;
use Laminas\View\Helper\HelperInterface;
use RuntimeException;

Expand All @@ -25,7 +24,9 @@ class FormCollection extends AbstractHelper
* @var array
*/
protected $validTagAttributes = [
'name' => true,
'name' => true,
'disabled' => true,
'form' => true,
];

/**
Expand Down Expand Up @@ -77,11 +78,6 @@ class FormCollection extends AbstractHelper
*/
protected $fieldsetHelper;

private array $doctypesAllowedToHaveNameAttribute = [
Doctype::HTML5 => true,
Doctype::XHTML5 => true,
];

/**
* Invoke helper as function
*
Expand Down Expand Up @@ -136,8 +132,12 @@ public function render(ElementInterface $element): string
// Every collection is wrapped by a fieldset if needed
if ($this->shouldWrap) {
$attributes = $element->getAttributes();
if (! isset($this->doctypesAllowedToHaveNameAttribute[$this->getDoctype()])) {
unset($attributes['name']);
if (! $this->getDoctypeHelper()->isHtml5()) {
unset(
$attributes['name'],
$attributes['disabled'],
$attributes['form']
);
}
$attributesString = $attributes !== [] ? ' ' . $this->createAttributesString($attributes) : '';

Expand Down
135 changes: 132 additions & 3 deletions test/View/Helper/FormCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ public function testCanDisableLabelHtmlEscape(): void
public function testForElementHelperNotInstanceOfHelperInterface(): void
{
$method = new ReflectionMethod(FormCollectionHelper::class, 'getElementHelper');
$method->setAccessible(true);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(
Expand Down Expand Up @@ -458,8 +457,138 @@ public static function provideDoctypesAndPermitFlagForNameAttribute(): array
[Doctype::HTML4_LOOSE, false],
[Doctype::HTML4_FRAMESET, false],
[Doctype::HTML5, true],
[Doctype::CUSTOM_XHTML, false],
[Doctype::CUSTOM, false],
];
}

/**
* @dataProvider provideDoctypesAndPermitFlagForDisabledAttribute
*/
public function testRenderCollectionWithDisabledAttribute(
string $doctype,
bool $allowsNameAttribute,
bool $allowsShortAttribute
): void {
$this->helper->setDoctype($doctype);

$form = $this->getForm();
$collection = $form->get('colors');
$collection->setAttribute('disabled', true);

$markup = $this->helper->render($collection);

if ($allowsNameAttribute) {
if ($allowsShortAttribute) {
self::assertStringContainsString('<fieldset name="colors" disabled>', $markup);
} else {
self::assertStringContainsString('<fieldset name="colors" disabled="disabled">', $markup);
}
} else {
self::assertStringContainsString('<fieldset>', $markup);
}
}

public static function provideDoctypesAndPermitFlagForDisabledAttribute(): array
{
return [
[
'doctype' => Doctype::XHTML11,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::XHTML1_STRICT,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::XHTML1_TRANSITIONAL,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::XHTML1_FRAMESET,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::XHTML1_RDFA,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::XHTML1_RDFA11,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::XHTML_BASIC1,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::XHTML5,
'allowsNameAttribute' => true,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::HTML4_STRICT,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::HTML4_LOOSE,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::HTML4_FRAMESET,
'allowsNameAttribute' => false,
'allowsShortAttribute' => false,
],
[
'doctype' => Doctype::HTML5,
'allowsNameAttribute' => true,
'allowsShortAttribute' => true,
],
];
}

/**
* @dataProvider provideDoctypesAndPermitFlagForFormAttribute
*/
public function testRenderCollectionWithFormAttributeAndDoctypeHtml5(
string $doctype,
bool $allowsFormAttribute
): void {
$this->helper->setDoctype($doctype);

$form = $this->getForm();
$collection = $form->get('colors');
$collection->setAttribute('form', 'foo');

$markup = $this->helper->render($collection);
if ($allowsFormAttribute) {
self::assertStringContainsString('<fieldset name="colors" form="foo">', $markup);
} else {
self::assertStringContainsString('<fieldset>', $markup);
}
}

public static function provideDoctypesAndPermitFlagForFormAttribute(): array
{
return [
[Doctype::XHTML11, false],
[Doctype::XHTML1_STRICT, false],
[Doctype::XHTML1_TRANSITIONAL, false],
[Doctype::XHTML1_FRAMESET, false],
[Doctype::XHTML1_RDFA, false],
[Doctype::XHTML1_RDFA11, false],
[Doctype::XHTML_BASIC1, false],
[Doctype::XHTML5, true],
[Doctype::HTML4_STRICT, false],
[Doctype::HTML4_LOOSE, false],
[Doctype::HTML4_FRAMESET, false],
[Doctype::HTML5, true],
];
}
}

0 comments on commit 39262de

Please sign in to comment.