How do I set template and labelOptions for Select2? - php

How to set my template and labelOptions in Select2 in Yii2? For a regular field, this can be done via field->labelOptions and field->template. And how to do it for Select2?
$form->field($model, 'test',[
'labelOptions' => ['style' => 'display:none !important;'],
'template' => '{label}{input}{error}'])
->radioButtonGroup($data, ['itemOptions' => ['class' => 'btn btn-default', 'style' => 'display:none !important;']
]); ?>
Select2, which is in question: https://demos.krajee.com/widget-details/select2

I asked the question himself and answered it himself, and even in the question itself. It was enough to do as in the example from the question - set field->template and field->label Options as needed:
$form->field($model, 'test', [
'template' => '{label}{input}{error}'
])->widget(Select2::classname(), [
'data' => $data,
'pluginOptions' => [
'allowClear' => true
],
]);

Related

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 :

How to pass "elementId" in barcode generator in Gridview Yii2?

I have gone through this:
http://www.yiiframework.com/extension/yii-barcode-generator-8-types/
https://github.com/Vilochane/Yii-Barcode-Generator
http://www.yiiframework.com/extension/yii2-barcode-generator-8-types/
But doesn't get it work. My gridView:
<?= GridView::widget([
'dataProvider' => new yii\data\ActiveDataProvider(['query' => $model->getLibBookMasters()]),
'summary' => '',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'lbm_book_no',
[
'attribute' => 'lbm_barcode_no',
//'type' => 'raw',
'value'=> function($model){
return \barcode\barcode\BarcodeGenerator::widget(
[
'elementId' => 'lbm_barcode_no',
'value'=> 'lbm_barcode_no',
'type'=>'ean13',
]);},
],
],
]); ?>
I need to pass elementId that do the trick but doesn't found it.
I just installed Barcode Generator and don't know how to play around with.
You need to pass different elementIds. As your code is currently your are passing the literal 'lbm_barcode_no' instead of the value of the lbm_barcode_no attribute of your models. In addition, you have to create the divs where the barcode is to be shown and set the format of the column to raw or html:
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'lbm_book_no',
[
'attribute' => 'lbm_barcode_no',
'format' => 'raw',
'value'=> function($model){
return yii\helpers\Html::tag('div', '', ['id' => 'barcode-'.$model->lbm_barcode_no]).
\barcode\barcode\BarcodeGenerator::widget([
'elementId' => 'barcode-'.$model->lbm_barcode_no,
'value'=> $model->lbm_barcode_no,
'type'=>'ean13',
]);
},
],
],
I prefixed the tags with barcode- to avoid collisions (you never know).

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