views:

131

answers:

1

I'm following the blog tutorial on CakePHP website, but validation doesn't work and i don't understand why, because i'm using the blog tutorial code.

I'll report directly from my files anyway...

MODEL

class Post extends AppModel
{
    var $name = 'Post';

    var $validate = array(
     'title' => array(
      'rule' => 'notEmpty'
     ),
     'body' => array(
      'rule' => 'notEmpty'
     )
    );
}

CONTROLLER

class PostsController extends AppController {
    var $name = 'Posts';

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

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

    }

    function add() {
     if (!empty($this->data)) {
      if ($this->Post->save($this->data)) {
       $this->Session->setFlash('Your post has been saved.');
       $this->redirect(array('action' => 'index'));
      }
     }
    }
}

VIEW (add view)

<h1>Add Post</h1>
<?php
echo $form->create('Post');
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->end('Save Post');
?>

When i reach the /posts/add page and then click to Save without any input text it doesn't reload with error, it inserts the empty data into db and then redirect me with the message "Your post has been saved".

Why doesn't validate itself?? I've read into docs that it's not necessary to call validate(), save() it's enough.

Any ideas about?

A: 

Ok i found the error: i was naming the model files capitalizing them. So, i had a Post.php instead of a post.php.

CakePHP didn't find my models and so it was using its "generated" models.

avastreg