3

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?

Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65
Ali Tavafi
  • 443
  • 1
  • 10
  • 28

1 Answers1

0

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 ...',
            ]
        ]);
yogesh godse
  • 110
  • 1
  • 7