DBM Validation System

The documentation is constantly evolving and updated alongside the project's progress. Since it is developed in parallel with the code, some sections may temporarily refer to earlier versions of the system-these are gradually adapted to the current state of the application. If you notice any outdated information, please report it or open an Issue / Pull Request.

Documentation

# DBM Validation System

# Overview

The Validator class provides a flexible and extensible way to validate form data.

# Features

  • Rule-based validation system
  • Custom validation rules
  • CSRF protection support
  • Translation support (multi-language)
  • Normalized error output (API-friendly)
  • Extendable via inheritance

# Basic Usage

# Without Translation

use Dbm\Validation\Validator;

$validator = new Validator();
$errors = $validator->rules([
    'email' => ['required', 'email'],
], $data);

# With Translation

$validator = new Validator($translation);
$errors = $validator->rules($rules, $data);

# Validation Flow

  1. Define rules
  2. Pass input data
  3. Validation runs through each rule
  4. Errors are collected and returned
$errors = $validator->rules($rules, $data);

if ($validator->isValid()) {
    // success
}

# Available Validation Rules

# Required

'required'

Field must not be empty.


# String

'string'

Value must be a string.


# Min Length

'min:3'

Minimum number of characters.


# Max Length

'max:255'

Maximum number of characters.


# Email

'email'

Valid email format.


# URL

'url'

Valid URL format.


# Phone

'phone'

Supports formats like:

  • 123 123 123
  • +48 123 123 123

# Letters & Spaces

'letters_spaces'

Allows only:

  • letters
  • spaces
  • ' and -

# Password

'password'

Requirements:

  • 8–30 characters
  • at least one lowercase letter
  • one uppercase letter
  • one digit
  • one special character

# Confirmed

'confirmed'

Checks if value matches:

  • {field}_confirmation
  • {field}_repeat

# Regex

'regex:/pattern/'

Custom regex validation.


# CSRF

'csrf'

Requires registering CSRF rule first.


# CSRF Protection

# Register CSRF Rule

$validator->registerCsrfRule($csrfManager);

# Usage

'form_csrf' => ['required', 'csrf']

# Custom Rules

You can define your own validation rules:

$validator->addRule('alpha_dash', function ($field, $value) {
    return preg_match('/^[A-Za-z0-9_-]+$/', $value)
        ? null
        : 'Invalid format';
});

# Error Handling

# Default Error Format

[
    'error_email' => 'Field email is required.'
]

# Normalized Errors (API-friendly)

$validator->getNormalizedErrors();

Output:

[
    'email' => 'Field email is required.'
]

# Translation Support

# Translation File Example

return [
    'validation.required' => 'The :field field is required.',
    'validation.email' => 'The :field must be a valid email address.',
];

# Configuration

Set available languages in .env:

APP_LANGUAGES=en,pl

# Placeholder Replacement

Supported placeholders:

  • :field
  • :value

Example:

'validation.min' => 'Field :field must be at least :value characters.'

Create form-specific validator:

class UserForm extends Validator
{
    public function validate(array $data): array
    {
        $this->rules([
            'email' => ['required', 'email'],
        ], $data);

        return $this->getErrors();
    }
}

# Advanced Example (Real Case)

# PanelUserForm

$form = new PanelUserForm($repository, $csrf, $translation);

$errors = $form->validateCreate($_POST);

# Features Demonstrated

  • Base validation rules
  • Conditional validation (create vs update)
  • Password confirmation
  • Database uniqueness checks
  • CSRF validation

# Conditional Validation

Example from real usage:

if (!$id) {
    // create logic
}

if ($id && $password) {
    // update logic
}

# Data Normalization

Optional preprocessing:

$data = $this->normalize($data);

Removes unnecessary spaces from strings.


# Internal Architecture

# Key Methods

MethodDescription
-------------------------------------
rules()Runs validation
applyRule()Applies single rule
registerError()Stores error
addRule()Adds custom rule
trans()Handles translations

# Best Practices

  • Always validate input before processing
  • Use normalizedErrors() for API responses
  • Keep validation logic inside Form classes
  • Use translation system for user-facing errors
  • Separate business logic from validation

# Summary

The DBM validation system is:

  • Lightweight
  • Extensible
  • Translation-ready
  • API-friendly
  • Framework-agnostic

Perfect for both:

  • simple forms
  • complex admin panels