跳转到内容
PHP Ecosystem
Explore

Select2

此内容尚不支持你的语言。

Searchable Select2 widgets for Yii2 forms

ActiveForm integration, multiple selections, tagging, and paginated AJAX searches

Configure Foxy native and the asset aliases in the installation guide, then install:

Terminal window
composer require --prefer-dist --prefer-stable php-forge/foxy:^0.4 yii2-extensions/select2:^0.2
use Yii2\Extensions\Select2\Select2;
echo $form
->field($model, 'country')
->widget(
Select2::class,
[
'items' => [
'mx' => 'Mexico',
'ca' => 'Canada',
],
'options' => ['prompt' => ''],
'clientOptions' => [
'placeholder' => 'Choose a country',
'allowClear' => true,
],
],
);
use yii\helpers\Html;
use Yii2\Extensions\Select2\Select2;
echo Html::label('Regions', 'regions');
echo Select2::widget(
[
'name' => 'regions',
'items' => [
'north' => 'North',
'south' => 'South',
],
'options' => [
'id' => 'regions',
'multiple' => true,
'unselect' => '',
],
],
);

Set clientOptions.tags=true separately to allow new values, and validate them on the server.

The widget uses Select2 4.0 and requires jQuery. Its native asset installation consumes published files without compiling them. See the installation guide for the Yii 22 client-validation adapter and frontend compatibility details.

Practical guides and usage examples for Select2 widgets, forms, and remote searches.

The guides and the API reference generated from the PHPDoc in src/ are published at https://yii2-framework.github.io/docs/extensions/select2/.

AI agents: read the documentation index and fetch the Markdown version of a page by appending index.md to its URL. The AI agents guide covers skill installation, client setup and verification.

The hosted documentation covers Yii 22.0. If it does not match the installed revision or is unavailable, use the repository guides, the skill source, the source PHPDoc and the changelog.

This code was initially developed by contributors at yii2-cms and hosted at yii-cms/yii2-select2. We thank them all. For continuity and maintenance we have moved a copy here.

Follow on X Follow on Facebook Join our Subreddit Join on Telegram

License