0

ユーザーがパスワードを変更できるように、UsersController.php ファイルにメソッドを作成しました。私のメソッドphpコードは次のとおりです。

public function changepass() {
    $this->User->id = $this->Auth->user('id');
    if($this->User->exists()) {         
        $new_pass = $this->request->data['User']['newpass'];
        $repeat_pass = $this->request->data['User']['newrepeat'];
        if($new_pass == $repeat_pass) {
            $this->User->saveField('password',$new_pass);
            $this->Session->setFlash(__('Updated successfully'));
            $this->redirect(array('controller' => 'users','action' => 'dashboard'));
        } else {
            $this->Session->setFlash(__('Passwords did not match'));
            $this->redirect(array('controller' => 'users','action' => 'changepass'));
        }

    }
}

私のchangepass.ctpビューファイルは次のとおりです。

<?php 
    echo $this->Form->create();

    echo $this->Form->input('newpass',array('type'=>'text','label'=>array('text'=>'Enter new password'))); 

    echo $this->Form->input('newrepeat',array('type'=>'text','label'=>array('text'=>'Confirm new password'))); 
?>

    <button type="submit">Save</button>

<?php echo $this->Form->end(); ?>

users/changepass の下を参照しようとすると、未定義のインデックスに関する 2 つのエラーが返されます。

Notice (8): Undefined index: User [APP/Controller/UsersController.php, line 108]

Notice (8): Undefined index: User [APP/Controller/UsersController.php, line 109]

私のコードのこの部分を指しています:

    $new_pass = $this->request->data['User']['newpass'];
    $repeat_pass = $this->request->data['User']['newrepeat'];

また、データベースのユーザーパスワードを(すぐに)空白に変更します。

何が問題なのか、まったくわかりません。この時点でお役に立てれば幸いです。

前もって感謝します。

4

1 に答える 1

2

コントローラーでのフォーム処理に関する重要な部分を 1 つ忘れていました。POST を確認してください。

if ($this->request->is('post')) {
    // only then try to access $this->request->data['User'] content!
}

投稿 (取得) ではない場合は、特に何も保存せずに、フォームを表示するだけです。

ヒント: ベイクされたコード (ケーキのベイクを使用) を見ると、それがどのように適切に行われているかがわかります。

于 2013-07-29T10:02:50.167 に答える