Load Specific Form Fields Upon Radio Button Selection

I am a fairly new user of Yii… Have been using it for less than 2 months…

I am trying to do a registration form that upon loading, it only shows 2 radio buttons(say choice 1 is Person, choice 2 is Organization), when the user selects a choice i want to dynamically display the fields corresponding to his selection using ajax.

Note: I am using the yii-user extension, and I am intending to use the same table to store both Person & Organization users, since they only differ in 3 attributes.

I have been stuck with this problem for 2 days and am not able to solve it…

This is a snippet of my code:

registration.php (view)




<?php $this->pageTitle=Yii::app()->name . ' - '.UserModule::t("Registration");

$this->breadcrumbs=array(

	UserModule::t("Registration"),

);

?>


<h1><?php echo UserModule::t("Registration"); ?></h1>


<?php if(Yii::app()->user->hasFlash('registration')): ?>

<div class="success">

<?php echo Yii::app()->user->getFlash('registration'); ?>

</div>

<?php else: ?>


<div class="form">

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

	'id'=>'registration-form',

	'enableAjaxValidation'=>true,

	'disableAjaxValidationAttributes'=>array('RegistrationForm_verifyCode'),

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

	'htmlOptions' => array('enctype'=>'multipart/form-data'),

)); ?>


	<p class="note"><?php echo UserModule::t('Fields with <span class="required">*</span> are required.'); ?></p>

	

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

	

    <div class="row">

    <?php echo $form->radioButtonList($model, 'profile_type', array(0=>'Person',1=>'Company'),array('separator'=>'', 'labelOptions'=>array('style'=>'display:inline'), 'onclick' =>CHtml::ajax(array("type"=>"GET", "url"=>array("registration/add"), "data"=>array('test'=>'js:this.value'), "update"=>"#test")))); ?>


	</div>



RegistrationController.php




         public function actionAdd() { 

		if(Yii::app()->request->isAjaxRequest){  

			$this->renderPartial("/user/test",array('model'=>$model),false,true);   

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

                }

	}



test.php




<?php

$val = $_GET['test'];


if($val == 1){

  echo "yes";

//nothing added yet

}

else{

	echo "no";

	?>

  <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($model,'email'); ?>

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

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

	</div>

    

	<div class="row">

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

	<?php echo $form->passwordField($model,'password'); ?>

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

	<p class="hint">

	<?php echo UserModule::t("Minimal password length 4 symbols."); ?>

	</p>

	</div>

	

	<div class="row">

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

	<?php echo $form->passwordField($model,'verifyPassword'); ?>

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

	</div>

<?php

	

}

?>



The erros am recieving when I select Person (choice 0) is the following:

Fatal error: Call to a member function labelEx() on a non-object in test.php

Any help is much appreciated!

Ajax seems overkill for this. Just render all form elements, but apply a class of either "person" or "organisation" to the extra ones and initially hide them. Then you can use some simple jQuery to show or hide them based on the selected option.

@Keith,

Thank u for your reply. I am now actually doing it the way u suggested, because i had no luck at all doing it with ajax.

The reason i want to use ajax, is that i want the view to render exactly the needed fields as per the selected option, and i didn’t like the idea of hiding fields at first, but now i have no option.

Thanks again.