From 8d00df988d6bf1bba545a84aac6f216a02bc1897 Mon Sep 17 00:00:00 2001 From: s-nakajima Date: Wed, 29 Jun 2016 16:29:46 +0900 Subject: [PATCH] =?UTF-8?q?BbsSetting=E3=81=AB=E3=83=90=E3=83=AA=E3=83=87?= =?UTF-8?q?=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E8=BF=BD=E5=8A=A0=20https:?= =?UTF-8?q?//github.com/NetCommons3/Bbses/issues/25?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/BbsFrameSetting.php | 1 - Model/BbsSetting.php | 43 ++++++++++++++++++++- Test/Case/Model/BbsSetting/ValidateTest.php | 35 +++-------------- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/Model/BbsFrameSetting.php b/Model/BbsFrameSetting.php index b569ec8..fa462e9 100644 --- a/Model/BbsFrameSetting.php +++ b/Model/BbsFrameSetting.php @@ -101,7 +101,6 @@ public function saveBbsFrameSetting($data) { //バリデーション $this->set($data); if (! $this->validates()) { - $this->rollback(); return false; } diff --git a/Model/BbsSetting.php b/Model/BbsSetting.php index 2ab596a..ccec393 100644 --- a/Model/BbsSetting.php +++ b/Model/BbsSetting.php @@ -37,6 +37,48 @@ class BbsSetting extends BbsesAppModel { 'Blocks.BlockRolePermission', ); +/** + * beforeValidate + * + * @param array $options Options passed from Model::save(). + * @return bool True if validate operation should continue, false to abort + * @link http://book.cakephp.org/2.0/ja/models/callback-methods.html#beforevalidate + * @see Model::save() + */ + public function beforeValidate($options = array()) { + $this->validate = Hash::merge($this->validate, array( + 'bbs_key' => array( + 'notBlank' => array( + 'rule' => array('notBlank'), + 'message' => __d('net_commons', 'Invalid request.'), + 'allowEmpty' => false, + 'required' => true, + 'on' => 'update', // Limit validation to 'create' or 'update' operations + ), + ), + 'use_workflow' => array( + 'boolean' => array( + 'rule' => array('boolean'), + 'message' => __d('net_commons', 'Invalid request.'), + ), + ), + 'use_like' => array( + 'boolean' => array( + 'rule' => array('boolean'), + 'message' => __d('net_commons', 'Invalid request.'), + ), + ), + 'use_unlike' => array( + 'boolean' => array( + 'rule' => array('boolean'), + 'message' => __d('net_commons', 'Invalid request.'), + ), + ), + )); + + return parent::beforeValidate($options); + } + /** * Get bbs setting data * @@ -75,7 +117,6 @@ public function saveBbsSetting($data) { //バリデーション $this->set($data); if (! $this->validates()) { - $this->rollback(); return false; } diff --git a/Test/Case/Model/BbsSetting/ValidateTest.php b/Test/Case/Model/BbsSetting/ValidateTest.php index e2782ed..668ba7c 100644 --- a/Test/Case/Model/BbsSetting/ValidateTest.php +++ b/Test/Case/Model/BbsSetting/ValidateTest.php @@ -148,38 +148,13 @@ public function dataProviderSaveOnValidationError() { */ public function dataProviderValidationError() { return array( - array($this->__getData(), 'use_workflow', 'a', //PENDING バリデーションなし? + array($this->__getData(), 'use_workflow', 'a', + __d('net_commons', 'Invalid request.')), + array($this->__getData(), 'use_like', 'a', + __d('net_commons', 'Invalid request.')), + array($this->__getData(), 'use_unlike', 'a', __d('net_commons', 'Invalid request.')), ); } -/** - * Validatesのテスト - * - * @param array $data 登録データ - * @param string $field フィールド名 - * @param string $value セットする値 - * @param string $message エラーメッセージ - * @param array $overwrite 上書きするデータ - * @dataProvider dataProviderValidationError - * @return void - */ - public function testValidationError($data, $field, $value, $message, $overwrite = array()) { - $model = $this->_modelName; - - if (is_null($value)) { - unset($data[$model][$field]); - } else { - $data[$model][$field] = $value; - } - $data = Hash::merge($data, $overwrite); - - //validate処理実行 - $this->$model->set($data); - $result = $this->$model->validates(); - $this->assertTrue($result); //PENDING - //$this->assertFalse($result); PENDING バリデーションなしの場合? - //$this->assertEquals($this->$model->validationErrors[$field][0], $message); - } - }