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

fix(MultiCheckbox): view helper is removing the hidden element #651

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion src/TwbsHelper/Form/View/Helper/MultiCheckboxTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,29 @@ public function render(\Laminas\Form\ElementInterface $element): string
$content = '';

foreach (explode(self::$TMP_SEPARATOR, $tmpContent) as $optionContent) {

// Retrieve input content
if (preg_match('/(<label[^>]*>.*)(<input[^>]+>)(.*<\/label[^>]*>)/', $optionContent, $matches)) {
$extraOptionContent = trim(str_replace($matches[0], '', $optionContent));


$labelContent = $matches[1] . $matches[3];
if (preg_match('/<label[^>]*>\s*<\/label[^>]*>/', $labelContent)) {
$labelContent = '';
}

$optionContent = $matches[2] . ($labelContent ? PHP_EOL . $labelContent : '');

$content .= ($content ? PHP_EOL : '') . $this->renderElementOption(
$optionContent = $this->renderElementOption(
$element,
$optionContent,
);

if ($extraOptionContent) {
$optionContent = $extraOptionContent . PHP_EOL . $optionContent;
}

$content .= ($content ? PHP_EOL : '') . $optionContent;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestSuite/Documentation/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Documentation;
namespace TestSuite\Documentation;

/**
* Base class to perform tests according to the Bootstrap documentation : %bootstrap-url%/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public function testRenderSimpleValues()
{
$this->assertEquals(
'<div class="form-check">' . PHP_EOL .
' <input class="form-check-input" name="test&#x5B;&#x5D;" type="checkbox" value="0"/>' . PHP_EOL .
' <label>First checkbox</label>' . PHP_EOL .
'</div>' . PHP_EOL .
'<div class="form-check">' . PHP_EOL .
' <input class="form-check-input" name="test&#x5B;&#x5D;" type="checkbox" value="1"/>' . PHP_EOL .
' <label>Second checkbox</label>' . PHP_EOL .
'</div>' . PHP_EOL .
'<div class="form-check">' . PHP_EOL .
' <input class="form-check-input" name="test&#x5B;&#x5D;" type="checkbox" value="2"/>' . PHP_EOL .
' <label>Third checkbox</label>' . PHP_EOL .
'</div>',
' <input class="form-check-input" name="test&#x5B;&#x5D;" type="checkbox" value="0"/>' . PHP_EOL .
' <label>First checkbox</label>' . PHP_EOL .
'</div>' . PHP_EOL .
'<div class="form-check">' . PHP_EOL .
' <input class="form-check-input" name="test&#x5B;&#x5D;" type="checkbox" value="1"/>' . PHP_EOL .
' <label>Second checkbox</label>' . PHP_EOL .
'</div>' . PHP_EOL .
'<div class="form-check">' . PHP_EOL .
' <input class="form-check-input" name="test&#x5B;&#x5D;" type="checkbox" value="2"/>' . PHP_EOL .
' <label>Third checkbox</label>' . PHP_EOL .
'</div>',
$this->helper->render(new \Laminas\Form\Element\MultiCheckbox(
'test',
[
Expand All @@ -37,4 +37,37 @@ public function testRenderSimpleValues()
))
);
}

public function testRenderHiddenElement()
{
$multiCheckbox = new \Laminas\Form\Element\MultiCheckbox(
'test',
[
'label' => 'MultiCheckbox with hidden element',
'value_options' => [
0 => 'First checkbox',
1 => 'Second checkbox',
2 => 'Third checkbox',
],
'use_hidden_element' => true,
]
);
$multiCheckbox->setUseHiddenElement(true);
$this->assertEquals(
'<input type="hidden" name="test" value=""/>' . PHP_EOL .
'<div class="form-check">' . PHP_EOL .
' <input class="form-check-input" name="test&#x5B;&#x5D;" type="checkbox" value="0"/>' . PHP_EOL .
' <label>First checkbox</label>' . PHP_EOL .
'</div>' . PHP_EOL .
'<div class="form-check">' . PHP_EOL .
' <input class="form-check-input" name="test&#x5B;&#x5D;" type="checkbox" value="1"/>' . PHP_EOL .
' <label>Second checkbox</label>' . PHP_EOL .
'</div>' . PHP_EOL .
'<div class="form-check">' . PHP_EOL .
' <input class="form-check-input" name="test&#x5B;&#x5D;" type="checkbox" value="2"/>' . PHP_EOL .
' <label>Third checkbox</label>' . PHP_EOL .
'</div>',
$this->helper->render($multiCheckbox)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TwbsHelper\View\Helper\HtmlAttributes\HtmlClass\Helper;
namespace TestSuite\TwbsHelper\View\Helper\HtmlAttributes\HtmlClass\Helper;

class GutterTest extends \PHPUnit\Framework\TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TwbsHelper\View\Helper\HtmlAttributes\HtmlClass\Helper;
namespace TestSuite\TwbsHelper\View\Helper\HtmlAttributes\HtmlClass\Helper;

class SpacingTest extends \PHPUnit\Framework\TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TwbsHelper\View\Helper\HtmlAttributes\HtmlClass\Helper;
namespace TestSuite\TwbsHelper\View\Helper\HtmlAttributes\HtmlClass\Helper;

class VariantTest extends \PHPUnit\Framework\TestCase
{
Expand Down
4 changes: 2 additions & 2 deletions website/docs/usage/components/badges.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ echo $this->formButton()->renderSpec([
'positioned' => true,
'type' => 'pill',
'hidden_content' => 'unread messages',
]
],
],
'variant' => 'primary',
],
Expand All @@ -143,7 +143,7 @@ echo $this->formButton()->renderSpec([
'positioned' => true,
'type' => 'pill',
'hidden_content' => 'New alerts',
]
],
],
'variant' => 'primary',
],
Expand Down
2 changes: 1 addition & 1 deletion website/docs/usage/components/button-group.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ echo $this->buttonGroup(
'attributes' => [
'class' => 'active',
'aria-current' => 'page',
'href' => '#'
'href' => '#',
],
],
[
Expand Down
14 changes: 7 additions & 7 deletions website/docs/usage/components/card.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ echo $this->card([
['label' => 'Active', 'uri' => '#', 'active' => true,],
['label' => 'Link', 'uri' => '#',],
['label' => 'Disabled', 'uri' => '#', 'visible' => false,],
]
],
],
'title' => 'Special title treatment',
'text' => 'With supporting text below as a natural lead-in to additional content.',
Expand Down Expand Up @@ -739,7 +739,7 @@ echo $this->card([
'text' => [
'This is a wider card with supporting text below as a natural lead-in to additional content. ' .
'This content is a little bit longer.',
'<small class="text-muted">Last updated 3 mins ago</small>'
'<small class="text-muted">Last updated 3 mins ago</small>',
],
'image_bottom' => ['/twbs-helper-module/img/docs/image-cap.svg', ['alt' => '...']],
]);
Expand Down Expand Up @@ -825,7 +825,7 @@ echo $this->card([
['alt' => '...', 'fluid' => true, 'rounded' => 'start'],
],
],
['column' => 'md-4']
['column' => 'md-4'],
],
[
[
Expand All @@ -840,7 +840,7 @@ echo $this->card([
],
],
['class' => 'g-0'],
]
],
], ['class' => 'mb-3', 'style' => 'max-width: 540px;']);
```

Expand Down Expand Up @@ -1218,7 +1218,7 @@ echo $this->cardGroup([
'title' => 'Card title',
'text' => [
'This card has supporting text below as a natural lead-in to additional content.',
'<small class="text-muted">Last updated 3 mins ago</small>'
'<small class="text-muted">Last updated 3 mins ago</small>',
],
],
[
Expand Down Expand Up @@ -1389,7 +1389,7 @@ echo $this->cardGrid(
'title' => 'Card title',
'text' => [
'This card has supporting text below as a natural lead-in to additional content.',
'<small class="text-muted">Last updated 3 mins ago</small>'
'<small class="text-muted">Last updated 3 mins ago</small>',
],
],
[
Expand Down Expand Up @@ -1433,7 +1433,7 @@ echo $this->cardGrid(
'title' => 'Card title',
'text' => [
'This card has supporting text below as a natural lead-in to additional content.',
'<small class="text-muted">Last updated 3 mins ago</small>'
'<small class="text-muted">Last updated 3 mins ago</small>',
],
],
[
Expand Down
2 changes: 1 addition & 1 deletion website/docs/usage/components/collapse.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ echo $this->collapse(
'options' => [
'variant' => 'primary',
'label' => 'Link with href',
'tag' => 'a'
'tag' => 'a',
],
],
[
Expand Down
14 changes: 7 additions & 7 deletions website/docs/usage/components/dropdowns.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ echo $this->formButton()->renderSpec([
'Something else here',
'---',
'Separated link',
]
],
],
],
]);
Expand Down Expand Up @@ -762,7 +762,7 @@ echo $this->dropdown()->renderMenu([
'Dropdown item text' => \TwbsHelper\View\Helper\Dropdown::TYPE_ITEM_TEXT,
'Action',
'Another action',
'Something else here'
'Something else here',
]);
```

Expand Down Expand Up @@ -791,7 +791,7 @@ echo $this->dropdown()->renderMenu([
echo $this->dropdown()->renderMenu([
'Regular link',
'Active link' => ['active' => true],
'Another link'
'Another link',
]);
```

Expand Down Expand Up @@ -820,7 +820,7 @@ echo $this->dropdown()->renderMenu([
echo $this->dropdown()->renderMenu([
'Regular link',
'Disabled link' => ['disabled' => true],
'Another link'
'Another link',
]);
```

Expand Down Expand Up @@ -1155,7 +1155,7 @@ echo $this->dropdown()->renderMenu([
<HtmlCode bootstrapVersion="5.1">
<div dangerouslySetInnerHTML={{ __html: `<ul class="dropdown-menu">
<li>
<form action="" class="px-4 py-3" id="dropdown" method="POST" name="dropdown" role="form">
<form class="px-4 py-3" id="dropdown" method="POST" name="dropdown" role="form">
<div class="mb-3">
<label class="form-label" for="exampleDropdownFormEmail1">Email address</label>
<input class="form-control" id="exampleDropdownFormEmail1" name="email" placeholder="[email protected]" type="email" value="">
Expand All @@ -1178,7 +1178,7 @@ echo $this->dropdown()->renderMenu([
<li><a class="dropdown-item" href="#">Forgot password?</a></li>
</ul>
<br>
<form action="" class="dropdown-menu p-4" id="dropdown" method="POST" name="dropdown" role="form">
<form class="dropdown-menu p-4" id="dropdown" method="POST" name="dropdown" role="form">
<div class="mb-3">
<label class="form-label" for="exampleDropdownFormEmail1">Email address</label>
<input class="form-control" id="exampleDropdownFormEmail1" name="email" placeholder="[email protected]" type="email" value="">
Expand Down Expand Up @@ -1248,7 +1248,7 @@ $form = $factory->create([
'options' => ['label' => 'Sign in', 'variant' => 'primary'],
],
],
]
],
]);

echo $this->dropdown()->renderMenu([
Expand Down
8 changes: 4 additions & 4 deletions website/docs/usage/components/modal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ echo $this->modal([
],
],
],
]
],
]);
```

Expand Down Expand Up @@ -807,7 +807,7 @@ echo $this->modal(
],
],
],
]
],
],
],
],
Expand Down Expand Up @@ -867,7 +867,7 @@ echo $this->modal(
<button aria-label="Close" class="btn-close" data-bs-dismiss="modal" name="button" type="button" value=""></button>
</div>
<div class="modal-body">
<form action="" id="form" method="POST" name="form" role="form">
<form id="form" method="POST" name="form" role="form">
<div class="mb-3">
<label class="form-label" for="recipient-name">Recipient:</label>
<input class="form-control" id="recipient-name" name="recipient" type="text" value="">
Expand All @@ -885,7 +885,7 @@ echo $this->modal(
</div>
</div>
</div>
<script type="text/javascript"></script>` }}></div>
<script></script>` }}></div>
</HtmlCode>
</TabItem>
<TabItem value="source" label="Source">
Expand Down
Loading