【问题标题】:CakePHP not checking form validationCakePHP 不检查表单验证
【发布时间】:2013-02-09 19:26:04
【问题描述】:

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

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 验证表单?

【问题讨论】:

    标签: php validation cakephp view model


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      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.'));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 2012-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多