0
votes

Fans,

i try to save multiple entries in the database.

Example:

index.ctp

echo $this->Form->create('Item');
echo $this->Form->input('0.Item.name');
echo $this->Form->input('1.Item.name');
echo $this->Form->end('save');

Item.php

class Item extends AppModel {
  var $validate = array(
    'name' => 'email'
  );
}

ItemsController.php

public function index() {
  if($this->request->is('post')){
    if($this->Item->saveAll($this->request->data))
        $this->Session->setFlash("saved successful");
      else{
        $this->Session->setFlash("saving error");
        debug($this->Item->validationErrors);
      }
  }
}

When i try to save a non-email value i get the "saving error" but no message for the input fields. The validationErrors are:

array(
    (int) 0 => array(
        'name' => array(
            (int) 0 => 'This field cannot be left blank'
        )
    ),
    (int) 1 => array(
        'name' => array(
            (int) 0 => 'This field cannot be left blank'
        )
    )
)

Anyone can help me?

Edit:

debugging $this->request->data

array(

    (int) 0 => array(
        'Item' => array(
            'name' => 'test'
        )
    ),
    (int) 1 => array(
        'Item' => array(
            'name' => 'test2'
        )
    )
)
1
show what is inside $this->request->data by debugging it - Moyed Ansari
see edit above @MoyedAnsari - fug4zi

1 Answers

1
votes

You may try to reformat your posted data and use Model::saveMany() in stead (see the documentation and Field naming Conventions);

echo $this->Form->create('Item');
echo $this->Form->input('Item.0.name');
echo $this->Form->input('Item.1.name');
echo $this->Form->end('save');

And, inside the controller;

$this->Item->saveMany($this->request->data['Item']);

Although, your existing Controller code should probably work as well using the modified naming of your inputs

Untested (not behind my computer), but worth a try :)