Files
xamxam/app/templates/partials/form/checkbox-list.php
Pontoporeia d588ae004d Reintroduce TFE duration metadata: DB columns, form fields, controllers, views, and migration
Add 'unsafe-eval' to CSP script-src directives (htmx requires Function())
2026-06-15 15:56:52 +02:00

70 lines
3.4 KiB
PHP

<?php
/**
* Checkbox list partial — renders a group of checkboxes (e.g. languages, formats).
*
* The group label uses a visible <span> as the first column (matching other form
* rows), while a <fieldset>/<legend> in the second column provides the accessible
* grouping required by WCAG 1.3.1. The <legend> is visually hidden (sr-only) to
* avoid duplicating the visible label text.
*
* Variables consumed:
* string $name — input name attribute (will be posted as array: name[])
* string $label — group label text (htmlspecialchars'd unless $labelHtml is set)
* string $labelHtml — raw HTML label override (bypasses htmlspecialchars, optional)
* array $options — each element must have 'id' and 'name' keys
* array $checked — array of 'id' values that are currently checked
* bool $required — whether at least one checkbox must be checked; default false
* string $hxPost — optional hx-post URL for HTMX live update
* string $hxTarget — optional hx-target CSS selector for HTMX swap
* string $hxSwap — optional hx-swap value; default 'outerHTML'
* string $hxInclude — optional hx-include selector; default 'this'
* string|null $errorFieldName — when set and matches $name, adds aria-invalid on checkboxes
*/
$checked = $checked ?? [];
$required = $required ?? false;
$hxPost = $hxPost ?? '';
$hxTarget = $hxTarget ?? '';
$hxSwap = $hxSwap ?? 'outerHTML';
$hxInclude = $hxInclude ?? 'this';
$errorFieldName = $errorFieldName ?? null;
$ariaInvalid = ($errorFieldName === $name) ? ' aria-invalid="true" aria-errormessage="flash-error"' : '';
// aria-describedby only when there's an error (no hint text for checkbox groups)
$ariaDescribedBy = ($errorFieldName === $name) ? ' aria-describedby="flash-error"' : '';
?>
<div>
<span class="admin-row-label"><?= isset($labelHtml) ? $labelHtml : (htmlspecialchars($label) . ($required ? ' <span class="asterisk">*</span>' : '')) ?></span>
<fieldset class="admin-checkbox-group"
role="group"
<?= $required ? ' aria-required="true"' : '' ?>
<?php if ($hxPost !== ''): ?>
hx-post="<?= htmlspecialchars($hxPost) ?>"
hx-target="<?= htmlspecialchars($hxTarget) ?>"
hx-trigger="change"
hx-include="<?= htmlspecialchars($hxInclude) ?>"
hx-swap="<?= htmlspecialchars($hxSwap) ?>"
<?php endif; ?>>
<legend class="sr-only"><?= htmlspecialchars($label) ?></legend>
<ul>
<?php foreach ($options as $opt): ?>
<li>
<label class="admin-checkbox-label">
<input type="checkbox"
name="<?= htmlspecialchars($name) ?>[]"
value="<?= htmlspecialchars((string)$opt['id']) ?>"
<?= in_array((string)$opt['id'], array_map('strval', $checked)) ? 'checked' : '' ?>
<?= $ariaInvalid ?>
<?= $ariaDescribedBy ?>>
<?= htmlspecialchars($opt['name']) ?>
</label>
</li>
<?php endforeach; ?>
</ul>
</fieldset>
</div>
<?php
unset($checked, $hxPost, $hxTarget, $hxSwap, $hxInclude, $ariaInvalid, $ariaDescribedBy);
// NOTE: $errorFieldName is intentionally NOT unset — it is a shared variable
// consumed by downstream partials (e.g. fichiers-fragment.php).