Custom Validators

Custom Validators can be used anywhere you need to validate data, such as in Forms or Controllers.

Validators must implement Centum\Interfaces\Validator\ValidatorInterface.

Validators require the following public method:

  • validate(mixed $value): list<non-empty-string>

The validate() method returns an array of violations as strings. An empty array means the value is valid.

Example: Not Empty Validator

namespace App\Validators;

use Centum\Interfaces\Validator\ValidatorInterface;

class NotEmptyValidator implements ValidatorInterface
{
    public function validate(mixed $value): array
    {
        if (empty($value)) {
            return [
                "Value is required and can't be empty.",
            ];
        }

        return [];
    }
}

See also Centum\Validator\NotEmpty.

Example: Validator with Dependency Injection

namespace App\Validators;

use Centum\Interfaces\Validator\ValidatorInterface;
use DatePeriod;
use DateTimeInterface;

class WithinDatePeriodValidator implements ValidatorInterface
{
    public function __construct(
        protected readonly DatePeriod $datePeriod
    ) {
    }

    public function validate(mixed $value): array
    {
        if (!($value instanceof DateTimeInterface)) {
            return [
                "Value is not a DateTimeInterface",
            ];
        }

        foreach ($this->datePeriod as $dateTime) {
            if ($dateTime->format("U") === $value->format("U")) {
                return [];
            }
        }

        return [
            "Value is not in date period.",
        ];
    }
}