Yii Framework Forum: Change Password - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Change Password Change Password Rate Topic: -----

#1 User is offline   er.amitsingla86 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 03-July 13

Posted 24 July 2013 - 08:47 AM

i am new to YII framework. i am writing a simple method to change password, i am want to validate the controls like if blank then show the error message or if password and re-type password does not match then show error. i have used the below code to do this :

Here is my view file

<?php
/* @var $this SiteController */
/* @var $model LoginForm */
/* @var $form CActiveForm */

$this->pageTitle=Yii::app()->name . ' - Change Password';
$this->breadcrumbs=array(
'Change Password',
);
?>

<h1>Change Password</h1>


<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'users-form',
'enableAjaxValidation'=>true,
)); ?>



<?php
$flashMessages = Yii::app()->user->getFlashes();
if ($flashMessages) {
echo '<ul class="flashes">';
foreach($flashMessages as $key => $message) {
echo '<li><div class="flash-' . $key . '">' . $message . "</div></li>\n";
}
echo '</ul>';
}
?>

<div class="row">
<?php echo $form->labelEx($model,'currentPassword'); ?>
<?php echo $form->passwordField($model,'currentPassword',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'currentPassword'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'newPassword'); ?>
<?php echo $form->passwordField($model,'newPassword',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'newPassword'); ?>
</div>


<?php /*?><div class="row">
<?php echo $form->labelEx($model,'newPassword'); ?>
<?php

$this->widget('ext.EStrongPassword.EStrongPassword',
array('form'=>$form, 'model'=>$model, 'attribute'=>'newPassword'));?>
<?php echo $form->error($model,'newPassword'); ?>
</div>
<?php */?>
<div class="row">
<?php echo $form->labelEx($model,'newPasswordRepeat'); ?>
<?php echo $form->passwordField($model,'newPasswordRepeat',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($model,'newPasswordRepeat'); ?>
</div>

<div class="row buttons">
<?php echo CHtml::submitButton('Change Password'); ?>
</div>

<?php $this->endWidget(); ?>
</div><!-- form -->

Here is my Model file :-

<?php

/**
* This is the model class for table "tbl_users_master".
*
* The followings are the available columns in table 'tbl_users_master':
* @property integer $user_id
* @property string $first_name
* @property string $last_name
* @property string $employee_code
* @property string $username
* @property string $password
* @property string $role
* @property string $joining_date
* @property string $pending_regular_leave
* @property string $pending_medical_leave
* @property string $allocated_regular_leave
* @property string $allocated_medical_leave
* @property integer $is_active
*/
class Users extends AttendanceActiveRecord
{

public $currentPassword;
public $newPassword;
public $newPasswordRepeat;
public $verifyCode;

public static function model($className=__CLASS__)
{
return parent::model($className);
}

public function tableName()
{
return 'tbl_users_master';
}

public function getJoiningDate()
{
return date("d-m-Y", strtotime($this->joining_date));
}


public function rules()
{
return array(
array('is_active', 'numerical', 'integerOnly'=>true),
array('first_name, joining_date,last_name, employee_code, username, password, role', 'required'),
array('employee_code', 'numerical', 'integerOnly'=>true),
array('username','email'),
array('username','valid_username','on'=>array('create')),

//array('username', 'contraints', 'readOnly'=>true, 'on'=>'update'),

array('currentPassword, newPassword, newPasswordRepeat', 'required','on'=>array('change')),
//array('newPassword', 'length', 'min' => 6, 'max'=>20, 'message'=>Yii::t("translation", "{attribute} is too short.")),
//array('newPassword','ext.SPasswordValidator.SPasswordValidator', 'preset' => 'strong', 'max' => 41),
array('newPassword', 'compare', 'compareAttribute'=>'newPasswordRepeat','on'=>array('change')),
array('currentPassword', 'equalPasswords','on'=>array('change')),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>array('forgotPassword')),
array('joining_date', 'safe'),
array('user_id, first_name, last_name, employee_code, username, password, role, joining_date, pending_regular_leave, pending_medical_leave, allocated_regular_leave, allocated_medical_leave, is_active', 'safe', 'on'=>'search'),
);
}


public function equalPasswords($attribute, $params)
{
$user = Users::findByPk(Yii::app()->user->id);
if ($user->password != md5($this->currentPassword))
$this->addError($attribute, 'Old password is incorrect.');
}

public function valid_username($attribute,$params)
{
if(Users::model()->findByattributes(array('username'=>$this->username)))
{
$this->addError('username',"This email already exists");
}
return true;
}


public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'leaves' => array(self::HAS_MANY, 'Leaves', 'leave_id'),
'creator' => array(self::BELONGS_TO, 'Users', 'user_id'),
'updator' => array(self::BELONGS_TO, 'Users', 'user_id'),
);
}

public function getFullName()
{
return $this->first_name.' '.$this->last_name;
}


