Witam mam takie pytanko odnośnie walidacji 2 modelu podczas użycia saveAll() =>
class Voucher extends AdminAppModel {
public $name = 'Voucher';
public $useTable = 'voucher';
public $primaryKey = 'id';
'Admin.VoucherImage'=> array('className'=> 'Admin.VoucherImage', 'foreignKey'=> 'voucher_image_id') );
public $validate = array( 'title'=> array('rule1'=> array('rule'=> 'notEmpty', 'message'=> 'Pole nie może być puste')), 'content'=> array('rule1'=> array('rule'=> 'notEmpty', 'message'=> 'Pole nie może być puste')), 'content_promotion'=> array('rule1'=> array('rule'=> 'notEmpty', 'message'=> 'Pole nie może być puste')), 'content_why'=> array('rule1'=> array('rule'=> 'notEmpty', 'message'=> 'Pole nie może być puste')), 'rule1'=> array('rule'=> 'numeric', 'message'=> 'Pole musi być cyfrą'), 'rule2'=> array('rule'=> 'notEmpty', 'message'=> 'Pole nie może być puste'), )
}
class VoucherImage extends AdminAppModel {
public $name = 'VoucherImage';
public $useTable = 'voucher_image';
public $primaryKey = 'id';
public $validate = array( 'name'=> array('rule'=> 'notEmpty', 'message'=> 'Pole nie może być puste !') );
}
Widok z formą:
<div class="backBox">
<div class="top"></div>
<div class="middle">
<?php
if(isset($id)) echo $this->Form->create('Voucher', array('url'=> array('controller'=> 'voucher', 'action'=> 'form', $id), 'type'=> 'file')); else echo $this->Form->create('Voucher', array('url'=> array('controller'=> 'voucher', 'action'=> 'form'), 'type'=> 'file')); echo $this->Form->input('Voucher.title', array('label'=> 'Tytuł kuponu')); echo $this->Tinymce->input('Voucher.content', null, null, "long", 'Opis'); echo $this->Tinymce->input('Voucher.content_promote', null, null, "long", 'Jak korzystać z promocji'); echo $this->Tinymce->input('Voucher.content_why', null, null, "long", 'Dlaczego warto skorzystać z promocji'); echo $this->Tinymce->input('Voucher.address', null, null, "long", 'Adres firmy'); echo $this->Form->input('Voucher.price_old', array('label'=> 'Cena przed rabatem')); echo $this->Form->input('Voucher.price', array('label'=> 'Cena po rabacie')); echo $this->Form->input('Voucher.promotion', array('label'=> 'Rabat procentowy')); echo $this->Form->input('Voucher.allowance', array('label'=> 'Oszczędność')); echo $this->Form->input('Voucher.date_start', array('type'=> 'date', 'label'=>'Data rozpoczęcia:','dateFormat'=>'DMY', 'monthNames'=> $monthPL)); echo $this->Form->input('Voucher.date_end', array('type'=> 'date', 'label'=>'Data zakończenia:','dateFormat'=>'DMY', 'monthNames'=> $monthPL)); echo $this->Form->input('Voucher.buy_min', array('label'=> 'Minimalna ilość kuponów')); echo $this->Form->input('Voucher.buy_max', array('label'=> 'Maksymalna ilość kuponów')); echo $this->Form->input('Voucher.voucher_city_id', array('label'=> 'Miasto', 'type'=> 'select','options'=> $city)); echo $this->Form->input('Voucher.lat', array('label'=> 'Google Map lat:')); echo $this->Form->input('Voucher.lng', array('label'=> 'Google Map lng:')); for($i=0;$i<4;$i++) echo $this->Form->input('VoucherImage.'.$i.'.name', array('type'=> 'text', 'label'=> 'test')); ?>
</div>
<div class="bottom"></div>
</div>
<?=$form->end('Zapisz')?>
Ok czyli mam w formie 2 modele Voucher i VoucherImage, VaoucherImage posiada pole name (wiem że bezsens ale to tak dla testów), które według zasad walidacji nie może być puste. Jednak gdy w kontrolerze daje taki kod:
public function form($id = null) {
if($this->data) {
if($id) $this->data['Voucher']['id'] = $id;
if($this->Voucher->saveAll($this->data, array('validate'=> 'first'))) { $this->Session->setFlash('Zapisano miasto');
echo 'poszedl savaall'; $this->log($this->Voucher->data); //$this->redirect(array('action'=> 'index'));
}
else {echo 'błąd:'; $this->log($this->Voucher->validateErrors);} }
elseif($id) $this->data = $this->Voucher->findById($id);
$this->set('city', $this->VoucherCity->find('list'));
}
Jeśli chodzi o walidacje dla pól modelu Voucher to jest ok wszystko działa, natomiast walidacja dla modelu VoucherImage nie działa, tzn nie zależnie czy pola są puste czy zapisane (a nie mogą być puste) saveAll zwraca true i się wykonuje zapis do bazy. Moje pytanie jak w prosty sposób (Cake way) sprawdzić żeby ta walidacja dla tych czterech pól w 2 modelu zaczęła działać ?