Selected Value is not showing in Kartik Select2 multiple select - yii2

This is my view file:
<?php
$data = array();
echo '<label class="control-label">Attribute Group Name</label>';
echo Select2::widget([
'name' => 'attribute_grp_name',
'data' => $attribute_group_name, // initial value
'options' => ['placeholder' => 'Please Enter Attribute Group Name', 'multiple' => true],
'pluginOptions' => [
'attribute_grp_name' => true,
'maximumInputLength' => 100,
],
]);
?>
In my view here selected data is not showing.
This is my controller for create and update function:
public function actionCreate()
{
$model = new AttributeSet();
$attribute_group = AttributeGroupLang::find()->select('*')->all();
$attribute_group_name = ArrayHelper::map($attribute_group, 'id_attribute_group', 'name');
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$post_array = Yii::$app->request->post();
foreach ($post_array['attribute_grp_name'] as $key => $value) {
$attribute_set_group_combination = new AttributeSetGroupCombination();
$attribute_set_group_combination->id_attribute_set = $model->id_attribute_set;
$attribute_set_group_combination->id_attribute_group = $value;
$attribute_set_group_combination->save(false);
}
return $this->redirect(['view', 'id' => $model->id_attribute_set]);
} else {
return $this->render('create', [
'model' => $model,
'attribute_group_name' => $attribute_group_name,
]);
}
}
This is update function:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$attribute_set_group_combination = AttributeSetGroupCombination::find()->where(['id_attribute_set' => $id])->all();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$post_array = Yii::$app->request->post();
if(!empty($post_array['attribute_grp_name'])){
AttributeSetGroupCombination::deleteAll('id_attribute_set = '.$id);
foreach ($post_array['attribute_grp_name'] as $key => $value) {
$attribute_set_group_combination = new AttributeSetGroupCombination();
$attribute_set_group_combination->id_attribute_set = $model->id_attribute_set;
$attribute_set_group_combination->id_attribute_group = $value;
$attribute_set_group_combination->save(false);
}
}
return $this->redirect(['view', 'id' => $model->id_attribute_set]);
} else {
return $this->render('update', [
'model' => $model,
'attribute_set_group_combination' => $attribute_set_group_combination,
'attribute_group_name' => $this->getExistAttrSet($id),
]);
}
}
public function getExistAttrSet($id){
$query = new Query;
$query->select(['attribute_group_lang.name AS attribute_group_name','attribute_group_lang.id_attribute_group'])
->from('attribute_set_group_combination')
->join('LEFT OUTER JOIN', 'attribute_set', 'attribute_set.id_attribute_set =attribute_set_group_combination.id_attribute_set')
->join('LEFT OUTER JOIN', 'attribute_group_lang', 'attribute_group_lang.id_attribute_group =attribute_set_group_combination.id_attribute_group')
->where('attribute_set.id_attribute_set='.$id)
->all();
$command = $query->createCommand();
$model = $command->queryAll();
$data = array();
foreach ($model as $attrsetlist[]) {
$data = ArrayHelper::map($attrsetlist, 'id_attribute_group', 'attribute_group_name');
}
return $data;
}
Can anyone help me that how can i show the selected value in the multiple select field.

You just need to pass in model value:
<?php
echo kartik\select2\Select2::widget([
'name' => 'some_field_name',
// ----------------------
'value' => 'a',
//'value' => ['a', 'b'],
//'value' => $model->some_value,
// ----------------------
'data' => ['a'=>'a', 'b'=>'b', 'c'=>'c'],
'options' => ['placeholder' => 'Select menu items ...', ],
]);
?>

Related

Save multiple selections from a listbox - Yii2

