In Yii2, I have been trying to create a dynamic form using the below tutorials.
Yii2-dynamicForm – GitHub and Youtube Tutorial.
I followed the same step as mentioned in tutorials, unfortunately I am held up with an error(screenshot attached)
And I am not able to resolve this error, can any one help me to find what I am missing.
For the reference I am attaching my Models here, rest the controllers and views are same as the tutorials.
Request Model
<?php namespace appmodels; use Yii; class Request extends yiidbActiveRecord { public static function tableName() { return 'request'; } public function rules() { return [ [['req_date', 'req_on', 'req_updated_on'], 'safe'], [['req_job', 'req_type', 'material_type', 'req_status'], 'required'], [['req_by'], 'integer'], [['req_status'], 'string'], [['req_job', 'req_type'], 'string', 'max' => 100], [['material_type'], 'string', 'max' => 150], ]; } public function attributeLabels() { return [ 'req_id' => 'Req ID', 'req_date' => 'Req Date', 'req_job' => 'Job No', 'req_type' => 'Type of Request', 'req_on' => 'Required On', 'material_type' => 'Material Type', 'req_by' => 'Req By', 'req_updated_on' => 'Req Updated On', 'req_status' => 'Request Status', ]; } }
RequestItems Model
<?php namespace appmodels; use Yii; use yiibaseModel; /** * This is the model class for table "request_items". * * @property int $req_item_id * @property int $req_id * @property string $item_name * @property string $item_qty * @property string $item_unit */ class RequestItems extends yiidbActiveRecord { /** * {@inheritdoc} */ public static function tableName() { return 'request_items'; } /** * {@inheritdoc} */ public function rules() { return [ [['req_id', 'item_name', 'item_qty', 'item_unit'], 'required'], [['req_id'], 'integer'], [['item_name'], 'string', 'max' => 100], [['item_qty', 'item_unit'], 'string', 'max' => 25], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'req_item_id' => 'Req Item ID', 'req_id' => 'Req ID', 'item_name' => 'Item Name', 'item_qty' => 'Item Qty', 'item_unit' => 'Item Unit', ]; } }
_form.php
<?php use yiihelpersHtml; use yiiwidgetsActiveForm; use wbragancadynamicformDynamicFormWidget; use kartikselect2Select2; use kartikcheckboxCheckboxX; /* @var $this yiiwebView */ /* @var $model appmodelsRequest */ /* @var $form yiiwidgetsActiveForm */ ?> <div class="request-form"> <?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?> <?= $form->field($model, 'req_date')->hiddenInput()->label(false) ?> <div class="row"> <div class="col-sm-6"> <?php $data = ['1001' => '1001/Sample Job 1', '1002' => '1002/Sample Job 2', '1003' => '1003/Sample Job 3']; echo $form->field($model, 'req_job')->widget(Select2::classname(), [ 'data' => $data, 'class'=>'form-control', 'options' => ['placeholder' => 'Select a state ...'], 'pluginOptions' => [ 'allowClear' => true ], ]); ?> </div> <div class="col-sm-6"> <?= $form->field($model, 'req_type')->radioList(array('Urgent'=>'Urgent','Normal'=>'Normal')); ?> </div> </div> <div class="row"> <div class="col-sm-6"> <?= $form->field($model, 'req_on')->input('date') ?> </div> <div class="col-sm-6"> <?= $form->field($model, 'material_type')->radioList(array('Civil'=>'Civil','Mechanical'=>'Mechanical', 'Manpower'=>'Manpower')); ?> </div> </div> <div class="row"> <div class="col-sm-6"> <?= $form->field($model, 'req_by')->hiddenInput()->label(false) ?> </div> <div class="col-sm-6"> <?= $form->field($model, 'req_updated_on')->hiddenInput()->label(false) ?> </div> </div> <!-- code for dynamic form --> <div class="panel panel-default"> <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Request 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' => 4, // 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' => $modelsAddress, 'formId' => 'dynamic-form', 'formFields' => [ 'item_name', 'item_qty', 'item_unit', ], ]); ?> <div class="container-items"><!-- widgetContainer --> <?php foreach ($modelsAddress as $i => $modelAddress): ?> <div class="item panel panel-default"><!-- widgetBody --> <div class="panel-heading"> <h3 class="panel-title pull-left">Requested Items</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 (! $modelAddress->isNewRecord) { echo Html::activeHiddenInput($modelAddress, "[{$i}]id"); } ?> <?= $form->field($modelAddress, "[{$i}]item_name")->textInput(['maxlength' => true]) ?> <div class="row"> <div class="col-sm-6"> <?= $form->field($modelAddress, "[{$i}]item_qty")->textInput(['maxlength' => true]) ?> </div> <div class="col-sm-6"> <?= $form->field($modelAddress, "[{$i}]item_unit")->textInput(['maxlength' => true]) ?> </div> </div><!-- .row --> </div> </div> <?php endforeach; ?> </div> <?php DynamicFormWidget::end(); ?> </div> </div> <?= $form->field($requestModel, 'req_status')->dropDownList(['Requested' => 'Requested', 'Approved' => 'Approved', 'Rejected' => 'Rejected', 'Hold' => 'Hold',], ['prompt' => '']) ?> <div class="form-group"> <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?> </div> <?php ActiveForm::end(); ?> </div>
Advertisement
Answer
You need to change the
'model'=>$modelsAddress
to
'model'=>$modelsAddress[0]
As stated in the basic example, its an array of models and the way you passing, it will be detected as array rather than a model.