0

为了测试目的,我在验证控制器内的表单字段时遇到了一些问题。

我有我的模型ExperiencehasMany ExperienceDetailExperienceDetail属于To Experience

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

索引.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,但这些与此无关。

经验.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留空时,它不会为我提供任何属于输入字段的错误。

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

4

0 回答 0