OSDN Git Service

=パスワード確認入力のバリデーション追加
authorcake_67 <cake_67@46fa8b77-3530-0410-9d82-d95c44d28aba>
Thu, 26 Nov 2009 02:22:31 +0000 (02:22 +0000)
committercake_67 <cake_67@46fa8b77-3530-0410-9d82-d95c44d28aba>
Thu, 26 Nov 2009 02:22:31 +0000 (02:22 +0000)
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
app/models/app_model.php
app/models/user.php
app/plugins/cakeplus/models/behaviors/add_validation_rule.php [new file with mode: 0644]
app/views/users/admin_add.ctp

index 7c30c74..80e6c27 100644 (file)
@@ -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'));
+                               }
                        }
+
                }
        }
 
index d313b85..8e9caa8 100644 (file)
@@ -1,12 +1,15 @@
 <?php
 class AppModel extends Model {
-       var $actsAs = array('Cakeplus.ValidationErrorI18n');
+       var $actsAs = array(
+               'Cakeplus.ValidationErrorI18n',
+       );
 
        function beforeValidate(){
                $error_messages = array(
                        'notEmpty'      => __('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 {
        }
 
 }
-
index 8dd0a10..34936e7 100644 (file)
@@ -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 (file)
index 0000000..35861af
--- /dev/null
@@ -0,0 +1,194 @@
+<?php
+
+/**
+ * 独自のバリデーションルールを追加するbehavior プラグイン
+ * 内部文字コードはデフォルトUTF-8(オプションで変更可能)
+ * Behavior of some validation rules.
+ * Internal encoding is UTF-8, can change it with parameter.
+ *
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright 2009, Yasushi Ichikawa. (http://d.hatena.ne.jp/cakephper/)
+ * @link          http://d.hatena.ne.jp/cakephper/
+ * @package       cakeplus
+ * @subpackage    cakeplus
+ * @version       0.03
+ * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
+ *
+ *
+ * =====利用方法=====
+ * 各モデルファイルで、下記のように使う。app_modelにactsAsで指定しても可
+ * In each model class or app_model, write code as follow.
+ *             var $actsAs = array('Cakeplus.AddValidationRule');
+ *
+ * 内部文字コードを変更したい場合は、オプションで変更可能(デフォルトはUTF-8)
+ * If you want to change encoding(UTF-8), write code as follow.
+ *             var $actsAs = array('Cakeplus.AddValidationRule' => 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 );
+
+       }
+
+}
+
+?>
index 3b828c6..b1c8803 100644 (file)
@@ -3,8 +3,25 @@
        <fieldset>
                <legend><?php __('Add User');?></legend>
        <?php
-               echo $form->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',
+                       )
+               );
        ?>
        </fieldset>
 <?php echo $form->end('Submit');?>