使用hasMany关系验证控制器中的表单字段

时间:2013-03-02 19:04:43

标签: validation cakephp

我在控制器内验证表单字段时遇到了一些问题,以便进行测试。

我的模型Experience包含hasMany ExperienceDetailExperienceDetail属于Experience

我使用FormHelper创建了一个表单,其中包含以下内容:

index.ctp

<?php
echo $this->Form->create('Experience', array('action' => 'index'));
echo $this->Form->input('Experience.date', array(
        'label' => array(
            'text' => 'Datum'),
        'type' => 'date',
        'dateFormat' => 'DMY',
        'monthNames' => false,
        'minYear' => date('Y') - 10,
        'maxYear' => date('Y')
        )
    );      
echo $this->Form->input('Experience.test');
echo $this->Form->input('ExperienceDetail.vertrekstation');
echo $this->Form->end('Verstuur!');
?>

ExperienceDetail提供了更多字段,但这些字段与此无关。

Experience.php

<?php 
class Experience extends AppModel {
    public $name = 'Experience';

    public $hasMany = 'ExperienceDetail';

    public $validate = array(
        'vertrekstation' => array(
            'rule' => 'notEmpty',
            'message' => 'Voer een vertrekstation in',
            'required' => true
        ),
        'test' => array(
            'rule' => 'notEmpty',
            'message' => 'Test mag niet leeg zijn!'
        )
    );
}
?>

ExperienceDetail.php

<?php
class ExperienceDetail extends AppModel {
    public $name = 'ExperienceDetail';

    public $belongsTo = 'Experience';

    public $validate = array(
        'vertrekstation' => array(
            'rule' => 'notEmpty',
            'message' => 'Voer een vertrekstation in'
        )
    );
}
?>

ExperiencesController.php

<?php 
class ExperiencesController extends AppController { 
public $helpers = array('Html', 'Form', 'Session');

public function index() {
    // $this->layout = 'default_orig'; 
    $this->set('title_for_layout', 'De OV-Ervaringenmeter!');

    // LOAD Model Carrier 
    $this->loadModel('Carrier');
    $this->set('carrier', $this->Carrier->find('list', array('order' => array('Carrier.name' => 'asc'))));

    // Check if form is allready filled
    if($this->request->is('post')) {

        $this->Experience->set($this->request->data);
        $this->Session->setFlash('No if or else statement is called');

        if ($this->Experience->validates()) {
            $this->Session->setFlash('Validates!');
        }
    }       

}
}
?>

问题是:当我发送表单并将test留空时,它会向我提供我在模型中设置的验证错误。但是当我将vertrekstation留空时,它不会向我提供任何属于输入字段的错误。

我做错了什么以及如何才能打印出这些错误?

0 个答案:

没有答案