forked from fa/breadcrumb-the-shire
59 lines
1.4 KiB
Markdown
59 lines
1.4 KiB
Markdown
# Request Input + Validation
|
|
|
|
Letzte Aktualisierung: 2026-03-06
|
|
|
|
Kurzstandard fuer neue Actions/Endpoints.
|
|
|
|
## Regeln
|
|
|
|
- Input immer ueber `requestInput()`.
|
|
- Validierungsfehler intern immer als `FormErrors`.
|
|
- API-Validation immer mit `ApiResponse::validationFromFormErrors(...)`.
|
|
- Web-Templates bekommen kompakte Fehlerlisten (`$errors`) aus `toFlatList()`.
|
|
- `Flash` ist fuer Redirect-/Outcome-Meldungen (z. B. Success) gedacht, nicht als primaerer Validation-Container.
|
|
- Bei Redirect-POST-Formen: `FormErrors` intern sammeln und erst am Redirect-Rand transportieren (z. B. `flashFormErrors(...)`).
|
|
|
|
## Web-Beispiel (Action)
|
|
|
|
```php
|
|
$request = requestInput();
|
|
$errorBag = formErrors();
|
|
|
|
if ($request->isMethod('POST')) {
|
|
if (!Session::checkCsrfToken()) {
|
|
$errorBag->addGlobal(t('Form expired, please try again'));
|
|
}
|
|
|
|
if (!$errorBag->hasAny()) {
|
|
$result = $service->save($request->bodyAll());
|
|
$errorBag->merge($result['errors'] ?? []);
|
|
}
|
|
}
|
|
|
|
$errors = $errorBag->toFlatList();
|
|
```
|
|
|
|
## API-Beispiel (Endpoint)
|
|
|
|
```php
|
|
$request = requestInput();
|
|
$errors = formErrors();
|
|
|
|
if ($request->bodyString('name') === '') {
|
|
$errors->add('name', 'required');
|
|
}
|
|
|
|
if ($errors->hasAny()) {
|
|
ApiResponse::validationFromFormErrors($errors);
|
|
}
|
|
```
|
|
|
|
## Migration-Gates
|
|
|
|
```bash
|
|
rg -n '\\$_POST|\\$_GET|\\$_FILES|REQUEST_METHOD' pages -g '*.php'
|
|
rg -n 'ApiResponse::readJsonBody\\(' pages/api/v1 -g '*.php'
|
|
```
|
|
|
|
Erwartung jeweils: `0` Treffer.
|