tags:

views:

34

answers:

1

Hi, so I have a Tarif, which hasMany Price (each for different currency - doesn't really matter)

the view (deleted irrelevant parts)

<?php echo $this->Form->create('Tarif');
  echo $this->Form->input("Price.0.price");
  echo $this->Form->input("Price.0.currency");
  echo $this->Form->input("Price.0.sms_format");
  echo $this->Form->input("Price.0.sms_number");
  echo $html->link(__('Add currency', true), '#', array('onclick'=>'return false;', 'class' => 'tarifs-add-currency'));

  echo $this->Form->input('Tarif.valid_since', array('timeFormat' => '24'));
  echo $this->Form->input('Tarif.valid_until', array('timeFormat' => '24'));
  echo $this->Form->input('Tarif.storage_time', array('label' => __('Storage time (days)', true)));
echo $this->Form->end(__('Submit', true));?>

The controller function to save it looks like this

function admin_add() {
  if (!empty($this->data)) {
    $this->Tarif->create();
    if ($this->Tarif->saveAll($this->data)) {
      $this->Session->setFlash(__('The tarif has been saved', true));
      $this->redirect(array('action' => 'admin_index'));
    } else {
      $this->Session->setFlash(__('The tarif could not be saved. Please, try again.', true));
    }
  }
}

The "add currency" link is there to add new inputs for new prices, but that isn't the problem, because it doesn't work even without adding currencies. When I try to save it, it says 'The tarif could not be saved. Please, try again.'. Don't you know what should I change in order to make it work?

Thanks EL

A: 

For me this look like you have validation problem - check if in your models you have some validation rules on fields which are not in the form. For example if you have a field user_id in the table and if you build your model with the bake console it's possible this field to be created as numeric.

This way even it's not in the form it's validated and the form return false. Try to print $this->YourModel->validationErrors and see if there is something there.

Another hint - have you use by any chance beforeSave() and beforeValidate() in your model? If so, they need to return bool true otherwise the save function can return false as well and it wont save anything.

Nik
Thanks for your answer Nik,validationError shows that tarif_id for the price isn't valid... I thought cake will take care of it in the saveAll() method, so is there an cake-way to set the tarif_id for all prices or do I just have to iterate over all prices and set it manually?
ELwhis
Removed tarif_id validation rule from Price model - everything working fine now. Thanks a lot ;)
ELwhis
Basically when you have ParentModel-<ChildModel the SaveAll adds the ParentModel_id value automatically ;)
Nik