Generate Dynamic Form

I want to create dynamic form which contains textbox to store socialmedia_url depending upon the number of values in the lookup table.

Suppose i have 4 values in lookup table like facebook,twitter,youtube and googlplus so it should create a form with 4 rows. I have created a socialmedia class which contains attribute (url) and extending to CFormModel.

How can i create multiple rows in a form dynamically.????

Also how to create model object.

The form should look something like this…

$form->textField($urlmodel,‘url[0]’,array(‘placeholder’=>HkLookup::item(‘SOCIALMEDIA_ACCOUNTS’,1)));

$form->textField($urlmodel,‘url[1]’,array(‘placeholder’=>HkLookup::item(‘SOCIALMEDIA_ACCOUNTS’,2)));

$form->textField($urlmodel,‘url[2]’,array(‘placeholder’=>HkLookup::item(‘SOCIALMEDIA_ACCOUNTS’,3)));

$form->textField($urlmodel,‘url[3]’,array(‘placeholder’=>HkLookup::item(‘SOCIALMEDIA_ACCOUNTS’,4)));

I did something like this but got an error: "SocialmediaUrl.0" is not defined.


$model=new Socialmedia;

if(isset($_POST['SocialmediaUrl']))

{

	$urlmodel->attributes=$_POST['SocialmediaUrl'];

	for($i=0;$i<count(HkLookup::items('SOCIALMEDIA_ACCOUNTS'));$i++)

		ChromePhp::log($urlmodel[$i]['url']);

}

Hi

Have a look at the Yii tutorial.

Without knowing the structure of your models and exactly what you are trying to achieve, it will be difficult to get answers tailored to your needs.

Thanx a lot…!!!

After going through this tutorial i have been able to solve my problem… ::)

Here’s the solution if someone needs help in future.

View:


foreach($urlmodel as $i=>$item):

	echo '<div class="control-group"><div class="controls">';

	echo CHtml::activeTextField($item,"[$i]url",array('placeholder'=>$socialmediatypes[$i+1],'class'=>'span6'));

	echo '</div></div>';

endforeach;

Controller:


$model=new Organisation;

$urlmodel=array();

foreach($socialmediatype as $i)

	$urlmodel[]=new SocialmediaUrl; //multiple instances of a model


//Socialmedia url part

$url=array();$flag=0;

if(isset($_POST['HkSocialmediaUrl'])):

	foreach($_POST['HkSocialmediaUrl'] as $j=>$media):				

		if(isset($_POST['HkSocialmediaUrl'][$j])):

			$urlmodel[$j]->attributes=$media;					

				$url[$socialmediatype[$j+1]]=$urlmodel[$j]['url'];

			if($urlmodel[$j]['url']!='')

				$flag=1;

		endif;

	endforeach;

	if($flag==1)

		$model->socialmedia_url=json_encode($url);

endif;