简体   繁体   English

CakePHP表单验证不起作用

[英]CakePHP Form Validation Not Working

I've been learning how to use CakePHP using a video tutorial series, and I'm having issues with validation on forms. 我一直在通过视频教程系列学习如何使用CakePHP,并且在表单验证方面遇到了问题。 I've tried several different things that I've found on the CakePHP book and it doesn't seem to be working either. 我已经尝试过在CakePHP本书中找到的几种不同的方法,而且似乎也不起作用。 It's pretty simple validation, just making sure that the title isn't empty or duplicate, and that the post isn't empty, yet the form is still submitting regardless of whether it's blank or duplicate. 这是非常简单的验证,只需确保标题不为空或重复,并且帖子不为空,但无论表单为空白还是重复,表单仍在提交。

Here is my model: 这是我的模型:

class Post extends AppModel {

    var $name = 'Post';
    var $validate = array(
        'title'=>array(
            'title_must_not_be_blank'=>array(
                'rule'=>'notEmpty',
                'message'=>'This post is missing a title!'
            ),
            'title_must_be_unique'=>array(
                'rule'=>'isUnique',
                'message'=>'A post with this title already exists!'
            )
        ),
        'body'=>array(
            'body_must_not_be_blank'=>array(
                'rule'=>'notEmpty',
                'message'=>'This post is missing its body!'
            )
        )
    );
}

Here is the controller: 这是控制器:

class PostsController extends AppController {

    var $name = 'Posts';

    function index() {
        $this->set('posts', $this->Post->find('all'));
    }

    function view($id = NULL) {
        $this->set('post', $this->Post->read(NULL, $id));
    }

    function add() {
        if (!empty($this->request->data)) {
            if($this->Post->save($this->request->data)) {
                $this->Session->setFlash('The post was successfully added!');
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash('The post was not saved... Please try again!');

            }
        }
    }

    function edit($id = NULL) {
        if(empty($this->data)) {
            $this->data = $this->Post->read(NULL, $id);
        } else {
            if($this->Post->save($this->data)) {
                $this->Session->setFlash('The post has been updated');
                $this->redirect(array('action'=>'view', $id));
            }
        }
    }

    function delete($id = NULL) {
        $this->Post->delete($id);
        $this->Session->setFlash('The post has been deleted!');
        $this->redirect(array('action'=>'index'));
    }

}

And here is the view: 这是视图:

<h2>Add a Post</h2>
<?php
echo $this->Form->create('Post', array('action'=>'add'));
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Create Post');
?>

<p><?php echo $this->Html->link('Cancel', array('action'=>'index')); ?></p>

Thanks in advance! 提前致谢!

The problem is that the rules you're trying to use aren't defined in the CakePHP framework. 问题在于,您要使用的规则未在CakePHP框架中定义。 If you want to require something and make sure the field isn't empty you should try this: 如果您需要某些内容并确保该字段不为空,则应尝试以下操作:

 'title' => array(
            'required' => array(
                'rule' => array('minLength', 1),
                'allowEmpty' => false,
                'message' => 'Please enter a title.'
            )          
       ),

The 'required' key tells Cake that the field is required while 'allowEmpty' => false tells Cake that the field needs to contain something and can't just be an empty string. 'required'键告诉Cake该字段是必填字段,而'allowEmpty' => false告诉Cake该字段需要包含某些内容,不能只是一个空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM