dongre6227 2013-02-09 19:26
浏览 30

CakePHP没有检查表单验证

I'm writing a contact form and want to add some simple validation routines. The action for this page looks like this:

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

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

and the Contact model is this:

<?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'
        )
    )
);

}

and finally my view page:

<?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')); ?>

However, when I submit the form with incorrect values the validation routines aren't called and no error messages are shown.

How can I get Cake to validate the form?

  • 写回答

2条回答 默认 最新

  • douzi9430 2013-02-09 19:59
    关注

    In your contact action all you are doing is loading the Contact model. You have to explicitly call the relevant model method to perform validation. Read the manual properly for how to do that.

    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备