3

I have to validate multiple model in single form using 'CakePHP'. I have 2 models.

Invoice

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 have used saveAll(), but it is only validating Invoice data and not the Client data.

Jelmer
  • 2,663
  • 2
  • 27
  • 45
Archna Rangrej
  • 664
  • 3
  • 14
  • 38

2 Answers2

0

Have a look at the following answer: https://stackoverflow.com/a/4673403/1110760

hasMany model's fields should be as array (when combined with parent model), see .0 added between field names

If you would like to post your Form as well when you are still stuck, we can help you.

Community
  • 1
  • 1
Jelmer
  • 2,663
  • 2
  • 27
  • 45
0

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
}
cedric
  • 11
  • 1