-1

我正在写一个联系表格,并想添加一些简单的验证例程。此页面的操作如下所示:

public function contact() {
    $this->loadModel('Contact');

    $this->set('pageTitle', 'Contact me');
}

Contact模型是这样的:

<?php

class Contact extends AppModel {
public $useTable = false;

public $validate = array(
    'name' => array(
        'between' => array(
            'rule' => array('between', 1, 60),
            'message' => 'Between 1 and 60 characters in length'
        )
    ),
    'email' => array(
        'kosher' => array(
            'rule' => 'email',
            'message' => 'Please make sure your email is entered correctly'
        ),
    ),
    'message' => array(
        'between' => array(
            'rule' => array('between', 1, 65000),
            'message' => 'Between 1 and 65000 characters in length'
        )
    )
);

}

最后是我的视图页面:

<?php echo $this->Form->create('Contact'); ?>
<?php echo $this->Form->input('name'); ?>
<?php echo $this->Form->input('email'); ?>
<?php echo $this->Form->input('message', array('type' => 'textarea')); ?>
<?php echo $this->Form->end(array('label' => 'Send', 'class' => 'btn btn-primary')); ?>

但是,当我提交带有不正确值的表单时,不会调用验证例程,也不会显示错误消息。

如何让 Cake 验证表单?

4

2 回答 2

0

查看文档以了解如何从控制器中的表单插入/更新数据。你会看到这样的东西:

if ($this->request->is('post')) {
    if ($this->Contact->save($this->request->data)) {
        // handle the success.
    } else {
       $this->Session->setFlash(__('The Contact could not be saved. Please, try again.'));
    }
}
于 2013-02-09T23:50:55.320 回答
0

在您的联系操作中,您所做的只是加载联系模型。您必须显式调用相关的模型方法来执行验证。正确阅读手册以了解如何做到这一点。

于 2013-02-09T19:59:20.600 回答