I have made a Listbox depend on a dropDownList, when selecting an option from the dropDownList brings me a list of data that is added to the Listbox, it works to save a single option but the problem occurs when trying to save multiple selections, I cannot save more than 1 option, I have tried to add a foreach in my controller but it throws an error.
DropDownList
<?php echo $form->field($model, 'group_id')->widget(Select2::classname(), [
'data' => $seccion->lgrupo, //I get the group list
'size' => Select2::MEDIUM,
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'placeholder' => '-- '.Yii::t('backend', 'Select').' --',
'onchange'=>'
$.post( "lists?id="+$(this).val(), function( data ) {//I get the list of people registered in the group and send it to the listbox
$( "select#assignment-user_id" ).html( data );
});',
],
'pluginOptions' => [
'allowClear' => true,
],
'addon' => [
'prepend' => [
'content' => Html::icon('building')
],
]
]); ?>
ListBox
<?php echo $form->field($model2, 'users_id')->listBox([] ,['multiple'=>true,'size'=>17]
); ?>
Groups Controller
public function actionCreate()
{
$model = new Groups();
$model2 = new Assignment();
$seccion = new Group();
if ($model->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post())) {
if ($model->save(false)) {
foreach ($model2->users_id as $i => $as) {
$as->assign_group_id = $model->id_group_list;
if ($model2->save()) {
} else {
// error in saving model
}
}
return $this->redirect(['view', 'id' => $model->id_group]);
}
}
return $this->render('create', [
'model' => $model,
'model2' => $model2,
'seccion' => $seccion,
]);
}
Tables
I hope your can tell me what I'm doing wrong.
public function actionCreate()
{
$model = new Groups();
$model2 = new Assignment();
$seccion = new Group();
if ($model->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post())) {
if ($model->save(false)) {
foreach ($model2->users_id as $user_id) {
$assignmentModel = new Assignment();
$assignmentModel->user_id= $user_id;
$assignmentModel->assign_group_id = $model->id_group_list;
//$assignmentModel->area= ''; //if you want to set some value to these fields
//$assignmentModel->assignment= '';
if ($assignmentModel->save()) {
} else {
// error in saving model
}
}
return $this->redirect(['view', 'id' => $model->id_group]);
}
}
return $this->render('create', [
'model' => $model,
'model2' => $model2,
'seccion' => $seccion,
]);
}

Yii2 insert data from CheckBoxList

In my Form:
<?= GridView::widget([
'id' => 'griditems',
'dataProvider' => $dataProvider,
'columns' => [
'receiver_name',
'receiver_phone',
'item_price',
'shipment_price',
['class' => 'yii\grid\CheckboxColumn'],
],
]); ?>
In my Controller:
public function actionCreate()
{
$model = new Package();
$searchModel = new ShipmentSearch();
$searchModel->shipment_position=1; // الشحنه في مقر الشركة
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if ($model->loadAll(Yii::$app->request->post())){
$select = Yii::$app->request->post('selection');
foreach($select as $id){
$shipment= shipment::findOne((int)$id);
$model->company_id=1;
$model->shipment_id=$shipment->id;
$model->send_from=$shipment->send_from;
$model->send_to=$shipment->send_to;
$model->save(false);
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'dataProvider'=>$dataProvider,
]);
}
}
I just try this way to insert data as above its works but the data inserted just last one or last CheckBox from the list. I tried to print the id that in foreach and there is more than one id.
That is beacuse you're reusing the same instace for each save() inside of foreach, so you're overwriting the same model over and over. You need to place $model = new Package() inside of foreach:
foreach($select as $id){
$shipment= shipment::findOne((int)$id);
$model = new Package();
$model->company_id=1;
$model->shipment_id=$shipment->id;
$model->send_from=$shipment->send_from;
$model->send_to=$shipment->send_to;
$model->save(false);
}

Yii2 Dynamic form update fail on kartik-Select2

