php - CakePHP: Save is failing due to validation failure -



php - CakePHP: Save is failing due to validation failure -

my "user" model code class user extends appmodel{ public $validate = array( 'username' => array( 'username_shoud_be_unique' => array( 'rule' => 'isunique', 'message' => 'username taken' ), 'username_cannot_be_empty' => array( 'rule' => 'notempty', 'message' => 'usename field cannot empty' ) ), 'password' => array( 'rule' => 'notempty', 'message' => 'password field cannot empty' ), 'confirm_password' => array( 'confirm_password_cannot_be_empty' => array( 'rule' => 'notempty', 'message' => 'confirm password field cannot empty' ), 'confirm_password_must_match_password' => array( 'rule' => array('checkpassword'), 'message' => 'password , confirm password field value should match' ) ), 'email' => array( 'email_should_be_unique' => array( 'rule' => 'isunique', 'message' => 'this email registered' ), 'email_cannot_be_empty' => array( 'rule' => 'notempty', 'message' => 'email cannot empty' ) ) ); public function checkpassword(){ if(!($this->data['user']['password'] === $this->data['user']['confirm_password'])){ $this->invalidate('password','password , confirm password field value should match'); homecoming false; } homecoming true; } }

and "userscontroller.php" is

class userscontroller extends appcontroller{ public $helper = array('html','form','session'); public $components = array('session'); public function index(){ if($this->request->is('post')){ $this->user->create(); $this->request->data['user']['ip'] = $this->request->clientip(); $this->request->data['user']['username_clean'] = strtolower($this->request->data['user']['username']); $this->request->data['user']['confirmcode'] = md5(md5($this->generaterandomstring())."com.ultimate-videochat"); if($this->user->save($this->request->data)){ $this->session->setflash(__('user created successfully')); } else{ $this->session->setflash(__('unable create user')); } } } //this function internal use. it's not action. private function generaterandomstring($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, strlen($characters) - 1)]; } homecoming $randomstring; } }

and index.ctp http://pastebin.com/difhlnv8

the problem when user can't saved due info validation failure, when user enters username exist, throws flash message "unable create user". how message when user can't created other reasons "connection failure" or other technical reason. validation failure reddish mark on fields enough.

you prevent flash message setting controller itself.

although don't believe do. what's wrong displaying flash message form error? that's more usable :)!

anyway if still believe want hide on validation error cases here go:

if($this->user->save($this->request->data)) { $this->session->setflash(__('user created successfully')); } else { if (empty($this->user->validationerrors)) { $this->session->setflash(__('unable create user')); } }

php validation cakephp

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -