kartik-v / yii2-widgets

Collection of useful widgets for Yii Framework 2.0
http://demos.krajee.com/widgets
Other
558 stars 175 forks source link

Couldn't use kartik\Select2 widget because of error Failed to instantiate component or class "yii\bootstrap\BootstrapAsset" #389

Closed svetlika closed 1 year ago

svetlika commented 1 year ago

Hi!

I want to use kartik\select2 widget in my Yii2 project, so I install it via composer and place into my view the example from documentation and get this error:

Not instantiable – yii\di\NotInstantiableException Failed to instantiate component or class "yii\bootstrap\BootstrapAsset". ↵ Caused by: ReflectionException Class yii\bootstrap\BootstrapAsset does not exist

The example which doesn't work is

  echo  kartik\widgets\Select2::widget([
          'bsVersion' => '5.x',
          'language' => Yii::$app->language,
          'name' => 'pagesize',
          'hideSearch' => true,
          'data' => [
            10 => 10,
            20 => 20,
            30 => 30,
            40 => 40,
            50 => 50,
            75 => 75,
            100 => 100,
          ],
          'value' => [(isset($_GET['pagesize']) ? $_GET['pagesize'] : 20 )], // initial value
          'options' => ['placeholder' => 'Select page size', 'id'=>'pagesize'],
          'pluginOptions' => [
              'allowClear' => true
          ],
        ]);

I use Bootstrap 5 and on that page I also use another kartik widget - kartik DatePicker for the filter in kartik GridView. And they both (gridview and datepicker) are working, although I have some troubles with the kartik gridview at the beginning- there was an error with bootstrap dropdown, but when I place the hidden div with id="w0" the js error dissapeared.

Anyway, they are working fine, but kartik widget Select2 - doesn't work at all.

As I understand, this widget couldn't find the bootstrap assets, but I don't understand why and what to do. I couldn't find any information about this or similar issue, so I decided to write it. Could you please help me? I am new to Yii2.

My AppAsset looks like this:

namespace app\assets;

use yii\web\AssetBundle;

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/sbadmin.css',
        'css/site.css',
    ];
    public $js = [
        'js/sbadmin.js',
        'js/site.js',
        //js переопределяющий yii.confirm
        'js/yii.confirm.overrides.js',
    ];
    public $depends = [
        'app\assets\FontAwesomeAsset',
        'yii\web\YiiAsset',
        'yii\bootstrap5\BootstrapAsset',
        'yii\bootstrap5\BootstrapPluginAsset',
        'app\assets\BootboxAsset',
    ];
}

And my composer.json now looks like this, because I try to install Select2 separately and after I try to install it with all widgets, but none of them is working (kartik\selet2\select2 or kartik\widgets\select2). I tried also to use other widget from this widgets pack (kartik/fileInput) and it generated the same error.

    "require": {
        "php": ">=7.4.0",
        "yiisoft/yii2": "~2.0.45",
        "yiisoft/yii2-bootstrap5": "~2.0.2",
        "yiisoft/yii2-symfonymailer": "~2.0.3",
        "kartik-v/yii2-grid": "dev-master",
        "kartik-v/yii2-bootstrap5-dropdown": "dev-master",
        "kartik-v/yii2-widget-select2": "dev-master",
        "kartik-v/yii2-mpdf": "dev-master",
        "kartik-v/yii2-widget-datepicker": "*",
        "kartik-v/yii2-field-range": "*",
        "kartik-v/yii2-widgets": "dev-master",
        "bower-asset/font-awesome": "^6.4",
        "bower-asset/bootbox": "*",
        "bower-asset/select2": "*"
    },

By the way, If I remove option "bsVersion" from the widget params, then I get another error:

You must install 'yiisoft/yii2-bootstrap' extension for Bootstrap 3.x version support. Dependency to 'yii2-bootstrap' has not been included with 'yii2-krajee-base'. To resolve, you must add 'yiisoft/yii2-bootstrap' to the 'require' section of your application's composer.json file and then run 'composer update'.

Can you tell me what I am doing wrong here, please?

kartik-v commented 1 year ago

Set bsVersion to 5.x in your Yii2 config params file and try.

svetlika commented 1 year ago

Thanks, it helped. Now this widgets works separately on the page.

P.S. I also have discovered, that these widgets works inside the grid without putting param "bsVersion" into my config params.