Skip to content

Commit f282fb1

Browse files
committed
bug #121 [stimulus-controller] fix bool attributes from being rendered incorrectly (jrushlow)
This PR was squashed before being merged into the main branch. Discussion ---------- [stimulus-controller] fix bool attributes from being rendered incorrectly fixes #118 Commits ------- 2694dca [stimulus-controller] fix bool attributes from being rendered incorrectly
2 parents 0cd625e + 2694dca commit f282fb1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Twig/StimulusTwigExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public function renderStimulusController(Environment $env, $dataOrControllerName
5959
$value = json_encode($value);
6060
}
6161

62+
if (is_bool($value)) {
63+
$value = $value ? 'true' : 'false';
64+
}
65+
6266
$key = twig_escape_filter($env, $this->normalizeKeyName($key), 'html_attr');
6367
$value = twig_escape_filter($env, $value, 'html_attr');
6468

tests/IntegrationTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public function provideRenderStimulusController()
214214
],
215215
],
216216
'controllerValues' => [],
217-
'expected' => 'data-controller="my-controller" data-my-controller-boolean-value="1" data-my-controller-number-value="4" data-my-controller-string-value="str"',
217+
'expected' => 'data-controller="my-controller" data-my-controller-boolean-value="true" data-my-controller-number-value="4" data-my-controller-string-value="str"',
218218
];
219219

220220
yield 'single-controller-nested-data' => [
@@ -247,7 +247,7 @@ public function provideRenderStimulusController()
247247
],
248248
],
249249
'controllerValues' => [],
250-
'expected' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="1"',
250+
'expected' => 'data-controller="symfony--ux-dropzone--dropzone" data-symfony--ux-dropzone--dropzone-my-key-value="true"',
251251
];
252252

253253
yield 'short-single-controller-no-data' => [
@@ -261,6 +261,18 @@ public function provideRenderStimulusController()
261261
'controllerValues' => ['myValue' => 'scalar-value'],
262262
'expected' => 'data-controller="my-controller" data-my-controller-my-value-value="scalar-value"',
263263
];
264+
265+
yield 'false-attribute-value-renders-false' => [
266+
'dataOrControllerName' => 'false-controller',
267+
'controllerValues' => ['isEnabled' => false],
268+
'expected' => 'data-controller="false-controller" data-false-controller-is-enabled-value="false"',
269+
];
270+
271+
yield 'true-attribute-value-renders-true' => [
272+
'dataOrControllerName' => 'true-controller',
273+
'controllerValues' => ['isEnabled' => true],
274+
'expected' => 'data-controller="true-controller" data-true-controller-is-enabled-value="true"',
275+
];
264276
}
265277

266278
/**

0 commit comments

Comments
 (0)