Yii2 select2 widget selected value does not appear when I click edit - yii2

This is my form. I have save value when I create but then when I update the value wont appear.
$form->field($model, 'unit_id')->widget(Select2::classname(), [
'data' => $getunit,
'language' => 'en',
'value'=>$model->unit_id, //cant work
'initValueText'=>$model->unit_id //cant work
'options' => ['placeholder' => 'Select','multiple'=>true,'value'=>$model->unit_id' //cant work ],
'pluginOptions' => [
'allowClear' => true,
'tags'=>true,
'maximumInputLength'=>10,
],
])->label(false)
Anyone know why? I search through online but I still cant solve it

Related

Yii2 kartik detailview hide label

If I'm doing 'label' => false, it is still showing the empty cell of the label. Is it possible completely not to render label cell somehow? Many thanks!
UPDATE: 'labelColOptions' => ['hidden' => true] works!
You can use visible property :
For example,
[
'attribute' => 'name',
'value' => whatever,
'visible' => (!empty($model->name)),
]

Yii2: Using TinyMCE into Kartik's DetailView with custom settings

I'd like to insert 2amigos' TinyMCE widget in Kartik's DetailView edit mode. This is what I got by now:
[
'attribute' => 'myAttribute',
'format' => 'raw',
'type' => 'widget',
'widgetOptions' => ['class' => TinyMce::classname()],
'value' => $model->myAttribute,
],
With this chunk I managed to show TinyMCE editor with default settings. What I'm trying to do now is to show it with custom settings defined by:
Yii::$app->params['myTinyMceParams']
In form I'm doing this:
<?= $form->field($model, 'myAttribute')->widget(TinyMce::className(), Yii::$app->params['myTinyMceParams']) ?>
Any ideas?
I finally found a solution, maybe not ideal but fully operative: to merge both 'class' array and rest-of-options array into 'widgetOptions':
'widgetOptions' => ArrayHelper::merge(['class' => TinyMce::classname()], Yii::$app->params['tinyMceParams']),

yii2 Kartik-V Typeahead Basic autocomplete on name but store integer value

Updates have been made below
I am trying to use the Kartik-V Typeahead Basic widget with the Yii2 Framework.
The code below is working to display the required data, the user can search via the university name and it appears in the autocomplete list.
The issue is, the model needs to the university id, not the name. Thus the rules are this field can only store an integer and returns a validation error once you select one of the typeahead results.
<?= $form->field($model, 'university_id')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...'],
]); ?>
I am hoping someone can help me understand if there is a setting that needs to be changed so when saving, the user friendly 'uni_name' data is changed back to the uni 'id'.
UPDATE:
I have gotten the code partly working thanks to "Insane Skull".
The new code is:
<?= $form->field($model, 'name')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...', 'id' => 'testID'],
'pluginEvents' => [
'typeahead:select' => new yii\web\JsExpression("function(event, ui) { $('#testing123').val(ui.item.id); }"),
]
]); ?>
<?= Html::activeHiddenInput($model, 'university_id', array ('id' => 'testing123'))?>
Now I am unfortunately getting the error:
Method yii\web\JsExpression::__toString() must return a string value
I would rather use Select2 instead of Typeahead, you are basically trying to implement the functionality that already exists on Select2 but using Typeahead.
<?= $form->field($model, 'university_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'options' => ['placeholder' => 'Filter as you type ...'],
]); ?>
You can use activeHiddenInput() for this purpose.
Create one public variable in model say name.
Then:
<?= $form->field($model, 'name')->widget(TypeaheadBasic::classname(), [
'data' => ArrayHelper::map(University::find()->all(),'id','uni_name'),
'pluginOptions' => ['highlight' => true],
'options' => ['placeholder' => 'Filter as you type ...'],
'select' => new yii\web\JsExpression("function( event, ui ) {
$('#id_of_hiddenField').val(ui.item.id);
}")
]); ?>
<?= Html::activeHiddenInput($model, 'university_id')?>
And in Controller Get the value of activeHiddenField.

Send serialize data on kartik file input

I am implementing \kartik\file\FileInput widget.
Here is my code:
<?php
echo FileInput::widget([
'name' => 'dataSiswa',
'options' => [
'multiple' => false
],
'pluginOptions' => [
'uploadUrl' => Url::toRoute('pesertadidikuploadproses'),
'uploadExtraData' => ['folderId' => ""],
'showUpload' => true
],
'pluginEvents' => [
'fileuploaded' => "function(event, data, previewId, index) {
$('#pesan').show();
$('#pesan').html(data.response.pesan);
}"
]
]);
?>
I want the value of uploadExtraData should be get as serialize data form --> $('#formid').serialize();
Like said in this post, you can't direct access to files on a user computer. One technique is to use and iframe to do the submission and that way you won't have to refresh the page.
So use this plugin and just do something as below:
$(function() {
$('#ifoftheform').ajaxForm(function(result) {
alert('the form was successfully processed');
});
});

Multiple values in one field Yii2

How to display multpiple values in one field.. I Use Select2 Widget. If i use $courses_model[0] it display only one value
Controller
public function actionUpdateteachers($id)
{
$courses_model = ReferenceTeachersCourses::find()->where(['reference_teachers_id' => $id])->all();
.....
}
View
...
<?= $form->field($courses_model[0], 'reference_course_type_id')->widget(Select2::classname(), [
'data' =>ArrayHelper::map($courses,'id','name'),
'options' => ['multiple' => true],
'pluginOptions' => [
'allowClear' => true,
],
]);
...
?>
This widgets works fine - but your $courses_model[0]->reference_course_type_id must have an array of ids as a value if you want to see multiple values selected.