mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-25 16:19:19 +02:00
79 lines
3.6 KiB
PHP
79 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* Select field partial.
|
|
*
|
|
* Variables consumed:
|
|
* string $name — select name attribute (also used for id)
|
|
* string $label — visible label text
|
|
* array $options — each element must have 'id' and 'name' keys;
|
|
* may optionally have 'code' for display suffix
|
|
* mixed $selected — currently selected value (compared to option 'id');
|
|
* pass null or '' for no selection
|
|
* bool $required — whether the field is required; default false
|
|
* string $placeholder — text for the leading empty <option>; default ''
|
|
* set to null to suppress the empty option entirely
|
|
* string|null $id — override the id attribute (defaults to $name)
|
|
* string|null $hint — optional hint shown in <small> below the select
|
|
* string|null $errorFieldName — when set and matches $name, adds aria-invalid + aria-errormessage
|
|
*/
|
|
|
|
$required = $required ?? false;
|
|
$placeholder = array_key_exists('placeholder', get_defined_vars()) ? $placeholder : '';
|
|
$id = $id ?? $name;
|
|
$hint = $hint ?? null;
|
|
$attrs = $attrs ?? [];
|
|
$errorFieldName = $errorFieldName ?? null;
|
|
|
|
// Build hint id for aria-describedby
|
|
$hintId = ($hint !== null) ? ($id . '-hint') : null;
|
|
|
|
// Build describedby string (hint + error)
|
|
$describedBy = [];
|
|
if ($hintId !== null) $describedBy[] = $hintId;
|
|
if ($errorFieldName === $name) $describedBy[] = 'flash-error';
|
|
$ariaDescribedBy = !empty($describedBy) ? ' aria-describedby="' . implode(' ', $describedBy) . '"' : '';
|
|
|
|
$ariaInvalid = ($errorFieldName === $name) ? ' aria-invalid="true" aria-errormessage="flash-error"' : '';
|
|
?>
|
|
<div>
|
|
<label for="<?= htmlspecialchars($id) ?>"><?= htmlspecialchars($label) ?><?= $required ? ' <span class="asterisk">*</span>' : '' ?></label>
|
|
<?php
|
|
$selectAttrStr = '';
|
|
foreach ($attrs as $k => $v) {
|
|
if ($v === true) {
|
|
$selectAttrStr .= ' ' . htmlspecialchars($k);
|
|
} else {
|
|
$selectAttrStr .= ' ' . htmlspecialchars($k) . '="' . htmlspecialchars((string)$v) . '"';
|
|
}
|
|
}
|
|
?>
|
|
<select id="<?= htmlspecialchars($id) ?>"
|
|
name="<?= htmlspecialchars($name) ?>"
|
|
<?= $required ? 'required' : '' ?>
|
|
<?= $selectAttrStr ?>
|
|
<?= $ariaInvalid ?>
|
|
<?= $ariaDescribedBy ?>>
|
|
<?php if ($placeholder !== null): ?>
|
|
<option value=""><?= htmlspecialchars($placeholder) ?></option>
|
|
<?php endif; ?>
|
|
<?php foreach ($options as $opt): ?>
|
|
<?php
|
|
// Match by id (numeric FK) or by name string (when the view returns the name).
|
|
$isSelected = ((string)$selected === (string)$opt['id'])
|
|
|| ($selected !== null && $selected !== '' && isset($opt['name']) && (string)$selected === (string)$opt['name']);
|
|
?>
|
|
<option value="<?= htmlspecialchars((string)$opt['id']) ?>"
|
|
<?= $isSelected ? 'selected' : '' ?>>
|
|
<?= htmlspecialchars($opt['name']) ?><?php if (!empty($opt['code'])): ?> (<?= htmlspecialchars($opt['code']) ?>)<?php endif; ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<?php if ($hint): ?>
|
|
<small id="<?= htmlspecialchars($hintId) ?>"><?= htmlspecialchars($hint) ?></small>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php
|
|
unset($required, $placeholder, $id, $hint, $attrs, $selectAttrStr, $k, $v, $hintId, $describedBy, $ariaDescribedBy, $ariaInvalid);
|
|
// NOTE: $errorFieldName is intentionally NOT unset — it is a shared variable
|
|
// consumed by downstream partials (e.g. fichiers-fragment.php).
|