Files
xamxam/templates/partials/form/select-field.php
Pontoporeia c2eff75789 WCAG 3.3.1: autofocus first invalid field on add/edit form validation failure
Add App::flashAutofocus(fieldName) and consumeAutofocus() to the thin App
helper so action handlers can identify which field caused a validation error
and the form page can move browser focus directly to it on reload.

Changes:
- src/App.php — flashAutofocus() stores field name in _flash_autofocus
  session key; consumeAutofocus() drains it and returns the name (or null)
- actions/formulaire.php — catch block maps exception messages to field
  names (auteurice, titre, synopsis, année, orientation, ap, finality,
  languages, tag, lien) and calls App::flashAutofocus()
- actions/edit.php — catch block maps common edit errors to field names
  and calls App::flashAutofocus()
- add.php — consumes the hint via App::consumeAutofocus() into
  $autofocusField; withAutofocus() helper merges autofocus=>true into
  $attrs for every field include; synopsis textarea gets inline autofocus
- edit.php — same pattern with inline ternary merges and textarea autofocus
- templates/partials/form/text-field.php — $attrs loop now emits bare
  attribute names (no ="...") when value === true, supporting autofocus,
  disabled, readonly etc. without special-casing
- templates/partials/form/select-field.php — same boolean-attr support
  added; $attrs variable initialised to [] when caller omits it

Closes WCAG 3.3.1 autofocus item in todo/04-accessibility.md.
2026-04-06 15:33:08 +02:00

62 lines
2.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
*/
$required = $required ?? false;
$placeholder = array_key_exists('placeholder', get_defined_vars()) ? $placeholder : '';
$id = $id ?? $name;
$hint = $hint ?? null;
$attrs = $attrs ?? [];
?>
<div>
<label for="<?= htmlspecialchars($id) ?>"><?= htmlspecialchars($label) ?></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 ?>>
<?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><?= htmlspecialchars($hint) ?></small>
<?php endif; ?>
</div>
<?php
unset($required, $placeholder, $id, $hint, $attrs, $selectAttrStr, $k, $v);