I am using Yii 2 and dynamic form widget in my application.
I have created a form where user selects the time (From time and To time) and fills the student details for each time using nested dynamic form.
In the table I have field as Time, but in the model I have used two variables FromTime and ToTime in such a way that while saving the record to the database I joined these two variables and then assign them to Time variables.
Everything is working correctly, but in the update part the time which is coming from the table I use explode function so that I can get From Time and To Time and assign them to the widget appropriately.
But From Time is getting displayed correctly but in the To Time 12:00 AM is displayed. To Time is not getting fetched properly.
I am using yii2-widget-timepicker extension
Below is the code,
<?php foreach ($modelsTime as $indexTime => $modelTime): ?>
<tr class="time-item">
<td class="vcenter" style='border: 1px solid black;'>
<?php
// necessary for update action.
if (! $modelTime->isNewRecord) {
echo Html::activeHiddenInput($modelTime, "[{$indexTime}]ASCReportDetailsId");
}
?>
</td>
<td style='border: 1px solid black;width:15%'>
<?php
if (! $modelTime->isNewRecord)
{
$time1 = explode('To',$modelTime->Time);
$fromtime=$time1[0];
$totime = $time1[1];
$modelTime->FromTime = $fromtime;
$modelTime->ToTime = $totime;
echo $form->field($modelTime, "[{$indexTime}]FromTime")->label(true)->widget(TimePicker::classname(),[
'pluginOptions' => [
'readOnly' => true,
'minuteStep' => 1,
],
]);
echo "<br>";
echo $form->field($modelTime, "[{$indexTime}]ToTime")->label(true)->widget(TimePicker::classname(),[
'pluginOptions' => [
'readOnly' => true,
'minuteStep' => 1,
],
]);
}
else
{
echo $form->field($modelTime, "[{$indexTime}]FromTime")->label(true)->widget(TimePicker::classname(),[
'pluginOptions' => [
'readOnly' => true,
'minuteStep' => 1,
],
]);
echo "<br>";
echo $form->field($modelTime, "[{$indexTime}]ToTime")->label(true)->widget(TimePicker::classname(),[
'pluginOptions' => [
'readOnly' => true,
'minuteStep' => 1,
],
]);
}?>
</td>
<td style='border: 1px solid black;'>
<?= $this->render('_form-studentdata', [
'form' => $form,
'indexTime' => $indexTime,
'modelsStudentdata' => $modelsStudentdata[$indexTime],
]) ?>
</td>
<td class="text-center vcenter" style='border: 1px solid black;'>
<button type="button" class="remove-time btn btn-danger btn-xs"><span class="fa fa-minus"></span></button>
</td>
</tr>
<?php endforeach; ?>