partial validation on a form

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
  • email
  • 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,

		)

	);



You can use scenarios (default or custom ones). Change your validation rule for username and password to


array('username, password', 'required', 'on'=>'insert')

That means that the rule won’t be checked on ‘update’ scenario, only on ‘insert’ one.

Edit: the validation rule above is an example of course.

Thanks! I’ll try and let you know if I have a problem with your solution.

Have a good day!

You seem to be making a new User rather than using find() to grab the existing one and then saving it. It should automatically be an update statement if you find() it and modify that. So yes, change the controller so it doesn’t just add another user but updates the current one instead.

I use scenarios as mentioned above. To display the information I use a disabled textfield.

By find you mean using something like this?




$user = User::model()->findByPk($userID);



But it would be weird to use a disabled textfield for password!