To solve this issues what I do is to create a function that will handle all elements in the form. You could include that function through a .live, or a onsubmit, or whatever…
The function needs to get the elements of the form by serializing this latest. For example:
onclick="javascript: var data = $(this.form).serialize(); $.ajax(…"
public function actionCreate()
{
$model=new Pagamarela;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Pagamarela']))
{
$model->attributes=$_POST['Pagamarela'];
$arrayMeios = $_POST['Pagamarela']['meios'];
$status = $_POST['Pagamarela']['status'];
Yii::log($arrayMeios,'info','arraymeios');
Yii::log($status,'info','status');
if(!empty($arrayMeios)){
foreach($arrayMeios as $meio){
Yii::log($meio,'info','meio');
}
}
if($model->save())
$this->redirect(array('index'));
}
$this->render('create',array(
'model'=>$model,
));
}
the ajax form (will write the fields):
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'createDialog',
'options'=>array(
'title'=>Yii::t('meios','Criar Meio de contato'),
'autoOpen'=>'true',
'modal'=>'true',
'width'=>'auto',
'height'=>'auto',
),
));
echo $this->renderPartial('_formDialog', array('model'=>$model));
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
and cel controller
public function actionCriarMeio(){
$model = new Meios;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
// copiado do exemplo no site: http://www.yiiframework.com/wiki/72/cjuidialog-and-ajaxsubmitbutton
// Flag to know if we will render the form or try to add
// new jon.
$flag=true;
if(isset($_POST['Meios']))
{
$flag=false;
$model->attributes=$_POST['Meios'];
if($model->save()){
$name = $model->id;
$value = $model->valor;
$htmlOptions = array('readonly'=>'readonly');
//Return an <option> and select it
//echo CHtml::tag('option', array('value'=>$model->id,'selected'=>true),CHtml::encode($model->nome),true);
echo "<div id='row'>";
echo CHtml::label($name, $name);
echo CHtml::textField($name, $value, $htmlOptions);
echo CHtml::hiddenField("Pagamarela_meios[$name]", $name);
echo CHtml::hiddenField("Pagamarela_status", $name);
echo "</div>";
}
}
if ($flag)
$this->renderPartial('createDialog',array('model'=>$model,),false,true);
}