Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabled- and form-Attribute on fieldsets #255

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
mimmi20 marked this conversation as resolved.
Show resolved Hide resolved
];

/**
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
45 changes: 42 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,48 @@ 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 $allowsShortAttribute
): void {
$this->helper->setDoctype($doctype);

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

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

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

public static function provideDoctypesAndPermitFlagForDisabledAttribute(): 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, false],
mimmi20 marked this conversation as resolved.
Show resolved Hide resolved
[Doctype::HTML4_STRICT, false],
[Doctype::HTML4_LOOSE, false],
[Doctype::HTML4_FRAMESET, false],
[Doctype::HTML5, true],
];
}
}