The related table has a tabular input form to collect multiple instances.
The problem I'm having is this:
If I omit any required fields from my primary table, I get an error message - this is good.
If I omit any required fields from the dependent table, it saves the primary and redirects. Not good.
I've read the wiki and forum about multimodel forms but this doesn't really apply to my situation. The problem is that I need to make sure that both the primary and related table have $_POST data and if not, show the required error.
How do you do multiple model forms with tabular input?
I've tried to change this code to use the $valid=$model->validate() && $valid workflow suggested in the wiki - but the problem is that I don't know how to make this work in a 1:Many situation.
My code:
if(isset($_POST['Inspection'])) {
//Manually assign model attributes since not coming from user form. Skipping massive assignment
$model->attributes=$_POST['Inspection'];
$model->userId=Yii::App()->user->id;
$model->assignmentId=$assignment->id;
$model->status=1;
if($model->save()) {
if(isset($_POST['InspectionData'])) {
//Need to handle if relatedData NOT posted as only parent model will be saved and will not see validation errors.
foreach ($_POST['InspectionData'] as $i=>$insData) {
//Foreach Inspection saved, load inspectionData model
//and assign form fields
$InspectionData = new InspectionData;
$InspectionData->inspectionId = $model->id;
$InspectionData->criteriaName = $criteria[$i]; //Criteria is not coming from user input but from the query
$InspectionData->compliant = $insData['compliant'];
$InspectionData->notes = $insData['notes'];
$InspectionData->save();
}
}
//Set create flash message and send user to dashboard? page (change to Inspection History when built)
Yii::app()->user->setFlash('create', 'Inspection recorded successfully.');
$this->redirect(array('/dashboard'));
}
}

Help














