Zum Inhalt springen
BirdAPI powered by SKYLITE.DESIGN
Kontakt
Laravel Snippets

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

app/Rules/Uppercase.php php
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.');
        }
    }
}
Als Datei laden Im Kundenportal speichern (bald)