Following the instructions from http://www.yiiframework.com/wiki/666/handling-tabular-data-loading-and-validation-in-yii-2/, I have done the following:
From my controller, I passed in an array $accessibleDepts that contains an array of models of class DeptEmployee.
In my view, I add these models to my active form like so:
foreach($accessibleDepts as $i=>$accessibleDept){
echo '<br>';
echo '<h5>' . $accessibleDept->deptGroupName . '</h5>';
echo $form->errorSummary($accessibleDept);
echo $form->field($accessibleDept, '[$i]ACTIVE_FLAG')->checkbox(['value'=>-1]);
echo $form->field($accessibleDept, '[$i]DEPT_START')->textInput(['readonly'=>false, 'class' => 'datepicker']);
echo $form->field($accessibleDept, '[$i]COMMENTS')->textarea(['readonly'=>false, 'style' => 'width:500px; height:150px']);
}
If I pass in two models in $accessibleDepts, I see both models displayed on my form the way I expect. However, when I submit the form as a post request, I see that the post object I am sending for the DeptEmployee class models is only including the second model.
It looks like
[
'$i' => [
'ACTIVE_FLAG' => '-1',
'DEPT_START' => '23-JUN-2017',
'COMMENTS' => 'TEST COMMENT',
]
]
I'm not sure why $i is not being converted to an index here, it seems like only the last model of class DeptEmployee is being kept and sent back to the controller.
The $accessibleDepts array is created in my controller like so
$deptEmployeeQuery = DeptEmployee::find()->where(['EMP_ID'=>$emp_id]);
if($deptEmployeeQuery->count() > 0){
$deptEmployeeArray = $deptEmployeeQuery->all();
}
foreach($deptEmployeeArray as $deptRecord){
if ($this->authModel->hasAccessToDeptGroup($deptRecord['DEPT_GROUP_ID']))
$accessibleDepts[] = $deptRecord;
}
if(DeptEmployee::loadMultiple($accessibleDepts, Yii::$app->request->post())){
return $this->render('success');
}
Does anyone know what the issue is here?
You should change your view
as below:
foreach($accessibleDepts as $i=>$accessibleDept){
echo '<br>';
echo '<h5>' . $accessibleDept->deptGroupName . '</h5>';
echo $form->errorSummary($accessibleDept);
echo $form->field($accessibleDept, "[$i]ACTIVE_FLAG")->checkbox(['value'=>-1]);
echo $form->field($accessibleDept, "[$i]DEPT_START")->textInput(['readonly'=>false, 'class' => 'datepicker']);
echo $form->field($accessibleDept, "[$i]COMMENTS")->textarea(['readonly'=>false, 'style' => 'width:500px; height:150px']);
}
Single quotation marks will depress variables to be interpreted. For more detail, you may go What is the difference between single-quoted and double-quoted strings in PHP?