raoptimus/yii2-composite-validator

Nested validators base class for Yii2 Framework

Installs: 14 715

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 4

Forks: 1

Open Issues: 0

Type:yii2-extension

1.0.0 2018-10-03 11:12 UTC

This package is auto-updated.

Last update: 2024-12-04 21:37:07 UTC


README

Stable Version Untable Version License Total Downloads Build Status

yii2-composite-validator

Composite Validator for Yii2 Framework

Installation

Install with composer:

composer require raoptimus/yii2-composite-validator

Usage samples

Create any simple composite validator:

class StringDefaultValidator extends CompositeValidator
{
    /** @var string */
    public $defaultValue;
    /** @var int */
    public $max;
    /** @var int */
    public $min;

    /**
     * @inheritdoc
     */
    protected function validators(): array
    {
        return [
            [StringValidator::class, 'max' => $this->max, 'min' => $this->min],
            [DefaultValueValidator::class, 'value' => $this->defaultValue],
        ];
    }
}

Create any form with composite validator

class DefaultForm extends Model
{
    /**
     * @var string
     */
    public $field;

    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['field'], StringDefaultValidator::class, 'max' => 50, 'defaultValue' => 'test'],
        ];
    }
}

Use form validate

$form = new DefaultForm();
$form->validate();

$form->field returns string 'test'