particleflux/yii2-blocklist-validator

Validate attributes against a blocklist

0.1.0 2024-04-30 18:05 UTC

This package is auto-updated.

Last update: 2024-04-30 18:08:14 UTC


README

Packagist Version (custom server) Packagist PHP Version build Maintainability Test Coverage

A Yii2 validator to block certain values

Installation

composer require particleflux/yii2-blocklist-validator

Usage

BlockListFileValidator

Block attribute values contained in a file.

public function rules(): array
{
    return [
        ['username', BlockListFileValidator::class, 'file' => '@app/config/bad-usernames.txt'],
    ];
}

Some of the behavior can be fine-tuned:

public function rules(): array
{
    return [
        [
            'username',
            BlockListFileValidator::class,
            'file' => '@app/config/bad-usernames.txt'   // the path to the blocklist file, can contain aliases
            'strict' => true,           // whether to do strict comparison (default: false)
            'useCache' => true,         // use cache component defined in 'cache' (default: true)
            'cacheTtl' => 60,           // cache TTL (default: null, meaning the component default)
            'cache' => 'customCache',   // cache component to use (default 'cache')
        ],
    ];
}