I have a form for editing a user record. Its updates the record for the first time but when i press the update button again, it shows me an empty screen and it unsets the picture as well. I cannot figure out what I am doign wrong Below is my code
public function actionEditProfile($affusername = NULL) {
$this->layout = 'layout_home';
if ($this->VarifyUser($affusername) && Yii::app()->user->id) {
$model = new Users('edit'); //apply rules if user comes directly to signup page
// uncomment the following code to enable ajax-based validation
if (isset($_POST['ajax']) && $_POST['ajax'] === 'editprofile-form') {
CActiveForm::validate($model);
Yii::app()->end();
}
if (isset($_POST['Users'])) {
$the_image = CUploadedFile::getInstance($model, 'image');
if (is_object($the_image) && get_class($the_image) === 'CUploadedFile') {
$model->image = $the_image;
}
if (is_object($the_image)) {
$file_name = Yii::app()->user->id . '.' . $model->image->extensionName;
$move = $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/themes/myproject/images/users/' . $file_name);
$_POST['Users']['image'] = $file_name;
}
$model->attributes = $_POST['Users'];
if ($model->validate()) {
// form inputs are valid, do something here
if ($model->updateByPk(Yii::app()->user->id, $_POST['Users'])) {
//$this->setFlashSuccess("Unable to register user, please try again");
$this->setFlashSuccess('Updated successfully, now redirecting.');
//$this->redirect($this->base_url);
} else {
$this->setFlashError("Unable to update user, please try again");
return false;
}
} else {
print_r($model->getErrors());
echo 'validation fail';
exit;
}
}
$user_data = Users::model()->getUsersByUserName($affusername); //getting user information
//print_r($user_data);
//$model=new Users('nonfb-create'); //apply rules if user comes directly to signup page
$this->render('editprofile', array('model' => $model, 'data' => $user_data,));
} else {
$this->redirect($this->base_url);
}
}
My view is
<div class="adminform_wrapp">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'editprofile-form',
'enableAjaxValidation' => false,
'clientOptions' => array(
'validateOnSubmit' => true,
),
'enableClientValidation' => true,
'focus' => array($model, 'first_name'),
'htmlOptions' => array(
'enctype' => 'multipart/form-data'
)
));
echo $form->errorSummary($model);
?>
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_First_Name">First Name: <span class="required">*</span></label>
<?php echo $form->textField($model, 'first_name', array('value' => $data['first_name'])); ?>
<?php $form->error($model, 'first_name'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Last_Name">Last Name: <span class="required">*</span></label>
<?php echo $form->textField($model, 'last_name', array('value' => $data['last_name'])); ?>
<?php $form->error($model, 'last_name'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Email">Email: <span class="required">*</span></label>
<?php echo $form->textField($model, 'email', array('value' => $data['email'])); ?>
<?php $form->error($model, 'email'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Phone_No">Phone No: <span class="required">*</span></label>
<?php echo $form->textField($model, 'phone_no', array('value' => $data['phone_no'])); ?>
<?php $form->error($model, 'phone_no'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label>Username:</label>
<span class="profile-username"><?php echo $data['username']; ?></span> </div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label>Edit Profile picture:</label>
<span class="image-placeholder">
<img src="<?php echo $this->theme_baseurl . '/images/users/' . $data['image']; ?>" style="width:96px; height:96px;"/>
</span>
<div id='file_browse_wrapper'>
<?php
//echo $form->labelEx($model, 'image');
echo $form->fileField($model, 'image', array('id' => 'file_browse'));
echo $form->error($model, 'image');
?>
</div>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<?php echo $form->labelEx($model, 'Address', array('class' => 'fieldname')); ?>
<?php echo $form->textField($model, 'address', array('value' => $data['address'])); ?>
<?php $form->error($model, 'address'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Country">Country: <span class="required">*</span></label>
<select class="error" onchange="print_state('Users_state',this.selectedIndex);" id="Users_country" name ="Users[country]"></select>
<?php $form->error($model, 'country'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_State">State: <span class="required">*</span></label>
<select name ="Users[state]" id ="Users_state"></select>
<script language="javascript">print_state("Users_state", '', "<?php echo $data['state'] ?>");</script>
<?php $form->error($model, 'state'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_City">City: <span class="required">*</span></label>
<?php echo $form->textField($model, 'city', array('value' => $data['city'])); ?>
<?php $form->error($model, 'city'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<label class="fieldname required" for="Users_Zipcode">Zipcode: <span class="required">*</span></label>
<?php echo $form->textField($model, 'zipcode', array('value' => $data['zipcode'])); ?>
<?php $form->error($model, 'zipcode'); ?>
</div>
<!--adminform_row-->
<!--adminform_row-->
<div class="adminform_row">
<input type="submit" class="adminupdate_btn" value="Update">
<input type="reset" class="admincancel_btn" value="Cancel">
</div>
<!--adminform_row-->
<?php $this->endWidget(); ?>
</div>