【问题标题】:Validate multiple models in single Form在单个表单中验证多个模型
【发布时间】:2013-01-11 19:43:28
【问题描述】:

我必须使用“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 数据。

【问题讨论】:

  • 请告诉我们您是如何创建表单的。

标签: cakephp


【解决方案1】:

取自这里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
}

【讨论】:

    【解决方案2】:

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

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

    如果您还想在遇到困难时也发布您的表单,我们可以为您提供帮助。

    【讨论】:

      猜你喜欢
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      相关资源
      最近更新 更多