PHPackages                             okkebal/yii2-yaireo-tagify - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. okkebal/yii2-yaireo-tagify

ActiveYii2-extension[Framework](/categories/framework)

okkebal/yii2-yaireo-tagify
==========================

Tagify widget for Yii2 Framework — lightweight, powerful tags/select input by @yaireo

02↓100%PHP

Since May 26Pushed 2w agoCompare

[ Source](https://github.com/okkebal/yii2-yaireo--tagify)[ Packagist](https://packagist.org/packages/okkebal/yii2-yaireo-tagify)[ RSS](/packages/okkebal-yii2-yaireo-tagify/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

yii2-tagify
===========

[](#yii2-tagify)

Yii2 widget wrapping [@yaireo/tagify](https://github.com/yairEO/tagify) v4 — lightweight, powerful tags/select/mix input.

Installation
------------

[](#installation)

```
composer require okkebal/yii2-yaireo-tagify
```

Or for local development, add to `composer.json`:

```
{
    "repositories": [
        { "type": "path", "url": "/var/www/yii2-yaireo--tagify" }
    ],
    "require": {
        "okkebal/yii2-yaireo-tagify": "*"
    }
}
```

Basic widget
------------

[](#basic-widget)

```
echo \okkebal\tagify\Tagify::widget([
    'name'        => 'tags',
    'value'       => 'php,yii2',
    'placeholder' => 'Add a tag…',
    'maxTags'     => 10,
]);
```

With ActiveForm
---------------

[](#with-activeform)

```
$form = ActiveForm::begin(['fieldClass' => \okkebal\tagify\ActiveField::class]);

// Free-form tags, CSV output
echo $form->field($model, 'tags')->tagify();

// Tags with sensible defaults (no inline editing, dropdown hidden until typing)
echo $form->field($model, 'tags')->tagifyTags(['maxTags' => 20, 'maxLength' => 25]);

// Single-value select from a fixed list
echo $form->field($model, 'genre')->tagifySelect(['rock', 'pop', 'jazz']);

// Multi-select from a fixed list
echo $form->field($model, 'skills')->tagifyMultiSelect(['php', 'js', 'css', 'mysql']);

// AJAX suggestions — endpoint receives ?q=, returns ["val1","val2"]
echo $form->field($model, 'tags')->tagifyAjax(['/api/tag-suggest']);

// Mix mode with @mentions
echo $form->field($model, 'body')->tagifyMix(['Alice', 'Bob', 'Carol'], '@');
```

Direct widget in an ActiveForm field
------------------------------------

[](#direct-widget-in-an-activeform-field)

```
echo $form->field($model, 'tags')->widget(\okkebal\tagify\Tagify::class, [
    'whitelist'        => ['php', 'yii2', 'mysql'],
    'enforceWhitelist' => true,
    'maxTags'          => 5,
    'placeholder'      => 'Pick a technology…',
]);
```

All widget properties
---------------------

[](#all-widget-properties)

PropertyTypeDefaultDescription`mode``string|null``null``null` = tags, `'select'` = single dropdown, `'mix'` = inline`placeholder``string|null``null`Input placeholder text`maxTags``int|null``null`Maximum number of tags`maxLength``int|null``null`Max character length per tag`delimiters``string``','`Characters that split input into tags`whitelist``array``[]`Allowed values; enables dropdown suggestions`blacklist``array``[]`Disallowed values`enforceWhitelist``bool``false`Restrict input to whitelist only`userInput``bool``true`Allow typing new values`editTags``bool|int``false`Edit tags: `false`=off, `1`=click, `2`=dbl-click`dropdown``array``[]`Tagify dropdown sub-options`outputFormat``string``'csv'``'csv'` or `'json'` (Tagify default)`ajaxUrl``string|array|null``null`Yii route for AJAX whitelist loading`ajaxDebounce``int``300`Debounce ms for AJAX calls`clientOptions``array``[]`Raw Tagify options (merged last, highest precedence)AJAX endpoint
-------------

[](#ajax-endpoint)

Your controller action should return JSON:

```
public function actionTagSuggest()
{
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    $q = \Yii::$app->request->get('q', '');
    return Tag::find()
        ->where(['like', 'name', $q])
        ->limit(20)
        ->column(); // returns ["php", "yii2", ...]
}
```

Or with label/value pairs:

```
return Tag::find()
    ->where(['like', 'name', $q])
    ->limit(20)
    ->asArray()
    ->all(); // returns [["id"=>1,"value"=>"php"], ...]
```

Value handling
--------------

[](#value-handling)

The widget automatically converts an array model attribute to a comma-separated string before passing it to Tagify. On form submit, `outputFormat='csv'` produces `"php,yii2,mysql"`.

To store/restore as an array in your model:

```
// In afterFind / beforeValidate:
public function afterFind()
{
    parent::afterFind();
    $this->tags = $this->tags ? explode(',', $this->tags) : [];
}

public function beforeValidate()
{
    if (is_array($this->tags)) {
        $this->tags = implode(',', $this->tags);
    }
    return parent::beforeValidate();
}
```

Using without ActiveField
-------------------------

[](#using-without-activefield)

```
TagifyAsset::register($this);

echo Html::textInput('tags', 'php,yii2', ['id' => 'my-tags', 'placeholder' => 'Add…']);

$this->registerJs(
