mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-25 16:19:19 +02:00
80 lines
3.5 KiB
PHP
80 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* Text field partial — single-line text / number / url / email input.
|
|
*
|
|
* Variables consumed:
|
|
* string $name — input name attribute (also used for id)
|
|
* string $label — visible label text
|
|
* string $value — current value (already htmlspecialchars'd by caller, or raw)
|
|
* string $type — input type; default 'text'
|
|
* bool $required — whether the field is required; default false
|
|
* string $placeholder — placeholder text; default ''
|
|
* string|null $hint — optional hint shown in <small> below the input
|
|
* string|null $id — override the id attribute (defaults to $name)
|
|
* array $attrs — extra HTML attributes as key=>value pairs (e.g. min/max for number)
|
|
* string|null $errorFieldName — when set and matches $name, adds aria-invalid + aria-errormessage
|
|
*
|
|
* The partial does NOT call htmlspecialchars on $value — the caller is responsible.
|
|
*/
|
|
|
|
$type = $type ?? 'text';
|
|
$required = $required ?? false;
|
|
$placeholder = $placeholder ?? '';
|
|
$hint = $hint ?? null;
|
|
$id = $id ?? $name;
|
|
$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) . '"' : '';
|
|
|
|
$attrStr = '';
|
|
foreach ($attrs as $k => $v) {
|
|
if ($v === true) {
|
|
$attrStr .= ' ' . htmlspecialchars($k);
|
|
} else {
|
|
$attrStr .= ' ' . htmlspecialchars($k) . '="' . htmlspecialchars((string)$v) . '"';
|
|
}
|
|
}
|
|
|
|
$ariaInvalid = ($errorFieldName === $name) ? ' aria-invalid="true" aria-errormessage="flash-error"' : '';
|
|
?>
|
|
<div>
|
|
<label for="<?= htmlspecialchars($id) ?>"><?= htmlspecialchars($label) ?><?= $required ? ' <span class="asterisk">*</span>' : '' ?></label>
|
|
<?php if ($hint): ?>
|
|
<div>
|
|
<input type="<?= htmlspecialchars($type) ?>"
|
|
id="<?= htmlspecialchars($id) ?>"
|
|
name="<?= htmlspecialchars($name) ?>"
|
|
value="<?= $value ?>"
|
|
<?= $required ? 'required' : '' ?>
|
|
<?= $placeholder ? 'placeholder="' . htmlspecialchars($placeholder) . '"' : '' ?>
|
|
<?= $attrStr ?>
|
|
<?= $ariaInvalid ?>
|
|
<?= $ariaDescribedBy ?>>
|
|
<small id="<?= htmlspecialchars($hintId) ?>"><?= htmlspecialchars($hint) ?></small>
|
|
</div>
|
|
<?php else: ?>
|
|
<input type="<?= htmlspecialchars($type) ?>"
|
|
id="<?= htmlspecialchars($id) ?>"
|
|
name="<?= htmlspecialchars($name) ?>"
|
|
value="<?= $value ?>"
|
|
<?= $required ? 'required' : '' ?>
|
|
<?= $placeholder ? 'placeholder="' . htmlspecialchars($placeholder) . '"' : '' ?>
|
|
<?= $attrStr ?>
|
|
<?= $ariaInvalid ?>
|
|
<?= $ariaDescribedBy ?>>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php
|
|
// Reset consumed variables so includes in a loop don't bleed state.
|
|
unset($type, $required, $placeholder, $hint, $id, $attrs, $attrStr, $k, $v, $hintId, $describedBy, $ariaDescribedBy, $ariaInvalid);
|
|
// NOTE: $errorFieldName is intentionally NOT unset — it is a shared variable
|
|
// consumed by the parent partial (e.g. fieldset-tfe-info.php for the synopsis textarea).
|