Entwicklerportal
CodeGewölbe
Kostenlose Sammlung für Entwickler, Admins und Website-Betreiber: Snippets, VelinStyle-Komponenten, VelinScript-Automationen und Security-Rezepte — copy-paste-fertig.
Custom Validation Rule
Wiederverwendbare Validierungsregel als Invokable Rule.
Fortgeschritten
10 Min.
Problem
Komplexe Validierung soll an mehreren Stellen gleich funktionieren.
Lösung
Invokable Rule mit __invoke implementieren.
Code
namespace App\Rules;
class Uppercase implements \Illuminate\Contracts\Validation\ValidationRule
{
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
if (strtoupper((string) $value) !== (string) $value) {
$fail('Das Feld :attribute muss Großbuchstaben enthalten.');
}
}
}