FilterableFilterable
Home
📦 Installation
  • Setting Up Filterable
  • Discover Command
  • Listing All Filters
  • Testing Filters
  • Inspecting Filterable Classes
  • Caching
GitHub
Home
📦 Installation
  • Setting Up Filterable
  • Discover Command
  • Listing All Filters
  • Testing Filters
  • Inspecting Filterable Classes
  • Caching
GitHub
  • Home
  • Introduction
  • Installation
  • Service Provider
  • How It Works
  • Engines

    • Invokable

      • Overview
      • Annotations

        • Overview
        • Authorize
        • SkipIf
        • Trim
        • Sanitize
        • Cast
        • DefaultValue
        • MapValue
        • Explode
        • Required
        • In
        • Between
        • Regex
        • Scope
    • Tree
    • Ruleset
    • Expression
  • Features

    • Lifecycle Hooks
    • Header-Driven Filter Mode
    • Auto Register Filterable Macro
    • Conditional Logic
    • Filter Aliases
    • Through callbacks
    • Auto Binding
    • Custom engines
    • Data Provisioning
  • Execution

    • Invoker
  • API Reference

    • Filterable
    • Filterable facade
    • Payload
    • Sorter
  • Caching

    • Overview
    • Getting Started
    • Strategies
    • Auto Invalidation
    • Cache Profiles
    • Scoping Cache
    • Monitoring Cached Items
    • API Reference
    • Examples
  • CLI

    • Setup Filterable
    • Discover Filters
    • Test Filter
    • List Filters
    • Inspect Filter
  • Exceptions
  • Event System
  • Profile Management
  • Profiler
  • Sorting
  • Authorization
  • Validation
  • Sanitization

#[Regex]

Stage: VALIDATE (3)

Validates that the payload value matches a given regular expression pattern. If it doesn't match, the filter is skipped.


Parameters

ParameterTypeRequiredDefaultDescription
$patternstring✅—The regex pattern to match against
$messagestring❌''Custom error message when validation fails

Usage

Alphabetic Only

use Kettasoft\Filterable\Engines\Foundation\Attributes\Annotations\Regex;

#[Regex('/^[a-zA-Z]+$/')]
protected function status(Payload $payload)
{
    return $this->builder->where('status', $payload->value);
}

Email Validation

#[Regex('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/')]
protected function email(Payload $payload)
{
    return $this->builder->where('email', $payload->value);
}

Slug Pattern

#[Regex('/^[a-z0-9]+(?:-[a-z0-9]+)*$/')]
protected function slug(Payload $payload)
{
    return $this->builder->where('slug', $payload->value);
}

Numeric Only

#[Regex('/^\d+$/')]
protected function zipCode(Payload $payload)
{
    return $this->builder->where('zip_code', $payload->value);
}

Custom Error Message

#[Regex('/^[A-Z]{2}-\d{4}$/', message: 'Invalid product code format. Expected: XX-1234')]
protected function productCode(Payload $payload)
{
    return $this->builder->where('code', $payload->value);
}

Behavior

ScenarioResult
Value matches the patternFilter executes normally
Value does not matchFilter is skipped
Value is not a stringFilter is skipped

Combining with Transform Attributes

#[Trim]
#[Sanitize('lowercase')]
#[Regex('/^[a-z0-9-]+$/')]
protected function slug(Payload $payload)
{
    // "  My-Slug-123  " → "my-slug-123" → passes regex
    return $this->builder->where('slug', $payload->value);
}
Edit this page
Last Updated:
Contributors: kettasoft
Prev
Between
Next
Scope