Reputation: 906
I created a dropdownList for my project and I made the field as required in my model rules.
In view
use app\models\Constituency;
use yii\helpers\ArrayHelper;
use kartik\widgets\Select
<?php
$constituency=Constituency::find()->all();
$listData=ArrayHelper::map($constituency,'constituency','constituency');
?>
<?php
echo '<label class="control-label">Constituency</label>';
echo Select2::widget([
'model' => $model,
'attribute' => 'place',
'data' => $listData,
'options' => ['placeholder' => 'Select a constituency'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
In Models
['place', 'required','message'=>'Place is required'],
Even I tired to give a custom error message for the field,But everything I tried was failed.
I am attaching the screenshot of the page here.
As you can see its shows no required error.
Can anyone tell me what I am missing??
Upvotes: 2
Views: 795
Reputation: 18021
Use active field widget:
<?= $form->field($model, 'place')->widget(Select2::className(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a constituency'],
'pluginOptions' => ['allowClear' => true],
]) ?>
Upvotes: 2