Wbraganca DynamicFormWidget Blank Line Upon Update

I’m using wbraganca DynamicFormWidget in a nested scenario. So I have a main model (Company) with a Dynamic Form (Drivers) and then 2 Dynamic Forms within the Drivers (Service & Certifications).

In a general sense, it is functional. That being said, I have one issue I can’t seem to figure out. When updating a record in which a Driver didn’t have any Services or Certifications assigned, a blank entry line is generated. This then causes and issue if you try to save the data as they are require fields and are all empty.

Below is my controller (I’m assuming the issue is there).

Thank you for any assistance you can provide.

public function actionUpdate($id){
// retrieve existing Deposit data
$model = $this->findModel($id);

// retrieve existing Driver data
$oldDriverIds = EmployeesDrivers::find()->select('DriverId')
    ->where(['EmpId' => $id])->asArray()->all();
$oldDriverIds = ArrayHelper::getColumn($oldDriverIds, 'DriverId');
$modelsDrivers = EmployeesDrivers::findAll(['DriverId' => $oldDriverIds]);
$modelsDrivers = (empty($modelsDrivers)) ? [new EmployeesDrivers] : $modelsDrivers;

// retrieve existing Driver Services data
$oldServiceIds = [];
foreach ($modelsDrivers as $i => $modelDriver) {
    $oldServices = EmployeesDriversServices::findAll(['DriverId' => $modelDriver->DriverId]);
    $modelsDriversServices[$i] = $oldServices;
    $oldServiceIds = array_merge($oldServiceIds, ArrayHelper::getColumn($oldServices, 'DriverServiceId'));
    $modelsDriversServices[$i] = (empty($modelsDriversServices[$i])) ? [new EmployeesDriversServices] : $modelsDriversServices[$i];
}

// retrieve existing Driver Certifications data
$oldCertificationIds = [];
foreach ($modelsDrivers as $i => $modelDriver) {
    $oldCertifications = EmployeesDriversCertifications::findAll(['DriverId' => $modelDriver->DriverId]);
    $modelsDriversCertifications[$i] = $oldCertifications;
    $oldCertificationIds = array_merge($oldCertificationIds, ArrayHelper::getColumn($oldCertifications, 'CertId'));
    $modelsDriversCertifications[$i] = (empty($modelsDriversCertifications[$i])) ? [new EmployeesDriversCertifications] : $modelsDriversCertifications[$i];
}

// handle POST
if ($model->load(Yii::$app->request->post())) {
    // get Drivers data from POST
    $modelsDrivers = Model::createMultipleDrivers(EmployeesDrivers::classname(), $modelsDrivers);
    Model::loadMultiple($modelsDrivers, Yii::$app->request->post());
    $newDriverIds = ArrayHelper::getColumn($modelsDrivers, 'DriverId');

    // get Drivers Services data from POST
    $newServiceIds = [];
    $servicesData['_csrf-backend'] =  Yii::$app->request->post()['_csrf-backend'];
    for ($i=0; $i < count($modelsDrivers); $i++) {
        if(isset(Yii::$app->request->post()['EmployeesDriversServices'][$i])){
            $servicesData['EmployeesDriversServices'] = Yii::$app->request->post()['EmployeesDriversServices'][$i];
            $modelsDriversServices[$i] = Model::createMultipleDriversServices(EmployeesDriversServices::classname(), $modelsDriversServices[$i], $servicesData);
            Model::loadMultiple($modelsDriversServices[$i], $servicesData);
            $newServiceIds = array_merge($newServiceIds, ArrayHelper::getColumn($servicesData['EmployeesDriversServices'], 'DriverServiceId'));
        }
    }

    // get Drivers Certifications data from POST
    $newCertificationIds = [];
    $certificationssData['_csrf-backend'] =  Yii::$app->request->post()['_csrf-backend'];
    for ($i=0; $i < count($modelsDrivers); $i++) {
        if(isset(Yii::$app->request->post()['EmployeesDriversCertifications'][$i])){
            $certificationssData['EmployeesDriversCertifications'] = Yii::$app->request->post()['EmployeesDriversCertifications'][$i];
            $modelsDriversCertifications[$i] = Model::createMultipleDriversCertifications(EmployeesDriversCertifications::classname(), $modelsDriversCertifications[$i], $certificationssData);
            Model::loadMultiple($modelsDriversCertifications[$i], $certificationssData);
            $newCertificationIds = array_merge($newCertificationIds, ArrayHelper::getColumn($certificationssData['EmployeesDriversCertifications'], 'CertId'));
        }
    }

    // delete removed data
    $delCertificationIds = array_diff($oldCertificationIds, $newCertificationIds);
    if (! empty($delCertificationIds)) EmployeesDriversCertifications::deleteAll(['CertId' => $delCertificationIds]);
    $delServiceIds = array_diff($oldServiceIds, $newServiceIds);
    if (! empty($delServiceIds)) EmployeesDriversServices::deleteAll(['DriverServiceId' => $delServiceIds]);
    $delDriverIds = array_diff($oldDriverIds, $newDriverIds);
    if (! empty($delDriverIds)) EmployeesDrivers::deleteAll(['DriverId' => $delDriverIds]);

    // validate all models
    $valid = $model->validate();
    // $valid = EmployeesDrivers::validateEmployee($modelsDrivers, $modelsDriversServices) && $valid;

    // save deposit data
    if ($valid) {
        if ($this->saveEmployee($model ,$modelsDrivers, $modelsDriversServices, $modelsDriversCertifications)) {
            // Yii::$app->getSession()->setFlash('success',
            //     Yii::t('payments','The deposit number {id} has been saved.', ['id' => $model->id]));
            // return $this->redirect('deposit-index-all');
            echo json_encode(['status' => 'Success', 'message' => 'Employee updated successfully.']);
        }else{
            echo json_encode(['status' => 'Error', 'message' => 'Something went wrong!']);
        }
    }
} else {
    return $this->renderAjax('update', [
        'model' => $model,
        'modelsDrivers' => (empty($modelsDrivers)) ? [new EmployeesDrivers] : $modelsDrivers,
        'modelsDriversServices' => (empty($modelsDriversServices)) ? [new EmployeesDriversServices] : $modelsDriversServices,
        'modelsDriversCertifications' => (empty($modelsDriversCertifications)) ? [new EmployeesDriversCertifications] : $modelsDriversCertifications,
    ]);
}

}