2013-08-05 34 views
0

我想在特殊頁面更改密碼。在yii應用程序中更改密碼頁的問題

當我點擊「更改密碼」,我必須去密碼視圖。

這是從那裏我需要去

<?php 
/* @var $this UserController */ 
/* @var $model User */ 

$this->menu=array(
    array('label'=>'User List', 'url'=>array('index')), 
    array('label'=>'Create User', 'url'=>array('create')), 
    array('label'=>'View User', 'url'=>array('view', 'id'=>$model->id)), 
    array('label'=>'Change password', 'url'=>array('password', 'id'=>$model->id)), 
); 
?> 

<h1>Change user <?php echo $model->id; ?></h1> 

<?php echo $this->renderPartial('_form', array('model'=>$model)); ?> 

用於更新視圖代碼這是我password.php查看文件

<?php 
$this->menu=array(
    array('label'=>'List user', 'url'=>array('index')), 
    array('label'=>'Create user', 'url'=>array('create')), 
    array('label'=>'View user', 'url'=>array('view', 'id'=>$model->id)), 
    array('label'=>'Change user', 'url'=>array('update', 'id'=>$model->id)), 
); 
?> 

<?php 
CHtml::beginForm(); 
CHtml::textField('password'); 
CHtml::submitButton('Change'); 
CHtml::endForm(); 
?> 

UserController中有動作actionPassword()

public function actionPassword($id) { 
     $model=$this->loadModel($id); 
     $model->password=$_POST['password']; 
     if($model->save()) 
      $this->redirect(array('view','id'=>$model->id)); 
     $this->render('password'); 
} 

在例子中,我使用這段代碼工作正常。但我有錯誤 http://pastebin.com/XbCwfhRT

所有文件和文件夾都具有root權限。我做錯了什麼?一個月前相同的代碼工作正常,我不明白什麼知道出錯。

+0

'$ model'被正確創建了嗎? '$ id'中的值是否有效?如何'var_dump($模型);'確保。將密碼保存爲明文也是個不錯的主意。 – Pitchinnate

+0

我在沒有它的情況下加入密碼功能。 ID有效。我有正確的$模型和$ ID。在URL中我有正確的鏈接。但頁面不起作用。 –

回答

0

您忘了檢查$_POST是否設置爲動作加載時(提交前)$_POST['password']不存在。

if(isset($_POST['password'])) { 
    $model->password=$_POST['password']; 
    if($model->save()) 
     $this->redirect(array('view','id'=>$model->id)); 
} 
+0

謝謝。但現在我有另一個麻煩。如果我離開$ this-> render('password');我有錯誤http://pastebin.com/GADp9bFV。如果我設置$ this-> render('password',array('model'=> $ this-> loadModel($ id)));我有頁面沒有形式 –

+0

ohhh。最新的麻煩太可愛了。我忘了在「CHtml」之前添加「echo」。現在一切正常。 –

相關問題