I am using wbraganca dynamic form widget. It works fine for the Create action.
Let me thanks for those guys making great tutorial video on youtube!!!
I am working on the Update action now. I work it on a purchase order function.
the controller of update action :
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelsItem = $model->purchaseitems;
if ($model->load(Yii::$app->request->post()) ) {
$oldIDs = ArrayHelper::map($modelsItem, 'purchaseitem_id', 'purchaseitem_id');
$modelsItem = Model::createMultiple(Purchaseitem::classname(), $modelsItem);
Model::loadMultiple($modelsItem, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsItem, 'purchaseitem_id', 'purchaseitem_id')));
$valid = $model->validate();
$valid = Model::validateMultiple($modelsItem) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) {
Purchaseitem::deleteAll(['purchaseitem_id' => $deletedIDs]);
}
foreach ($modelsItem as $modelItem) {
$modelItem->purchaseitem_purchaseorder_id = $model->purchaseorder_id;
$modelItem->purchaseitem_description = Inventory::findOne($modelItem->purchaseitem_inventory_id)->inventory_partno;
if (! ($flag = $modelItem->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->purchaseorder_id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
//return $this->redirect(['view', 'id' => $model->purchaseorder_id]);
} else {
return $this->render('update', [
'model' => $model,
'modelsItem' => (empty($modelsItem)) ? [new Purchaseitem] : $modelsItem
]);
}
}
But I think the problem may happen on the view file, as the Select2 field can show the value, which is the 'id' of the product rather than the product code.
view:
<div class="panel panel-default">
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper',
'widgetBody' => '.container-items',
'widgetItem' => '.item',
'limit' => 50,
'min' => 1,
'insertButton' => '.add-item',
'deleteButton' => '.remove-item',
'model' => $modelsItem[0],
'formId' => 'dynamic-form',
'formFields' => [
'purchaseitem_inventory_id',
'purchaseitem_qty',
'purchaseitem_cost_usd',
'purchaseitem_deliverydate',
],
]); ?>
<?php foreach ($modelsItem as $i => $modelItem): ?>
<div class="item">
<?php
// necessary for update action.
if (! $modelItem->isNewRecord) {
echo Html::activeHiddenInput($modelItem, "[{$i}]purchaseitem_id");
}
?>
<div class="row">
<?= $form->field($modelItem, "[{$i}]purchaseitem_inventory_id")->widget(
Select2::classname(), [
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 2,
'language' => [
'errorLoading' => new JsExpression("function () { return 'Error on finding results...'; }"),
],
'ajax' => [
'url' => Url::toRoute('inventory/inventorylist'),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(purchaseitem_inventory_id) { return purchaseitem_inventory_id.text; }'),
'templateSelection' => new JsExpression('function (purchaseitem_inventory_id) { return purchaseitem_inventory_id.text; }'),
],
])->label(false) ?>
<?= $form->field($modelItem, "[{$i}]purchaseitem_qty")->textInput(['maxlength' => true])->label(false) ?>
<?= $form->field($modelItem, "[{$i}]purchaseitem_cost_usd")->textInput(['maxlength' => true])->label(false) ?>
<?= $form->field($modelItem, "[{$i}]purchaseitem_deliverydate")->widget(
DatePicker::className(), [
'options' => [
['placeholder' => 'Please enter delivery date'],
],
'removeButton' => false,
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy-mm-dd',
'todayHighlight' => true,
]
]
)->label(false) ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
I have a thought that the problem maybe related to that few lines of JsExpression function.
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(purchaseitem_inventory_id) { return purchaseitem_inventory_id.text; }'),
'templateSelection' => new JsExpression('function (purchaseitem_inventory_id) { return purchaseitem_inventory_id.text; }'),
For the Select2 query URL method is here:
public function actionInventorylist($q = null, $id = null) {
Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
$out = ['results' => ['id' => '', 'text' => '']];
if (!is_null($q)) {
$query = new Query;
$query->select('inventory_id AS id, inventory_partno AS text')
->from('inventory')
->where(['like', 'inventory_partno', $q])
->limit(10);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
}
elseif ($id > 0) {
$out['results'] = ['id' => $id, 'text' => Inventory::find($id)->inventory_partno];
}
return $out;
}
I can load the record, when I click in the update view. Most of the data are feed in right place of the form, except the 'partno' field. I use Select2 to let user select partno by text and store the 'id' in table. It works on the Create view.
but in the update view, it only show the 'id' instead of the 'partno'.
if I make input to the field, I can select 'other' partno only, let me explain here:
if there are 2 code, "ABC" with 'id' is 1, "XYZ" with 'id' 2.
the record original is "ABC", the field show "1".
If I input "XYZ", it will show "XYZ" as normal effect of widget. But, if I change back to "ABC", it will show "1" instead of "ABC".
And the form also cannot submit for update. the button click with no effect.
I am new to Yii2 framework, and quite stuck on this issue, does anyone knows how can I solve this?
THANKS!!!!
I just solve the problem, a few issue happened actually.
To solve the Select2 widget cannot display the partno instead of the ID, I find the partno by the ID and feed it with initValueText in Select2. For my case:
$partno = empty($modelItem->purchaseitem_inventory_id) ? '':Inventory::findOne($modelItem->purchaseitem_inventory_id)->inventory_partno;
$form->field($modelItem, "[{$i}]purchaseitem_inventory_id")->widget(Select2::classname(), ['initValueText' => $partno, ...
About the update POST fail issue, I got error of Getting unknown property: backend\models\Purchaseitem::id, and I found it happened on the Dynamic-form widgets Model.php line 25. That lines is $keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));
If I change the 'id' to my ID field name, i.e 'purchaseitem_id', it will work, but this model will only work for this Id field name afterward. So I get the model's primaryKey and make it work for my other model.
add this line $primaryKey = $model->tableSchema->primaryKey; and modify above line $keys = array_keys(ArrayHelper::map($multipleModels, $primaryKey, $primaryKey));

