我需要用不同的数据保存模型两次:
控制器:
$modelClient = new Client;
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validateTabular( array( $modelClient));
Yii::app()->end();
}
查看(仅相关部分)
<?php echo $form->textFieldRow($modelClient, '[0]name'); ?>
<?php echo $form->textFieldRow($modelClient, '[0]street'); ?>
<?php echo $form->textFieldRow($modelClient, '[1]name'); ?>
<?php echo $form->textFieldRow($modelClient, '[1]street'); ?>
JSON 输出
{"Client_0_name":["field is empty"],"Client_0_street":["field is empty"]}
所以第二个模型被忽略了。
我试过
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
foreach ($_POST[Client] as $client) {
$temp = new Client;
$temp->setAttributes($client);
echo CActiveForm::validate( array( $modelClient));
}
Yii::app()->end();
}
但它返回没有正确 id 的 JSON 输出,例如:
{"Client_name":["field is empty"],"Client_street":["field is empty"]}
结果它只是不验证任何字段。