2013-08-05 35 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”。现在一切正常。 –

相关问题