News

Composite Validation Rules

Published
Composite Validation Rules image

Validation Composite is a package by Paul Klimov that allows uniting several validation rules into a single one for easy reuse.

Here are a few simple examples that will help visualize how this package works. Let’s say you want to centralize your password or avatar rules into one composite rule:

<?php
 
namespace App\Rules;
 
use Illuminatech\Validation\Composite\CompositeRule;
 
class PasswordRule extends CompositeRule
{
protected function rules(): array
{
return ['string', 'min:8', 'max:200'];
}
}
 
class AvatarRule extends CompositeRule
{
protected function rules(): array
{
return ['file', 'mimes:png,jpg,jpeg', 'max:1024'];
}
}

Example usage of these composite rules looks like this in a controller:

public function update(Request $request)
{
$validatedData = $request->validate([
'password' => ['required', new PasswordRule],
'avatar' => ['required', new AvatarRule],
// ...
]);
 
// ...
}

You can learn more about this package, get full installation instructions, and view the source code on GitHub at illuminatech/validation-composite.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Laravel Auditor Audits Your App With Your Own AI Agent image

Laravel Auditor Audits Your App With Your Own AI Agent

Read article
A simple form builder that stays out of your way image

A simple form builder that stays out of your way

Read article
Laravel AI: Trace Agent Runs With Lifecycle Events image

Laravel AI: Trace Agent Runs With Lifecycle Events

Read article
Laravel AI: Get Raw HTTP Responses and Rate Limits image

Laravel AI: Get Raw HTTP Responses and Rate Limits

Read article
Agent Run Observability in Laravel AI SDK 0.11 image

Agent Run Observability in Laravel AI SDK 0.11

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article