Checking multiple models in one form

I need to validate multiple models in one form using 'CakePHP'. I have 2 models.

Score

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'
        ));

      

Client

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';

      

And I have one form with fields like "clientName", "Address", "InvoiceNumber" and "InvoiceDate". I used saveAll (), but it only validates data Invoice

, not data Client

.

+3


source to share


2 answers


Have a look at the following answer: fooobar.com/questions/341755 / ...

hasMany model fields must be like an array (combined with the parent model), see..0, appended between the field names



If you want to post your form when you are still stuck, we can help you.

0


source


taken from here 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
}

      

0


source







All Articles