public function setDateFormat()
{
$this->created_date=date("d-m-Y h:i a", strtotime($this->created_date));
$this->modified_date=date("d-m-Y h:i a", strtotime($this->modified_date));
$this->joining_date=date("d-m-Y", strtotime($this->joining_date));
}


public function RandomString($length)
{
if(!is_int($length)||$length<1)
{
trigger_error('Invalid length for random string');
exit();
}
$chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

$randstring='';
$str = '';
$maxvalue=strlen($chars)-1;

for($i=0;$i<$length;$i++) $randstring.=substr($chars,rand(0,$maxvalue),1);
$str = strtoupper(substr(md5($randstring),1,$length));
return $str;
}

public function attributeLabels()
{
return array(
'user_id' => 'User',
'first_name' => 'First Name',
'last_name' => 'Last Name',
'employee_code' => 'Employee Code',
'username' => 'Email',
'password' => 'Password',
'role' => 'Role',
'joining_date' => 'Joining Date',
'pending_regular_leave' => 'Pending Regular Leave',
'pending_medical_leave' => 'Pending Medical Leave',
'allocated_regular_leave' => 'Allocated Regular Leave',
'allocated_medical_leave' => 'Allocated Medical Leave',
'is_active' => 'Is Active',
'currentPassword' => 'Old Password',
'newPassword' => 'New Password',
'newPasswordRepeat' => 'Confirm Password',
'verifyCode'=>'Verification Code',
'created_date'=> 'Created On',
'modified_date'=>'Modified On',
'created_ip'=>"Created by IP",
'modified_ip'=>"Modified by IP",
);
}

public function AddRegularLeaves($hours,$isPermitted)
{
if($isPermitted==1)
{
$this->allocated_regular_leave = $this->allocated_regular_leave + $hours;
$this->update();
}
}

public function SubtractRegularLeaves($hours,$isPermitted)
{
if($isPermitted==1)
{
$this->allocated_regular_leave = $this->allocated_regular_leave - $hours;
$this->update();
}
}

public function AddMedicalLeaves($hours,$isPermitted)
{
if($isPermitted==1)
{
$this->allocated_medical_leave = $this->allocated_medical_leave + $hours;
$this->update();
}
}

public function SubtractMedicalLeaves($hours,$isPermitted)
{
if($isPermitted==1)
{
$this->allocated_medical_leave = $this->allocated_medical_leave - $hours;
$this->update();
}
}


public function searchWithRelated() {
$criteria = new CDbCriteria;

$criteria->together = true;
$criteria->with = array('user');
$criteria->compare('leaves.user_id',$this->user_id, true);

return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 1,
),
));
}




public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('user_id',$this->user_id);

$criteria->compare('first_name',$this->first_name,true);
$criteria->compare('last_name',$this->last_name,true);
$criteria->compare('employee_code',$this->employee_code,true);
$criteria->compare('username',$this->username,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('role',$this->role,true);
$criteria->compare('joining_date',$this->joining_date,true);
$criteria->compare('pending_regular_leave',$this->pending_regular_leave,true);
$criteria->compare('pending_medical_leave',$this->pending_medical_leave,true);
$criteria->compare('allocated_regular_leave',$this->allocated_regular_leave,true);
$criteria->compare('allocated_medical_leave',$this->allocated_medical_leave,true);
$criteria->compare('is_active',$this->is_active);

return new CActiveDataProvider($this, array(
'pagination' => array('pageSize' => 2),
'criteria'=>$criteria,
));
}
}

Here is my Controller file :-

public function actionChange()
{
$model=new Users;
$model->scenario='change';
if (isset($_POST['Users'])) {
$model->setAttributes($_POST['Users']);

if($model->validate())
{
print "true";
}
else{

print "false";
}

if($model->validate())
{
$pass = md5($_POST['Users']['newPassword']);
$userModel = Users::model()->findByPk(Yii::app()->user->id);
$userModel->password = $pass;
$data = $userModel->update();
}

if(isset($data))
{
Yii::app()->user->setFlash('success',"Password changed successfully!");
$this->redirect(array('Users/change'), true);
}
}


$this->render('change_password', array('model'=>$model,true));
}

But **$model->validate()** always returns false to me either i fill the fields or not.

please help me.
0

#2 User is offline   Travis Stroud 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 29-August 12

Posted 24 July 2013 - 11:22 AM

It's hard to follow your code without using the forums code tags, but are you seeing the error summary in the view when the validation fails? The code

<?php echo $form->errorSummary($model); ?>

inside your view would tell what didn't validate. From there you can narrow down what the problem is.
0

#3 User is offline   Rohit Suthar 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 153
  • Joined: 12-May 14
  • Location:Mumbai, India

Posted 02 July 2014 - 07:34 AM

Follow this wiki to find best solution -

Change Password with TbActiveForm
Thanks & regards,

ROHIT SUTHAR,
Mumbai(India)

Follow me - @rohisuthar

www.yiiframework.us



Don't be shellfish to click +1 button
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users