From 46196264044404319d04c876e2c68bae3e50ada2 Mon Sep 17 00:00:00 2001 From: cake_67 Date: Thu, 26 Nov 2009 02:22:31 +0000 Subject: [PATCH] =?utf8?q?=3D=E3=83=91=E3=82=B9=E3=83=AF=E3=83=BC=E3=83=89?= =?utf8?q?=E7=A2=BA=E8=AA=8D=E5=85=A5=E5=8A=9B=E3=81=AE=E3=83=90=E3=83=AA?= =?utf8?q?=E3=83=87=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/trpgtools-onweb/src/trunk/members_framework/cakePHP_frame@135 46fa8b77-3530-0410-9d82-d95c44d28aba --- app/controllers/users_controller.php | 22 ++- app/models/app_model.php | 11 +- app/models/user.php | 14 +- .../models/behaviors/add_validation_rule.php | 194 +++++++++++++++++++++ app/views/users/admin_add.ctp | 21 ++- 5 files changed, 243 insertions(+), 19 deletions(-) create mode 100644 app/plugins/cakeplus/models/behaviors/add_validation_rule.php diff --git a/app/controllers/users_controller.php b/app/controllers/users_controller.php index 7c30c74..80e6c27 100644 --- a/app/controllers/users_controller.php +++ b/app/controllers/users_controller.php @@ -33,14 +33,24 @@ class UsersController extends ModuleController { } function admin_add() { + $this->set('idLength', Configure::read('User.UserId.Length')); + $this->set('passwordLength', Configure::read('User.Password.Length')); + if (!empty($this->data)) { - $this->User->create(); - if ($this->User->save($this->data)) { - $this->Session->setFlash(__('The User has been saved', true)); - $this->redirect(array('action'=>'index')); - } else { - $this->Session->setFlash(__('The User could not be saved. Please, try again.', true)); + // バリデーション + $this->User->set($this->data); + if ($this->User->validates()) { + // passwordセット + $this->data['User']['password'] = $this->Auth->password($this->data['User']['password1']); + + // save + $this->User->create(); + if ($this->User->save($this->data)) { + $this->Session->setFlash(__('The User has been saved', true)); + $this->redirect(array('action'=>'index')); + } } + } } diff --git a/app/models/app_model.php b/app/models/app_model.php index d313b85..8e9caa8 100644 --- a/app/models/app_model.php +++ b/app/models/app_model.php @@ -1,12 +1,15 @@ __('Please be sure to input.', true), 'between' => __('Between %2$d and %3$d characters.', true), - 'alphaNumeric' => __('Please input only alphameric characters.',true), + 'alphaNumeric' => __('Please input only alphameric characters.', true), + 'compare2fields' => __('Please input same as above.', true), ); $this->setErrorMessageI18n($error_messages, false); @@ -15,7 +18,8 @@ class AppModel extends Model { } /* 本線alphaNumeric()の上書き */ - function alphaNumeric($data) { + function alphaNumeric($data) + { $check = is_array($data) ? array_shift($data) : $data; if (preg_match('/^[0-9a-z]+$/i',$check)) { return true; @@ -25,4 +29,3 @@ class AppModel extends Model { } } - diff --git a/app/models/user.php b/app/models/user.php index 8dd0a10..34936e7 100644 --- a/app/models/user.php +++ b/app/models/user.php @@ -2,6 +2,9 @@ class User extends AppModel { var $name = 'User'; + var $actsAs = array( + 'Cakeplus.AddValidationRule', + ); var $validate = array( 'username' => array( @@ -21,6 +24,9 @@ class User extends AppModel { ), ), 'password2' => array( + 'compare2fields' => array( + 'rule' => array('compare2fields', 'password1', false), + ), 'alphaNumeric' => array( 'rule' => 'alphaNumeric', ), @@ -29,12 +35,6 @@ class User extends AppModel { ), ) ); -/* - function checkUsernameBetween($data) - { - $idLength = Configure::read('User.UserId.Length'); - return checkBetween($data, $idLength['min'], $idLength['max']); - } -*/ + } ?> \ No newline at end of file diff --git a/app/plugins/cakeplus/models/behaviors/add_validation_rule.php b/app/plugins/cakeplus/models/behaviors/add_validation_rule.php new file mode 100644 index 0000000..35861af --- /dev/null +++ b/app/plugins/cakeplus/models/behaviors/add_validation_rule.php @@ -0,0 +1,194 @@ + array('encoding' => 'EUC') ); + * + * + * 各モデルファイル内のバリデーションの書き方は下記を参考に。 + * Example: validation definition in a model. + * var $validate = array( + * 'test' => array( + * "rule2" => array('rule' => array('maxLengthJP', 5), + * 'message' => '5文字以内です' + * ), + * "rule3" => array('rule' => array('minLengthJP', 2), + * 'message' => '2文字以上です' + * ), + * "rule4" => array('rule' => array('compare2fields', 'test_conf'), + * 'message' => '値が違います' + * ), + * "rule5" => array('rule' => array('space_only'), + * 'message' => 'スペース以外も入力してください' + * ), + * "rule6" => array('rule' => array('katakana_only'), + * 'message' => 'カタカナのみ入力してください' + * ), + * ), + * ); + * + * Authコンポーネントでパスワードフィールドがハッシュ化されている場合は、compare2fieldsの第3配列にtrueを指定する + * Using Auth component, If you want compare password and password confirm field, + * set "true" in 3rd parameter of compare2fields validation, password_conf field is encrypted. + * var $validate = array( + * 'password' => array( + * "rule" => array('rule' => array('compare2fields', 'password_conf',true), + * 'message' => '値が違います' + * ), + * ), + * ); + * + * + */ +class AddValidationRuleBehavior extends ModelBehavior { + + function setup(&$model, $config = array()){ + + //change encoding with parameter option. + if( !empty( $config['encoding'] ) ){ + mb_internal_encoding($config['encoding']); + }else{ + mb_internal_encoding("UTF-8"); + } + } + + + /** + * マルチバイト用バリデーション 文字数上限チェック + * check max length with Multibyte character. + * + * @param array &$model model object, automatically set + * @param array $wordvalue field value, automatically set + * @param int $length max length number + * @return boolean + */ + function maxLengthJP( &$model, $wordvalue, $length ) { + $word = array_shift($wordvalue); + return( mb_strlen( $word ) <= $length ); + } + + /** + * マルチバイト用バリデーション 文字数下限チェック + * check min length with Multibyte character. + * + * @param array &$model model object, automatically set + * @param array $wordvalue field value, automatically set + * @param int $length min length number + * @return boolean + */ + function minLengthJP( &$model, $wordvalue, $length ) { + $word = array_shift($wordvalue); + return( mb_strlen( $word ) >= $length ); + } + + + /** + * フィールド値の比較 + * emailとemail_confフィールドを比較する場合などに利用 + * $compare_filedに比較したいフィールド名をセットする(必須) + * Compare 2 fields value. Example, email field and email_conf field. + * Set field name for comparison in $compare_filed + * + * authにtrueを指定すると、Authコンポーネントのパスワードフィールドを前提として + * 比較するpassword_confフィールドの値をハッシュ化する + * If set "true" in $auth, $compare_filed is encrypted with Security::hash. + * + * @param array &$model model object, automatically set + * @param array $wordvalue field value, automatically set + * @param string $compare_filed set field name for comparison + * @param boolean $auth set true, $compare_filed is encrypted with Security::hash + * @return boolean + */ + function compare2fields( &$model, $wordvalue , $compare_filed , $auth = false ){ + + $fieldname = key($wordvalue); + if( $auth === true ){ + App::import('Component','Auth'); + return ( $model->data[$model->alias][$fieldname] === AuthComponent::password($model->data[$model->alias][ $compare_filed ]) ); + }else{ + return ( $model->data[$model->alias][$fieldname] === $model->data[$model->alias][ $compare_filed ] ); + } + } + + + + /** + * 全角カタカナ以外が含まれていればエラーとするバリデーションチェック + * Japanese KATAKANA Validation + * + * @param array &$model + * @param array $wordvalue + * @return boolean + */ + function katakana_only( &$model, $wordvalue){ + + $value = array_shift($wordvalue); + + return preg_match("/^[ァ-ヶー゛゜]*$/u", $value); + + } + + + + + /** + * 全角、半角スペースのみであればエラーとするバリデーションチェック + * Japanese Space only validation + * + * @param array &$model + * @param array $wordvalue + * @return boolean + */ + function space_only( &$model, $wordvalue){ + + $value = array_shift($wordvalue); + + if( mb_ereg_match("^(\s| )+$", $value) ){ + + return false; + }else{ + return true; + } + } + + + /** + * only Allow 0-9, a-z , A-Z + * check it including Multibyte characters. + * + * @param array ref &$model + * @param array $wordvalue + * @return boolean + */ + function alpha_number( &$model, $wordvalue ){ + $value = array_shift($wordvalue); + return preg_match( "/^[a-zA-Z0-9]*$/", $value ); + + } + +} + +?> diff --git a/app/views/users/admin_add.ctp b/app/views/users/admin_add.ctp index 3b828c6..b1c8803 100644 --- a/app/views/users/admin_add.ctp +++ b/app/views/users/admin_add.ctp @@ -3,8 +3,25 @@
input('username'); - echo $form->input('password'); + echo $form->input('username', array( + 'label' => __('UserId', true), + 'maxlength' => $idLength['max'], + 'after' => sprintf(__('Between %d to %d characters', true), $idLength['min'], $idLength['max']), + ) + ); + echo $form->input('password1', array( + 'label' => __('Password', true), + 'maxlength' => $passwordLength['max'], + 'after' => sprintf(__('Between %d to %d characters', true), $passwordLength['min'], $passwordLength['max']), + 'type' => 'password', + ) + ); + echo $form->input('password2', array( + 'label' => __('Password [confirm]', true), + 'maxlength' => $passwordLength['max'], + 'type' => 'password', + ) + ); ?>
end('Submit');?> -- 2.11.0