Files
tsv08kulmbach-website/app/components/form-field.php
fs 6a612e52c3 Sportheim mieten: Buchungsseite mit Anfrageformular
Neue Seite /sportheimbuchung mit Bento-Layout (Fotos, Lage, Ausstattung) und
einem Buchungsanfrage-Formular nach Projektkonvention (form-field-Komponente,
Status-Region, Honeypot + ft-Token, Whitelist-Validierung, Rate-Limiting, PRG).
Versand via Brevo SMTP über app/actions/sportheimbuchung-senden.php, POST-Route
in index.php. form-field um min/max-Attribute erweitert (Personenzahl-Feld).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HGzc6GhWhmLJt1jC2q1SRZ
2026-06-20 20:46:44 +02:00

69 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
/**
* Einheitliches Formularfeld — EINZIGE Quelle für Feld-Markup aller Formulare.
*
* Props: $field (array):
* type text | email | tel | select | textarea | checkbox (Default: text)
* name, label Pflicht
* id Default: field-<name>
* required bool
* autocomplete, maxlength, rows (textarea)
* options[] + placeholder (select)
* hint optionaler Hilfetext (aria-describedby)
* label_html NUR für vertrauenswürdiges Markup aus Komponenten-Code
* (z. B. Datenschutz-Link im Checkbox-Label) — nie Nutzereingaben!
*
* Jedes Feld bekommt ein leeres Fehler-Element (#<id>-error), das form.js bei
* ungültiger Eingabe mit der Browser-Validierungsmeldung füllt (aria-describedby).
*/
$type = $field['type'] ?? 'text';
$name = $field['name'];
$id = $field['id'] ?? 'field-' . $name;
$required = !empty($field['required']);
$errorId = $id . '-error';
$hintId = $id . '-hint';
$describedBy = (!empty($field['hint']) ? $hintId . ' ' : '') . $errorId;
$requiredMark = $required
? ' <span class="form__required" aria-hidden="true">*</span>'
: '';
$commonAttrs = 'name="' . e($name) . '" id="' . e($id) . '"'
. ($required ? ' required' : '')
. ' aria-describedby="' . e($describedBy) . '"'
. (!empty($field['autocomplete']) ? ' autocomplete="' . e($field['autocomplete']) . '"' : '')
. (!empty($field['maxlength']) ? ' maxlength="' . e($field['maxlength']) . '"' : '')
. (isset($field['min']) ? ' min="' . e((string) $field['min']) . '"' : '')
. (isset($field['max']) ? ' max="' . e((string) $field['max']) . '"' : '');
?>
<?php if ($type === 'checkbox'): ?>
<div class="form__field form__field--checkbox">
<input type="checkbox" <?= $commonAttrs ?> value="1">
<label for="<?= e($id) ?>"><?= $field['label_html'] ?? e($field['label']) ?><?= $requiredMark ?></label>
<p class="form__error-msg" id="<?= e($errorId) ?>" hidden></p>
</div>
<?php else: ?>
<div class="form__field">
<label for="<?= e($id) ?>"><?= e($field['label']) ?><?= $requiredMark ?></label>
<?php if ($type === 'select'): ?>
<select <?= $commonAttrs ?>>
<option value=""><?= e($field['placeholder'] ?? 'Bitte wählen') ?></option>
<?php foreach ($field['options'] as $option): ?>
<option value="<?= e($option) ?>"><?= e($option) ?></option>
<?php endforeach; ?>
</select>
<?php elseif ($type === 'textarea'): ?>
<textarea <?= $commonAttrs ?> rows="<?= e($field['rows'] ?? 6) ?>"></textarea>
<?php else: ?>
<input type="<?= e($type) ?>" <?= $commonAttrs ?>>
<?php endif; ?>
<?php if (!empty($field['hint'])): ?>
<p class="form__hint" id="<?= e($hintId) ?>"><?= e($field['hint']) ?></p>
<?php endif; ?>
<p class="form__error-msg" id="<?= e($errorId) ?>" hidden></p>
</div>
<?php endif; ?>