不确定我是否正确理解了您的问题,但是如果您想以相同的形式验证和保存两个模型,您可以这样做。
控制器:
$teacherModel = new Teacher;
$studentModel = new Student;
if(isset($_POST['Teacher']) && isset($_POST['Student']))
{
$teacherModel->attributes = $_POST['Teacher'];
$studentModel->attributes = $_POST['Student'];
// check both models for validation errors before saving and redirecting.
$teacherValid = $teacherModel->validate();
$studentValid = $studentModel->validate();
if($teacherValid && $studentValid)
{
$teacherModel->save(false); // false = don't perform validation before saving
$studentModel->save(false);
$this->redirect(array('view','id'=>$studentModel->id));
}
}
$this->render('create',array(
'teacherModel'=>$teacherModel,
'studentModel'=>$studentModel,
));
这将确保两个模型在重定向之前都是有效的,否则它将返回到表单。
然后,您可以在视图中显示来自两个模型的验证错误,方法是将它们以数组的形式传递给方法 $form->errorSummary:
<?php echo $form->errorSummary(array($teacherModel, $studentModel)); ?>