Prevent Prefilling Form Fields

I have a form using 2 models, $model and $profile. For some reason the $model values always remain empty and the $profile fields are always prefilled with the data currently saved in the database. What determines if this field is filled or not?

The thing that really matters here is that when a user fails form validation, the fields are reset back to the original every time. This I have to fix.

Ideally what I am trying to do here is load the update form with the current values in the database, when the user changes the values and fails validation I want their failed values to remain in the form.

I discovered this when trying to figure out how to validate both models at the same time but it currently only validates one class at a time.

Form:




<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'welcome-form',

	'enableAjaxValidation'=>false,

)); ?>


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


	<div class="row">

		<?php echo $form->labelEx($model,'username'); ?>

		<?php echo $form->textField($model,'username'); ?>

		<?php echo $form->error($model,'username'); ?>

	</div>

	

	<div class="row">

		<?php echo $form->labelEx($profile,'firstname'); ?>

		<?php echo $form->textField($profile,'firstname'); ?>

		<?php echo $form->error($profile,'firstname'); ?>

	</div>

	

	<div class="row">

		<?php echo $form->labelEx($profile,'lastname'); ?>

		<?php echo $form->textField($profile,'lastname'); ?>

		<?php echo $form->error($profile,'lastname'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton('Submit'); ?>

	</div>


<?php $this->endWidget(); ?>




Controller:




public function actionWelcome()

{

  $model = new InprocessingForm;

  $profile = Profile::model()->find(array(

    'condition'=>'user_id=:user_id', 

    'params'=>array(':user_id'=>$user->id)

  ));


  if(isset($_POST['ajax']) && $_POST['ajax']==='welcome-form')

  {

    echo CActiveForm::validate(array($model, $profile));

    Yii::app()->end();

  }


  if(isset($_POST['WelcomeForm'])&& isset($_POST['Profile']))

  {

    $model->attributes=$_POST['WelcomeForm'];

    if($model->validate() && profile->validate())

    {

	  $profile->attributes=$_POST['Profile'];

	  if($profile->save())

	  {

	      Yii::app()->user->setFlash('invitation','Profile updated.');

	      Yii::app()->controller->redirect(array('/post'));

	  }

    }

    $this->render('welcome',array('model'=>$model, 'profile'=>$profile));

  }




I have added a else condition, this may help you…


public function actionWelcome()

{

  $model = new InprocessingForm;

  $user = User::model()->findByPk(Yii::app()->user->id);

  $role = UserRole::model()->find(array(

    'condition'=>'user_id=:user_id', 

    'params'=>array(':user_id'=>$user->id)

  ));

  $profile = Profile::model()->find(array(

    'condition'=>'user_id=:user_id', 

    'params'=>array(':user_id'=>$user->id)

  ));


  if(isset($_POST['ajax']) && $_POST['ajax']==='inprocessing-form-welcome-form')

  {

    echo CActiveForm::validate(array($model, $profile));

    Yii::app()->end();

  }


  if(isset($_POST['InprocessingForm'])&& isset($_POST['Profile']))

  {

    $model->attributes=$user->attributes=$_POST['InprocessingForm'];

    if($model->validate())

    {

      $user->password=$user::hashPassword($user->password);

        if($user->save()) 

	{

	  $profile->attributes=$_POST['Profile'];

	  if($profile->save())

	  {

	    $role->role_id=2;

	    $role->user_id=$user->id;

	    if($role->save())

	    {

	      Yii::app()->user->setFlash('invitation','Profile and password updated.');

	      Yii::app()->controller->redirect(array('/post'));

	    }

	  }

	}

      }

	  else{

		   $model = new InprocessingForm;

	  }

    }

    $this->render('welcome',array('model'=>$model, 'profile'=>$profile));

  }

I tried adding the else. All it seems to do is load a new form every time validation fails. This prevents any validation message at all.

So I figured this out. I needed to move


$profile->attributes=$_POST['Profile'];



to before the


if($model->validate())



and to validate both models at the same time I replaced this


if($model->validate() && profile->validate())

with this


			

$valid=$model->validate();

$valid=$profile->validate() && $valid;

if($valid)

   // do stuff