Hello everyone,
I have a simple page with a form on it where users can update their personal details (i.e. name, email, etc).
I am trying to figure out the best practice to do this. The problem I have is that I don’t want to allow users to change their username. So there is no text field on the page for the users to modify their usernames. However, when I post the form, Yii complains that the username cannot be empty. I know I can do my own verification. But I prefer to take advantage of Yii’s capabilities if possible. Thus I need to know if it is possible to apply validation on the said fields.
The User model has the following fields:
- username
- firstname
- lastname
- password
and in the view I just have fields for firstname, lastname, and email. So I don’t want the password and username fields be validated (they are not supposed to be empty).
Here is my view file:
<h3><?php echo $user->username;?></h3>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm'); ?>
<?php echo $form->errorSummary($user); ?>
<div class="row">
<?php echo $form->label($user,'firstname'); ?>
<?php echo $form->textField($user,'firstname') ?>
</div>
<div class="row">
<?php echo $form->label($user,'lastname'); ?>
<?php echo $form->textField($user,'lastname') ?>
</div>
<div class="row">
<?php echo $form->label($user,'email'); ?>
<?php echo $form->textField($user,'email') ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('Update'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I know my controller is no good.But I am posting it nonetheless:
$user=new User;
if(isset($_POST['User']))
{
$user->attributes=$_POST['User'];
$valid=$user->validate();
if($valid)
{
// use false parameter to disable validation
$user->save(false);
}
} else {
$userID = Yii::app()->user->id;
$user = User::model()->findByPk($userID);
}
$this->render('profile', array(
'user'=>$user,
)
);