selected data in multiple checkbox not recognized

I am getting frusted because the edit.ctp is not recognizing the stored data within a multible value checkbox. This is my code:
Controller:
public function edit($id = null) {
$this->set('userfunc', $this->Auth->user('userfunction_id'));
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
**$selection = explode('#',$user->member);**
$userfunctions = $this->Users->Userfunctions->find('list', ['limit' => 200]);
$userappartments = $this->Users->Userappartments->find('list', ['limit' => 200]);
$userparkings = $this->Users->Userparkings->find('list', ['limit' => 200]);
$this->set(compact('user', 'userfunctions', 'userappartments', 'userparkings', 'selection'));
$this->set('_serialize', ['user']);
}
edit.ctp:
<?php
echo $this->Form->label('User.status', 'Status');
$options = $statusList;
echo $this->Form->radio('status', $statusList);
echo $this->Form->label('User.member', 'Mitglied');
echo $this->Form->input('member', ['options' => $memberList, 'multiple' => 'checkbox', 'label' => false, 'selected' => $selection]);
echo $this->Form->input('userappartment_id', ['options' => $userappartments, 'label' => 'Wohnung']);
echo $this->Form->input('userparking_id', ['options' => $userparkings, 'label' => 'Parkplatz']);
echo $this->Form->input('contact', ['label' => 'Kontakt']);
?>
================
print_r is displaying an array that looks fine:
enter image description here
but the field in the form is emty:
enter image description here

Undefined variable: model2

