Filterable
Home
📦 Installation
GitHub
Home
📦 Installation
GitHub
  • Home
  • Introduction
  • Installation
  • How It Works
  • Engines

    • Invokable
    • Tree
    • Ruleset
    • Expression
  • Features

    • Header-Driven Filter Mode
  • Authorization
  • Validation
  • Sanitization

Request validation

Important Note

All filter engines in this package support automatic request validation, except for the Closure Pipeline Engine

Overview

When using class-based filters (e.g., in the Dynamic Method Engine). you may define validation rules directly inside the filter class using protected $rules property. These rules follow Laravel's native validation format.

Before any filtering logic runs, the engine will validate incomming request data against the defined rules.

  • If validation passes, filtering proceeds as expected.
  • If validation fails, a
    ValidationException is thrown and the process stoped.

Supported Engines

EngineValidation Support
RuelsetYes
Dynamic methodYes
Tree basedYes
Closure pipelineNo
Others (if any)Yes

Example (Dynamic Method Engine)

class PostFilter extends Filterable
{
    /**
     * Registered filters to operate upon.
     *
     * @var array
     */
    protected $filters = [
        'status',
        'title',
    ];

    public function rules(): array
    {
        return [
            'status' => ['required', 'string', 'in:pending,active'],
            'title' => ['required', 'string', 'max:32'],
        ];
    }

    /**
     * Filter the query by a given status.
     *
     * @param Payload $payload
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function status($payload)
    {
        return $this->builder->where('status', $payload);
    }

    /**
     * Filter the query by a given title.
     *
     * @param Payload $payload
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function title($payload)
    {
        if ($payload) {
            return $this->builder->where('title', $payload);
        }

        return $this->builder;
    }
}

Error Handling

If the request does not satisfy the rules, the system will return a structured error.

{
  "message": "The given data was invalid.",
  "errors": {
    "status": ["The status field is required."],
    "title": ["The title field is required."]
  }
}
Edit this page
Last Updated:
Contributors: Abdalrhman Emad Saad
Prev
Authorization
Next
Sanitization