0
我正在使用的应用程序(订单)允许用户在iframe中输入多个子记录。这些子记录通过外键连接到主记录。CakePHP:在提交父表单前检查iframe中的订单项
main_records line_items
----------- ----------
id int(11) PK etc. id int(11) PK etc.
main_record_id (FK)
我需要应用程序来检查表单提交之前是否至少有一个订单项存在于此iframe中。我想利用模型中的$ validate功能,但我不确定如何继续。以下是我在主要模型中试过的内容:
App::uses('AppModel', 'Model', 'LineItem');
public $hasMany = array(
'LineItem' => array(
'className' => 'LineItem',
'foreignKey' => 'main_record_id',
'dependent' => false
)
);
public $validate = array(
'main_record_id' = array(
'allowEmpty' => false,
'rule' => 'checkForLineItem',
'message' => 'You must enter at least one line item!'
)
);
//Check to make sure there is at least one line item before saving changes/submitting for approval
function checkForLineItem($id) {
$lines = $this->LineItem->find('all', array(
'fields' => array('LineItem.main_record_id'),
'conditions' => array('LineItem.main_record_id'=>$id, 'LineItem.deleted_record'=>0))
);
if(!empty($lines)) {
return true;
} else {
return false;
}
}
我还会跟踪订单项是否已被删除。如果有,那么它不会被添加到$行。
我知道我可以在控制器中完成这项工作,但据我所知,这将需要表单发布,并且用户在回发时将失去任何更改(我尚未在此表单上实现jQuery) 。我在正确的轨道上如何做到这一点?我应该做些什么才能使其发挥作用?