I have 2 models that will load in one form.
But when I want to access the second model, there is error said "Undefined variable: model2"
Please help.
This is the controller
InventoryController.php
public function actionInsert() {
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
$model = new Inventory();
$model2 = new \app\models\Unitofmeasurement();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$command = $connection->createCommand('{call usp_M_Inventory#Transaksi(:ID_Item,:Item_Name, :IDMom, :Item_Price, :ID_InvCategory,:Item_PIC1,
:Item_PIC2,:Item_active, :UserInventory, :ID_Mom, :Satuan_Beli, :Qty_Beli, :Satuan_Jual,:Qty_Jual, :ActiveMOM, :UserMOM)}');
$ID_Item = $model->ID_Item;
$Item_Name = $model->Item_Name;
$IDMom = $model->IDMom;
$Item_Price = $model->Item_Price;
$ID_InvCategory = $model->ID_Inv_Category;
$Item_PIC1 = $model->Item_PIC1;
$Item_PIC2 = $model->Item_PIC2;
$Item_active = $model->Item_active;
$UserInventory = Yii::$app->user->identity->username;
$ID_Mom = $model2->ID_Mom;
$Satuan_Beli = $model2->Satuan_Beli;
$Qty_Beli = $model2->Qty_Beli;
$Satuan_Jual = $model2->Satuan_Jual;
$Qty_Jual = $model2->Qty_Jual;
$ActiveMOM = $model2->Active;
$UserMOM = Yii::$app->user->identity->username;
if ($command->execute() == 0) {
$transaction->commit();
} else {
$transaction->rollBack();
foreach ($model->getErrors() as $key => $message) {
Yii::$app->session->setFlash('error', $message);
}
}
return $this->redirect(['view', 'id' => $model->ID_Item]);
} else {
return $this->render('create', array(
'model' => $model,
'model2' => $model2,
'model3' => $model3,
'model4' => $model4,
'model5' => $model5,
'model6' => $model6,
'model7' => $model7,
));
}
This is Create View
create.php
<h1><?= Html::encode($this->title) ?></h1>
<?=
$this->render('_form', [
'model' => $model,
'model2' => $model2,
'model3' => $model3,
'model4' => $model4,
'model5' => $model5,
'model6' => $model6,
'model7' => $model7,
])
?>
This is the form
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'ID_Item')->textInput() ?>
<?= $form->field($model, 'Item_Name')->textInput() ?>
<?= $form->field($model, 'ID_Mom')->textInput() ?>
<?= $form->field($model, 'Item_Price')->textInput() ?>
<?= $form->field($model, 'ID_Inv_Category')->textInput() ?>
<?= $form->field($model, 'Item_PIC1')->textInput() ?>
<?= $form->field($model, 'Item_PIC2')->textInput() ?>
<?=
$form->field($model, 'Item_active')->widget(SwitchInput::classname(), [
'pluginOptions' => [
'onText' => 'Active',
'offText' => 'Not Active',
]
])
?>
<?=$form->field($model2,'ID_Mom')->textInput() ?>
Your url is: localhost:81/posrkidev/web/index.php/inventory/create
you should put all the code from
public function actionInsert() {
...
}
To action create:
public function actionCreate() {
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
$model = new Inventory();
$model2 = new \app\models\Unitofmeasurement();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$command = $connection->createCommand('{call usp_M_Inventory#Transaksi(:ID_Item,:Item_Name, :IDMom, :Item_Price, :ID_InvCategory,:Item_PIC1,
:Item_PIC2,:Item_active, :UserInventory, :ID_Mom, :Satuan_Beli, :Qty_Beli, :Satuan_Jual,:Qty_Jual, :ActiveMOM, :UserMOM)}');
$ID_Item = $model->ID_Item;
$Item_Name = $model->Item_Name;
$IDMom = $model->IDMom;
$Item_Price = $model->Item_Price;
$ID_InvCategory = $model->ID_Inv_Category;
$Item_PIC1 = $model->Item_PIC1;
$Item_PIC2 = $model->Item_PIC2;
$Item_active = $model->Item_active;
$UserInventory = Yii::$app->user->identity->username;
$ID_Mom = $model2->ID_Mom;
$Satuan_Beli = $model2->Satuan_Beli;
$Qty_Beli = $model2->Qty_Beli;
$Satuan_Jual = $model2->Satuan_Jual;
$Qty_Jual = $model2->Qty_Jual;
$ActiveMOM = $model2->Active;
$UserMOM = Yii::$app->user->identity->username;
if ($command->execute() == 0) {
$transaction->commit();
} else {
$transaction->rollBack();
foreach ($model->getErrors() as $key => $message) {
Yii::$app->session->setFlash('error', $message);
}
}
return $this->redirect(['view', 'id' => $model->ID_Item]);
} else {
return $this->render('create', array(
'model' => $model,
'model2' => $model2,
'model3' => $model3,
'model4' => $model4,
'model5' => $model5,
'model6' => $model6,
'model7' => $model7,
));
}
or try this url: localhost:81/posrkidev/web/index.php/inventory/insert