Inserting with empty string in cakePHP -


i new in cakephp framework version 2.0 problem when save data or insert new record field empty or no data save how can fix this. .

my model

class post extends appmodel{     public $name = 'posts'; }  

my controller

public function add(){    if($this->request->is('post')){         $this->post->create();         if($this->post->save($this->request->data)){             $this->session->setflash('the posts saved');             $this->redirect('index');         }            } } 

my view

echo $this->form->create('create posts'); echo $this->form->input('title'); echo $this->form->input('body'); echo $this->form->end('save posts'); 

you need put validation rules in post model, can check validate data or not in controller action before save model. see following model , controller

in model

class post extends appmodel{     public $name = 'posts';      public $validate = array(         'title' => array(             'alphanumeric' => array(                 'rule' => 'alphanumeric',                 'required' => true,                 'message' => 'this can'\t blank'             ),          ),         'body' => array(             'alphanumeric' => array(                 'rule' => 'alphanumeric',                 'required' => true,                 'message' => 'this can'\t blank'             ),                     ),     ); }  

in controller

public function add(){    if($this->request->is('post')){         $this->post->create();         $this->post->set($this->request->data);          if ($this->post->validates()) {         // validated logic             if($this->post->save($this->request->data)){                 $this->session->setflash('the posts saved');                 $this->redirect('index');             }         } else {             // didn't validate logic             $errors = $this->post->validationerrors;         }            } } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -