简体   繁体   English

Yii2 需要“select2”小部件的验证规则

[英]Yii2 required validation rule on “select2” widget

I'm using kartik select2 widget in Yii2 framework.我在 Yii2 框架中使用 kartik select2 小部件 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 ...',
            ]
        ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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