Yii2 not showing error message on dropdownList - php

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??

Use active field widget:
<?= $form->field($model, 'place')->widget(Select2::className(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a constituency'],
'pluginOptions' => ['allowClear' => true],
]) ?>

Related

How to hide detail view labels on yii2

I want to hide labels for the detail view
<?= DetailView::widget([
'model' => $model,
//To hide labels
'label' => ['hidden' => true],
'attributes' => [
'visits'
],
]) ?>
On the above snipet of code, there is 'label' => ['hidden' => true], is there but it is not a method which is existing. I want to know is there a method to hide labels Something which is equivalent to that.
If you want to hide label column in GridView you must modify its enter link description heretemplate. For example:
<?= DetailView::widget([
'model' => $model,
'template' => '<tr><td{contentOptions}>{value}</td></tr>',
'attributes' => [
// your attributes for displaying
],
]) ?>
If you want to hide a label in one cell you can set an empty string to label property:
<?= DetailView::widget([
'model' => $model,
'template' => '<tr><td{contentOptions}>{value}</td></tr>',
'attributes' => [
[
'attribute' => 'id',
'label' => ''
]
// your attributes for displaying
],
]) ?>

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 ...',
]
]);

Yii2 kartik datepicker multiple inline

I want a Datepicker inline with multidate enabled.
It is rendered correctly but returns only the last selected date.
Return value: '12.11.2016'
If I set 'type' to TYPE_INPUT everything is working fine.
Expected return Value: '23.11.2016, 24.11.2016, 18.11.2016, 12.11.2016'
<?= $form->field($model, 'dateString')->widget(DatePicker::className(), [
'type' => DatePicker::TYPE_INLINE,
'pluginOptions' => [
'multidate' => true,
],
]);
?>
I'm using "kartik-v/yii2-widget-datepicker": "#dev" and the model is a ActiveRecord Model.
Seems to have been fixed with this commit: https://github.com/kartik-v/yii2-widget-datepicker/commit/39e0e71277d0f115341e118a2b879a0dfcbd01c3
This will solve the problem
echo $form->field($model, 'date_time')->widget(DatePicker::classname(), [
'options' => [
'value' => "Jul-11-2020,Jul-14-2021,Jul-12-2020",
'class' => "col-md-12 form-control picker"
],
'readonly' => false,
'language' => 'en',
'type' => DatePicker::TYPE_INLINE,
'pluginOptions' => [
'format' => 'M-dd-yyyy',
'todayHighlight' => true,
'multidate' => true,
],
]);

data loading was blocked Select 2 yii2

I worked with select2 widget and it works with a single, but when I put two select2 widget the second widget is blocked. I need your help please.
this is my select2 code:
<?= $form->field($model, 'idClient')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Client::find()->all(),'idClient','nom'),
'options' => ['placeholder' => 'Selectionée le Client ...'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'idDevis')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Devis::find()->all(),'idDevis','reference'),
'options' => ['placeholder' => 'Selectionée le Devis ...'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
the result is like that :

Yii2 Doesn't Get the Value of Disabled Dropdown List or Textfield

I want to disable certain textfields and dropdown lists to prevent user from changing its values. But whenever I try to, it doesn't collect/get the data of that specific disabled textfield or dropdown list.
Here's my view where I display my dropdown lists. It's inside a for loop:
echo $form->field($model1[$i], 'earning_item_id')->widget(Select2::classname(), [
'data' => $earningslistData,
'options' => ['placeholder' => '', 'prevOptionID' => $model1[$i]->earning_item_id, 'prevOptionName' => $earningslistData[$model1[$i]->earning_item_id],
"name" => "EarningDetails[".$i."][earning_item_id]", "row_count1" => $i],
//'disabled' => true,
'pluginOptions' => [
'allowClear' => true,
'label' => false
]
]);
Here's how it looks like without disabling them:
Then, when I save it, it looks like this:
But, when I disable the dropdown lists, it will give me this:
I think the Full Name comes from my model but I don't know why:
public function getFullName()
{
return $this->user ? $this->user->fname . ' ' . $this->user->lname : 'Full Name';
}
It goes the same when I disable a textfield:
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff'],
'disabled' => true,
'pluginOptions' => [
'allowClear' => true,
],
])->label('Employee Name');
I am using Kartik widgets for my form fields.
Is there a way to fix this? Please tell me how.
EDIT
Thanks to the commenters below I found out the difference between disabled and readonly. Since it's a dropdown list, here's what I did:
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff', ],
'pluginOptions' => [
'allowClear' => true,
],
])->label('Employee Name');
echo $form->field($model, 'user_id')->widget(Select2::classname(), [
'data' => $listData,
'options' => ['placeholder' => 'Select a Staff', 'style' => 'display:none'],
'pluginOptions' => [
'allowClear' => true,
],
])->label('');
Disabled html form field will not submit, the problem is not with yii itself. The solution in this case is to have 2 copies of the same field, one as disabled as you have already included and the other one hidden with the same value as below after the original one.
echo $form->field($model1[$i], 'earning_item_id')->hiddenInput()->label('');

Categories