I am using wbraganca/yii2-dynamicform for dynamic form in my OrderMaster page to add multiple OrderLineItem.
As per instruction I have added all the codes to actionUpdate() and actionCreate(). actionCreate is working fine. But in case of update I am getting exception as below.
PHP Notice – yii\base\ErrorException Undefined variable: modelOrderLineItems
statk trace is as below:
yii\base\ErrorException: Undefined variable: modelOrderLineItems in E:\xampp\htdocs\payment\views\order-master_form.php:97 Stack trace: E:\xampp\htdocs\payment\views\order-master_form.php(97): yii\web\ErrorHandler->handleError() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\View.php(348): yii\web\View->unknown() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\View.php(257): yii\web\View->renderPhpFile() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\View.php(156): yii\web\View->renderFile() E:\xampp\htdocs\payment\views\order-master\update.php(18): yii\web\View->render() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\View.php(348): yii\web\View->unknown() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\View.php(257): yii\web\View->renderPhpFile() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\View.php(156): yii\web\View->renderFile() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\Controller.php(386): yii\web\View->render() E:\xampp\htdocs\payment\controllers\OrderMasterController.php(210): app\controllers\OrderMasterController->render() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\InlineAction.php(57): app\controllers\OrderMasterController->actionUpdate() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\InlineAction.php(57): ::call_user_func_array:{E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\InlineAction.php:57}() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\Module.php(528): app\controllers\OrderMasterController->runAction() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\web\Application.php(103): yii\web\Application->runAction() E:\xampp\htdocs\payment\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest() E:\xampp\htdocs\payment\web\index.php(12): yii\web\Application->run() {main}
my _form file:
<div class="row">
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i>PO Line Items</h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 30, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelOrderLineItems[0],
'formId' => 'dynamic-form',
'formFields' => [
'item_detail',
'unit_rate',
'units',
'total_price',
'occurance',
],
]); ?>
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelOrderLineItems as $i => $modelOrderLineItem): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">PO Line Item</h3>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (! $modelOrderLineItem->isNewRecord) {
echo Html::activeHiddenInput($modelOrderLineItem, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelOrderLineItem, "[{$i}]item_detail")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelOrderLineItem, "[{$i}]unit_rate")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
<div class="row">
<div class="col-sm-4">
<?= $form->field($modelOrderLineItem, "[{$i}]units")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelOrderLineItem, "[{$i}]total_price")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelOrderLineItem, "[{$i}]occurance")->dropDownList(['OTC'=>'OTC','ARC'=>'ARC','OTC+ARC'=>'OTC+ARC']) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
My Controller File:
<?php
namespace app\controllers;
use Yii;
use app\models\OrderMaster;
use app\models\OrderMasterSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use app\models\OrderLineItem;
use app\models\Model;
use yii\helpers\ArrayHelper;
//use yii\base\Model;
use yii\web\Response;
/**
* Updates an existing OrderMaster model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelOrderLineItems = $model->orderLineItems;
if ($model->load(Yii::$app->request->post())) {
// For File upload. get the instance of uploaded file
$poName = str_replace('/', '_', $model->order_number);
$model->file = UploadedFile::getInstance($model, 'file');
if(!empty($model->file)){
$model->file->saveAs('poFiles/'.$poName.'.'.$model->file->extension);
//save the path in the DB column
$model->po_file = 'poFiles/'.$poName.'.'.$model->file->extension;
}
//For Dynamic Form Update for PO Line Items
$oldIDs = ArrayHelper::map($modelOrderLineItems, 'id', 'id');
$modelOrderLineItems = Model::createMultiple(OrderLineItem::classname(), $modelOrderLineItems);
OrderLineItem::loadMultiple($modelOrderLineItems, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelOrderLineItems, 'id', 'id')));
// // ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelOrderLineItems),
ActiveForm::validate($model)
);
}
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelOrderLineItems) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) {
OrderLineItem::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelOrderLineItems as $modelOrderLineItem) {
$modelOrderLineItem->order_id = $model->id;
if (! ($flag = $modelOrderLineItem->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
// $model->save();
// return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
'modelOrderLineItems'=> (empty($modelOrderLineItems)) ? [new OrderLineItem] : $modelOrderLineItems
]);
}
Please help me I am not able to solve this problem.
I found my mistake. I missed to pass the variable $modelOrderLineItems to the _form.php from the update.php.
This solved the problem.