パスワード再入力チェック

CookBookを参考に、確認用パスワードとの照合用バリデーション定義を作成しようとしましたが、うまく行かず
http://book.cakephp.org/ja/view/150/Custom-Validation-Rules

結局また、Cakephperさんのadd_validation_rule.phpに頼ってしまいました ^^;
2009-06-03


文字数チェックはしたいので、要素名はpassword1/2。
暗号化なしでの照合としました。

plugins/cakeplus/models/behaviors/add_validation_rule.phpを設置して、user.phpで読込み追加。

user.phpでpassword2(confirm用入力)側にcompare2fieldsのvalidateを追加。

app_model.phpにcompare2fields用のメッセージ追加。文言に迷いましたが「上と同じ値を入力」の表現としました。*1

user_controller.phpに、validate後passwordに暗号化したpassword1を代入して保存を追加。


ソース改修分

Index: models/user.php
===================================================================
--- models/user.php     (revision 134)
+++ models/user.php     (working copy)
@@ -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 @@
                        ),
                ),
                'password2' => array(
+                       'compare2fields' => array(
+                               'rule' => array('compare2fields', 'password1', false),
+                       ),
                        'alphaNumeric' => array(
                                'rule' => 'alphaNumeric',
                        ),

Index: models/app_model.php
===================================================================
--- models/app_model.php        (revision 134)
+++ models/app_model.php        (working copy)
@@ -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),
+                       'compare2fields' => __('Please input same as above.', true),
                );
                $this->setErrorMessageI18n($error_messages, false);

-
Index: controllers/users_controller.php
===================================================================
--- controllers/users_controller.php    (revision 131)
+++ controllers/users_controller.php    (working copy)
@@ -33,14 +33,24 @@
        }

        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'));
+                               }
                        }
+
                }
        }


独自に作ったvalidate定義でうまく行かなかったのは、$dataを引数にしていたから。
add_validation_rule.phpでは$modelオブジェクトを参照引数としています。
 なるほどなあ。。

	function compare2fields( &$model, $wordvalue , $compare_filed , $auth = false ){

*1:エラーメッセージに代入可能な変数はcompare2fieldsに渡すパラメータのみのため。 「password1と同じ値を〜」なら表示できるんですが・・・