みなさんお疲れ様です。
先日はたくさんの質問にお答えいただきましてありがとうございました。
isUniqueに関してはいまだに効いてなかったりしますが、あらかた
validationと例外処理につきましては対応できました。(自分の中で)
ちなみにこんな風になりました。
Model Report.php
php
1<?php 2App::uses('AppModel', 'Model'); 3class Report extends AppModel { 4 5 var $name = 'Report'; 6 7 var $validate = array( 8 9 'userObjectId' => array( //50文字以内、英数字のみ、空欄不可 10 array( 11 'rule' => array('maxLength', 50), 12 'message' => '50文字以内に収めてください' 13 ), 14 array( 15 'rule' => 'alphaNumeric', 16 'message' => '英数字のみ可です' 17 ), 18 array( 19 'rule' => 'notEmpty', 20 'message' => '空欄は許可しておりません' 21 ) 22 ), 23 24 'requestId' => array( //50文字以内、英数字のみ、空欄不可 25 array( 26 'rule' => array('maxLength', 50), 27 'message' => '50文字以内に収めてください' 28 ), 29 array( 30 'rule' => 'alphaNumeric', 31 'message' => '英数字のみ可です' 32 ), 33 array( 34 'rule' => 'notEmpty', 35 'message' => '空欄は許可しておりません' 36 ) 37 ), 38 39 'reason' => array( //数字のみ、1文字、空欄不可 40 array( 41 'rule' => 'notEmpty', 42 'message' => '空欄は許可しておりません' 43 ), 44 array( 45 'rule' => '/[0-9]{1}/', 46 'message' => '数字のみで1文字のみ可' 47 ) 48 ), 49 50 'message' => array( //1000文字以内、空欄許可 51 'rule' => array('maxLength', 1000), 52 'message' => '1000文字以内に収めてください', 53 'allowEmpty' => true 54 ), 55 56 'comment' => array( //66文字以内、空欄許可 57 'rule' => array('maxLength', 66), 58 'message' => '66文字以内に収めてください', 59 'allowEmpty' => true 60 ), 61 62 'status' => array( //数字のみ、1文字、空欄不可 63 array( 64 'rule' => '/[0-9]{1}/', 65 'message' => '数字のみで1文字のみ可' 66 ), 67 array( 68 'rule' => 'notEmpty', 69 'message' => '空白は許可していません。' 70 ) 71 ), 72 73 'modified' => array( //空欄許可、50文字以内 74 array( 75 'rule' => 'notEmpty', 76 'message' => '空白は許可していません。' 77 ), 78 array( 79 'rule' => array('maxLength', 50), 80 'message' => '50文字以内に収めてください' 81 ) 82 ), 83 84 'created' => array( //空欄不可、50文字以内 85 array( 86 'rule' => 'notEmpty', 87 'message' => '空白は許可していません。' 88 ), 89 array( 90 'rule' => array('maxLength', 50), 91 'message' => '50文字以内に収めてください' 92 ) 93 ) 94 95 ); 96 97}
DetailsController.php
php
1<?php 2 3App::uses('AppController', 'Controller'); 4 5class DetailsController extends AppController { 6 //読み込むコンポーネントの指定 7 public $components = array('Session', 'Auth'); 8 9 var $uses = array("Report", "Request"); 10 11 //どのアクションが呼ばれてもはじめに実行される関数 12 public function beforeFilter() { 13 parent::beforeFilter(); 14 15 $this->Auth->allow(); 16 $this->set('user', $this->Auth->user()); 17 $this->layout = 'main'; //レイアウトを指定 18 19 //未ログインでアクセスできるアクションを指定 20 //これ以外のアクションへのアクセスはloginにリダイレクトされる規約になっている 21 $this->Auth->deny('index', 'logout'); 22 } 23 24 public function index() { 25 26 if($this->request->is('get')){ 27 28 //queryのidを元にreportのオブジェクトを取得------------------------------------- 29 $reportId = $this->request->query['id']; 30 31 $report = $this->Report->find('all', array('conditions' => array("id" => $reportId)));//そのreportsの中にあるさっきのidがあるドキュメントの情報を取得 32 $report_obj = $report[0]["Report"]; 33 34 //ReportsCollectionから変数をセット------------------------------------------- 35 $userid = $report_obj["userObjectId"]; 36 $requestId = $report_obj["requestId"]; 37 $reason = $report_obj["reason"]; 38 $message = htmlspecialchars($report_obj["message"]); 39 $comment = $report_obj["comment"]; 40 $reportstatus = $report_obj["status"]; 41 $modified = $report_obj["modified"]->toDateTime()->format('Y-m-d H:i:s'); 42 $created = $report_obj["created"]->toDateTime()->format('Y-m-d H:i:s'); 43 44 //report_objのrequestIdから該当するオブジェクトを取得---------------------------- 45 $request = $this->Request->find('all', array('conditions' => array("id" => $requestId)));//先ほどのrequestIdがあるrequestsの中のドキュメントを取得 46 $request_obj = $request[0]["Request"]; 47 48 //RequestsCollectionから変数をセット------------------------------------------ 49 $imagepath = $request_obj["imagePath"]; 50 $requeststatus = $request_obj["status"]; 51 52 //ビューに変数を引き渡す------------------------------------------------------- 53 $this->set(compact("reportId", "userid", "requestId", "reason", "message", "comment", "reportstatus", "modified", "created", "imagepath", "requeststatus")); 54 $this->set('imagehost', IMAGEPATH); 55 56 } else { //this->request->is('post')の場合 57 throw new NotFoundException('このPostは見つかりませんでした。'); 58 } 59 } 60 61 public function update(){ 62 63 $this->autoRender = false; 64 //処理状態の更新-------------------------------------------------------------- 65 66 if($this->request->is('post')){ 67 68 if(isset($this->request->data['report_status'])){ 69 $reportId = $this->request->data['report_id']; 70 $report = $this->Report->find('all', array('conditions' => array("id" => $reportId)));//そのreportsの中にあるさっきのidがあるドキュメントの情報を取得 71 $report_obj = $report[0]["Report"]; 72 $report_obj["status"] = $this->request->data['report_status']; 73 $report_obj["modified"] = array('modified' => date('Y-m-d H:i:s')); 74 $data = $report_obj; 75 $this->Report->save($data); 76 77 //条件分岐による例外処理 78 if ( $this->Report->save($data) === false ) { 79 $this->Session->setFlash('処理状態の更新に失敗しました'); 80 $this->Session->flash(); 81 } 82 83 $this->redirect($_SERVER['HTTP_REFERER']); 84 85 } 86 //表示状態の更新-------------------------------------------------------------- 87 88 if(isset($this->request->data['request_status'])){ 89 $requestId = $this->request->data['request_id']; 90 $request = $this->Request->find('all', array('conditions' => array("id" => $requestId)));//そのreportsの中にあるさっきのidがあるドキュメントの情報を取得 91 $request_obj = $request[0]["Request"]; 92 $request_obj["status"] = $this->request->data['request_status']; 93 $data = $request_obj; 94 $this->Request->save($data); 95 96 if ( $this->Request->save($data) === false ) { 97 $this->Session->setFlash('表示状態の更新に失敗しました'); 98 $this->Session->flash(); 99 } 100 101 $reportId = $this->request->data['report_id']; 102 $report = $this->Report->find('all', array('conditions' => array("id" => $reportId)));//そのreportsの中にあるさっきのidがあるドキュメントの情報を取得 103 $report_obj = $report[0]["Report"]; 104 $report_obj["modified"] = array('modified' => date('Y-m-d H:i:s')); 105 $data = $report_obj; 106 $this->Report->save($data); 107 108 if ( $this->Report->save($data) === false ) { 109 $this->Session->setFlash('表示状態の更新に失敗しました'); 110 $this->Session->flash(); 111 } 112 113 $this->redirect($_SERVER['HTTP_REFERER']); 114 115 } 116 //コメントの更新-------------------------------------------------------------- 117 118 if(isset($this->request->data['report_comment'])){ 119 $reportId = $this->request->data['report_id']; 120 $report = $this->Report->find('all', array('conditions' => array("id" => $reportId)));//そのreportsの中にあるさっきのidがあるドキュメントの情報を取得 121 $report_obj = $report[0]["Report"]; 122 $report_obj["comment"] = $this->request->data['report_comment']; 123 $report_obj["modified"] = array('modified' => date('Y-m-d H:i:s')); 124 $data = $report_obj; 125 $this->Report->save($data); 126 127 //条件分岐による例外処理 128 if ( $this->Report->save($data) === false ) { 129 $this->Session->setFlash('コメントの更新に失敗しました'); 130 $this->Session->flash(); 131 } 132 133 $this->redirect($_SERVER['HTTP_REFERER']); 134 135 } 136 137 } else { 138 throw new NotFoundException('このGetは見つかりませんでした。'); 139 } 140 141 } 142} 143
長くなってすみません。
そして、これを上司に見せたところ、validationをかけるのはいいが、このフィールド自体がなかった場合にはどうするのか、その時の処理をかかなければいけないと言われました。
正直僕自身も今の状態が正しいのか、判断ついていないので、
悪意を持ってされることを防ぐためにどういった処理が必要でどういう風に書かなければいけないのでしょうか。
抽象的な質問になって申し訳ないのですが、よろしければかいつまんで一部分でもいいので回答いただければと思います。
宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/08/19 12:57