###前提・実現したいこと
cakePHPでecサイトを作っています。
今はパスワード再設定画面を手掛けていて
パスワードを入力したときにvalidationをかけたいのですが、メッセージが表示されません。
###発生している問題・エラーメッセージ
パスワード再設定画面でvalidationをかけているのですが、
引っかかったときのエラーメッセージが出ません。
(メッセージが出ないだけで、validationにはちゃんと引っかかります)
参考書や色々なサイトを見る限り、view画面の方で
と記述すればできるとのことなので同じようにしてみたのですが表示されません、、。
以下ソースコードです。長くて申し訳ありませんが、よろしくお願い致します。
###該当のソースコード
【Model】
User.php…validation設定を行っているモデル
//パスワードが一致しているかをチェックするバリデータ public function confirm($check){ foreach ($check as $key => $value) { //カラムに値がなかったら(issetしてなかったら)falseを返す if(! isset($this->data[$this->name][$key.'_confirm'])){ return false; } //$valueの値と、そのカラムに_confirmを足した値が等しくなかったら //falseを返す(確認入力をさせる場合に使うチェック) if($value !== $this->data[$this->name][$key.'_confirm']){ return false; } } //入力が正しいことを確認したらtrueを返す return true; } public validate = [ 'password' => [ [ 'rule' => 'notBlank', 'message' => 'パスワードを入力してください。' ], [ 'rule' => ['custom','/^[(a-z)(A-Z)(0-9)]+$/'], 'message' => '半角英数字で入力してください。' ], [ 'rule' => 'confirm', 'message' => 'パスワードが一致していません。' ], [ 'rule' => ['between', 6, 32], 'message' => '6文字以上、32文字以下で入力して下さい。' ] ] ];
【Model】
ResetPassword.php…パスワード再設定の処理を行っているモデル
public function reissuePassword($activation_code,$password,$password_again) { if(is_null($activation_code) || is_null($password) || is_null($password_again)) { //引数が不正 assert(false); return -10; } App::import('Model', 'TransactionManager'); $transaction_manager = new TransactionManager(); //start transaction $transaction = $transaction_manager->begin(); $commit = false; try { $options = [ 'fields' => [ 'ResetPassword.user_id' ], 'conditions' => [ 'ResetPassword.activate_code' => $activation_code, ] ]; $rec = $this->find('first',$options); App::import('Model','User'); $user = new User(); $user->validate = [ 'password' => $user->validate['password'], ]; $user->set([ 'User' => [ 'user_id' => $rec['ResetPassword']['user_id'], 'password' => $password, 'password_confirm' => $password_again ] ]); if(! $user->validates()) { //バリデーションで引っかかった場合 return; } if(!$user->save()) { //トランザクション失敗 $commit = false; return -2; } $commit = true; return 1; return $result; } finally { if($commit) { if(! $transaction_manager->commit($transaction)) { //コミットが失敗したとき } else { $transaction_manager->rollback($transaction); } } } }
【Controller】
AuthController.php…ログイン、パスワード再発行などのサインイン全体を管理しているコントローラー。
ResetPasswordモデルのreissuePassword()を呼び出して結果を受け取っている。
public function reissue_password() { if(is_null(Hash::get($this->request->data,'password')) || is_null(Hash::get($this->request->data,'password_again'))) { $this->set('code',Hash::get($this->request->query,'code')); $this->render('reissue_password'); return; } $password = Hash::get($this->request->data,'password'); $password_again = Hash::get($this->request->data,'password_again'); $activation_code = Hash::get($this->request->data,'code'); $this->loadModel('ResetPassword'); $result = $this->ResetPassword->reissuePassword($activation_code,$password,$password_again); switch($result) { case 1: //パスワード再設定完了 $this->render('resetted_password'); break; case -10: //引数が不正なとき $this->redirectIllegalParameter(); break; case -2: //トランザクションエラー $this->redirectCriticalError(); break; } }
【View】
resetted_password.ctp…パスワード再設定画面
<?php $this->start('title'); ?>パスワード再設定<?php $this->end(); ?> パスワードを再設定します。 半角英数字で入力してください。 <?php echo $this->Form->create('User',['type' => 'post','url'=> ['controller' => 'auth','action' => 'reissue_password'],'novalidate' => true]); ?> <?php echo $this->Form->error('User.password'); ?> <?php echo $this->Form->password('password',['placeholder' => 'パスワードを入力','required' => false]); ?> <?php echo $this->Form->password('password_again',['placeholder' => 'パスワードを再入力','required' => false]); ?> <?php echo $this->Form->hidden('code',['value' => $code]); ?> <?php echo $this->Form->end('パスワードを再設定'); ?>
###補足情報(言語/FW/ツール等のバージョンなど)
- 言語:PHP5
- フレームワーク:cakePHP 2.9
- 実行環境: XAMP
回答2件
あなたの回答
tips
プレビュー