3

我必须使用“CakePHP”以单一形式验证多个模型。我有2个模型。

发票

public $validate = array(
        'type' => array(
            'rule' =>'notEmpty',
            'message' => 'Select Invoice Type.',
            'required' => true
        ),
        'number' => array(
            'rule' =>'numeric',
            'message' => 'Enter Invoice Number.',
            'required' => true
        ),
        'date' => array(
            'rule' => array('date', 'dmy'),
            'allowEmpty' => true,
            'message' => 'Enter Invoice Date.'
        ),
    );

    public $belongsTo = array(
        'Client' => array(
            'className' => 'Client',
            'foreignKey' => 'client_id'
        ));

客户

public $validate = array(
        'name' => array(
            'rule' =>'notEmpty',
            'message' => 'Enter Your Name.',
            'required' => true
        ),
        'company' => array(
            'rule' =>'notEmpty',
            'message' => 'Enter Your Company Name.',
            'required' => true
        ),
        'address' => array(
            'rule' =>'notEmpty',
            'message' => 'Enter Your address.',
            'required' => true
        )
    );

    public $hasMany = 'Invoice';

我有一个表单,其中包含“ clientName ”、“ Address ”、“ InvoiceNumber ”和“ InvoiceDate ”等字段。我使用了 saveAll(),但它只是验证Invoice数据而不是Client数据。

4

2 回答 2

0

看看以下答案:https ://stackoverflow.com/a/4673403/1110760

hasMany 模型的字段应为数组(与父模型组合时),请参阅在字段名称之间添加 .0

如果您还想在仍然卡住时发布您的表格,我们可以为您提供帮助。

于 2013-01-28T13:13:00.027 回答
0

取自这里http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html

if ($this->ModelName->saveAll(
    $this->request->data, array('validate' => 'only')
)) {
  // validates
} else {
  // does not validate
}
于 2014-06-01T08:42:07.227 回答