sovngare/yii2-array-validator

Array validator for for the Yii framework

v1.0 2020-12-29 15:57 UTC

This package is auto-updated.

Last update: 2024-03-29 04:19:43 UTC


README

The preferred way to install this extension is through composer.

composer require sovngare/yii2-array-validator

Usage

Use class: sovngare\validators\ArrayValidator

Example №1

Validate true if property "fields" will be array.

public function rules()
{
    return [
        ['fields', 'required'],
        ['fields', ArrayValidator::class]
    ];
}

Example №2

Validate true if status[code] key will be integer in the range 100 and 500.

public function rules()
{
    return [
        ['fields', ArrayValidator::class, 'key' => 'status.code', 'rules' => [
            ['integer', 'min' => 100, 'max' => 500]
        ]]
    ];
}

Example №3

By default key label is "Attribute". Model errors log:

{
    "fields": [
        "Attribute must be no less than 100."
    ]
}

To change label you must send param label:

public function rules()
{
    return [
        ['fields', ArrayValidator::class, 'key' => 'status.code', 'rules' => [
            ['integer', 'min' => 100, 'max' => 500]
        ], 'label' => 'status_code']
    ];
}

Model errors log now:

{
    "fields": [
        "Status Code must be no less than 100."
    ]
}

Validate images

Data from client:

phone:7087952412
fields[name]:Sovngare
fields[avatar]:(\yii\web\UploadedFile)

Model:

class TestModel extends Model
{
    public $phone;
    public $fields;

    public function rules()
    {
        return [
            [['phone', 'fields'], 'required'],
            ['fields', ArrayValidator::class, 'key' => 'name', 'rules' => [
                ['required'],
                ['string', 'length' => [4, 16]]
            ], 'label' => 'name'],
            ['fields', ArrayValidator::class, 'key' => 'avatar', 'rules' => [
                ['required'],
                ['image', 'maxSize' => 1024 * 1024 * 3]
            ], 'label' => 'avatar'],
        ];
    }
}

Controller

public function actionIndex()
{
    $model = new TestModel();
    $model->attributes = Yii::$app->request->getBodyParams();

    if (is_array($model->fields)) {
        $model->fields['avatar'] = UploadedFile::getInstanceByName('fields[avatar]');
    }

    if (!$model->validate()) {
        return $model->errors;
    }

    return $model->attributes;
}