information overload

by sebastian de deyne

Laravel closure validation rules

16 Jun 2023

Today I was looking for a way to create a custom Laravel validation rule without the overhead of a new class. The rule I needed would only be used in one place, so wanted to keep it close to (or in) the request class.

Upon re-reading the validation docs, I learned that Laravel supports closures as rules.

class JournalEntryRequest extends Request
{
    public function rules(): array
    {
        return [
            // …
            'lines' => [
                function (string $attribute, mixed $value, Closure $fail) {
                    $debit = collect($value)->where('type', 'debit')->sum('amount');
                    $credit = collect($value)->where('type', 'credit')->sum('amount');

                    if ($debit !== $credit) {
                        $fail("Debit and credit don't match.");
                    }

                    if ($debit !== 0) {
                        $fail("Amount must be greater than 0.");
                    }
                },
            ]
        ];
    }
}

Just what I needed!

Information Overload newsletter

I occasionally send out a dispatch with personal stories, things I'm working on, and interesting links I come across.

Only for occasional updates. No tracking.