Question

I've been learning how to use CakePHP using a video tutorial series, and I'm having issues with validation on forms. I've tried several different things that I've found on the CakePHP book and it doesn't seem to be working either. 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!

Was it helpful?

Solution

The problem is that the rules you're trying to use aren't defined in the CakePHP framework. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top