php - Yii Change Password for admin -
i created change password functionality change admin password.i used this toutorial.
face problem in $model->validate().
can me??
controller
public function actionindex() { $id = 1; $model = user::model()->findbyattributes(array('usertype' => $id)); $model->setscenario('changepwd'); if (isset($_post['user'])) { $model->attributes = $_post['user']; if ($model->validate()) { $model->password = md5($model->new_password); if ($model->save()) { yii::app()->user->setflash('success', "password changed successfully!"); } } else { yii::app()->user->setflash('error', "change password failed!"); } } $this->render('index', array('model' => $model)); }
model
class user extends cactiverecord { public $old_password; public $new_password; public $repeat_password; /** * @return string associated database table name */ public function tablename() { return '{{user}}'; } /** * @return array validation rules model attributes. */ public function rules() { // note: should define rules attributes // receive user inputs. return array( array('usertype, firstname, lastname, email, password, mobile, gender, dob, country, area, city, address, street, housenumber, extradirection, createdon', 'required'), array('usertype, country, area', 'numerical', 'integeronly'=>true), array('firstname, lastname, email, mobile, dob, city, street, housenumber', 'length', 'max'=>155), array('password', 'length', 'max'=>225), array('gender', 'length', 'max'=>6), array('status', 'length', 'max'=>1), array('updatedon', 'safe'), // following rule used search(). // @todo please remove attributes should not searched. array('id, usertype, firstname, lastname, email, password, mobile, gender, dob, country, area, city, address, street, housenumber, extradirection, createdon, updatedon, status', 'safe', 'on'=>'search'), array('old_password, new_password, repeat_password', 'required', 'on' => 'changepwd'), array('old_password', 'findpasswords', 'on' => 'changepwd'), array('repeat_password', 'compare', 'compareattribute'=>'new_password', 'on'=>'changepwd'), ); } public function findpasswords($attribute, $params) { $user = user::model()->findbypk(yii::app()->user->id); //echo '<pre>';print_r($user);echo '</pre>'; if ($user->password != md5($this->old_password)) $this->adderror($attribute, 'old password incorrect.'); }
form
<div class="login_con_new"> <div class="form"> <?php $form=$this->beginwidget('cactiveform', array( 'id'=>'change-password-form', //'action' => yii::app()->createurl('login/authenticate'), // 'enableajaxvalidation' => false, 'enableclientvalidation' => true, 'clientoptions' => array('validateonsubmit' => true,), 'htmloptions' => array( 'class' => 'form', ) )); ?> <div class="col-sm-6"> <h2 class="title">change password</h2> <?php foreach(yii::app()->user->getflashes() $key => $message) { echo '<div class="flash-' . $key . '">' . $message . "</div>\n"; } ?> <div class="form-group"> <?php echo $form->labelex($model,'current_password'); ?> <?php echo $form->passwordfield($model,'old_password',array('class'=>'form-control login-field','size'=>60,'maxlength'=>222)); ?> <?php echo $form->error($model,'old_password'); ?> </div> <div class="form-group"> <?php echo $form->labelex($model,'new_password'); ?> <?php echo $form->passwordfield($model,'new_password',array('class'=>'form-control login-field','size'=>60,'maxlength'=>222)); ?> <?php echo $form->error($model,'new_password'); ?> </div> <div class="form-group"> <?php echo $form->labelex($model,'repeat_password'); ?> <?php echo $form->passwordfield($model,'repeat_password',array('class'=>'form-control login-field','size'=>60,'maxlength'=>222)); ?> <?php echo $form->error($model,'repeat_password'); ?> </div> <div class="form-group"> <div class="col-lg-4" style="padding-left: 0px;"> <?php echo chtml::submitbutton('change',array('class' => 'btn btn-success','style'=>'color:white')); ?></div> </div> </div> <?php $this->endwidget(); ?> </div> </div>
the $valid returns me false , entering else part.
i think in line $model = user::model()->findbyattributes(array('usertype' => $id));
did mistake usertype. user id.
Comments
Post a Comment