I am trying to update user information. When I update any field, password field automatically get changed.
i.e. I have created separate form consisting some fields - address, city which are not required. When I update this form, password get changed.
Here is my code :
<div class="form form-custom">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'account-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'first_name'); ?>
<?php echo $form->textField($model,'first_name',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'first_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'last_name'); ?>
<?php echo $form->textField($model,'last_name',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'last_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'street_1'); ?>
<?php echo $form->textField($model,'street_1',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'street_1'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'street_2'); ?>
<?php echo $form->textField($model,'street_2',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'street_2'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'city'); ?>
<?php echo $form->textField($model,'city',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'city'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'state'); ?>
<?php echo $form->textField($model,'state',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'state'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'country'); ?>
<?php echo $form->textField($model,'country',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'country'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'zip'); ?>
<?php echo $form->textField($model,'zip',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'zip'); ?>
</div>
<div class="formfieldarea">
<div class="form-text"></div>
<div class="form-field">
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Register' : 'Save'); ?>
</div>
<div class="af-element buttonContainer">
<input type="reset" name="reset" value="Clear" tabindex="501" style=" width: 40%; height: 29px; padding: 3px 5px 2px 5px; font-size: 16px; border-radius: 3px; border: none; background-color: #2E2E2B; color: #fff; cursor: pointer; -webkit-appearance: none;" class="submit">
<div class="af-clear"></div>
</div>
</div>
</div>
<?php $this->endWidget(); ?>
controller:
public function actionUpdateAccount(){
$model=$this->loadModel(Yii::app()->user->id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if($model->save())
$this->redirect(array('user/profile'));
}
$this->renderPartial('updateAccount',array(
'model'=>$model,false,true
));
}
Can anyone tell me why it is happening and how to fix it ?
This is a possible duplicate of Error causes password change while updating new data in Yii
Using save()
with no parameters saves every attribute by default. You're probably using a beforeSave() method in your model to hash up the password, and this is getting called every time you use save()
,
But you can specify which attributes to save:
$model->save(true, array('attributes', 'you', 'want', 'to', 'save'));
// true to use validation, the array to specify which attributes to save