简体   繁体   中英

Yii2 required validation rule on “select2” widget

I'm using kartik select2 widget in Yii2 framework. Required validation rule doesn't work on it.
Here is my view code:

$form->field($model, 'city')->widget(\kartik\select2\Select2::classname(), [
            'data' => $cities,
            'options' => [
                'class' => 'form-control',
                'placeholder' => 'Please select city...',
                'multiple' => false,
            ],
            'pluginOptions' => [
                'allowClear' => true
            ],
        ])->label('City');

Here is my model rule code:

[['city'], 'required'],
[['city'], 'integer']

Any idea to make the dropdown required?

Use this in plugin option

pluginOptions' => [                   
                'initialize' => true,
            ],

This is working code for me

 $form->field($model, 'tech_type')->widget(Select2::classname(), [
            'options'=>['id'=>'tech-id'],
            'data' => ArrayHelper::map(Techtypes::find()->asArray()->all(), 'tech_id', 'tech_type'),
            'pluginOptions'=>[
                'initialize' => true,
                'placeholder' => 'Select Technician Type ...',
            ]
        ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM