php - the data validation is always return true in cakephp -


the data validation in user model return true. can tell me why? model: user.php

class user extends appmodel {  var $name = "user";  public $validate = array(      "username" => array(         "rule" => "notblank",         "message" => "please enter title !",     ),     "password" => array(         "rule" => "notblank", // tập luật là không rỗng         "message" => "please enter info !", // thông báo khi có lỗi     ));   } 

userscontroller:

if(($this->request->is('post'))) {          $this->loadmodel('user');         $this->user->set($this->data);         echo "</br> vao ".$this->user->validates();         if($this->user->validates()){             echo "</br>".$this->user->validates();             $this->session->setflash("data avaliable !");          }else{             $error = $this->user->validationerrors;             $this->session->setflash("data mot avaliable !");             echo"not validate";         } 

but return alway true.

i tried run cakephp 2..7.2 , 2.6.0 result still same.

in cakephp 2.x request/form data accessed via $this->request->data not $this->data. need change line set model's data using $this->user->set() use $this->request->data:-

if ($this->request->is('post')) {         $this->user->set($this->request->data);         if ($this->user->validates()){             $this->session->setflash("data avaliable !");         } else{             $this->session->setflash("data not avaliable !");         } } 

as long you're sticking cakephp conventions there no need load user model in userscontroller should auto-loaded. can remove $this->loadmodel('user').

if you're using notblank validation rule make sure using cakephp 2.7 rule not exist in earlier versions of cake. cakephp 2.6 use notempty rule.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -