How To Send A Cactiveform Widget

hello world :)

I have a problem… (certainly a stupid newbie problem :P)

I try to update a view (a form) by using ajax and renderPartial.

So i followed this : http://www.yiiframework.com/wiki/49/update-content-in-ajax-with-partialrender/

there is my problem :

In my view, I have a form with several options, In the form, I put a comboBox with 2 choices. depending of this choice, I want to show different fields, options, to fill in my form.

So I put this in my radioButton,in the formView :


<?php 

echo CHtml::radioButton('btn', false, array(

			value'=>'2',

			'id'=>'radioAdd',

			'name'=>'gestionnaireGroupe',

			'uncheckValue'=>null,

				'checked'=>true,

				'onChange'=>CHtml::ajax(array(	'type'=>'POST',

			        'url'=>array("admin/UpdateAjaxFormGestionnaire"),

				'update'=>'#gestionnaireCell')),

		)); ?>

I declared my form above the radio button still in my formView like this :


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

		'id'=>'evenement-form',

		// Please note: When you enable ajax validation, make sure the corresponding

		// controller action is handling ajax validation correctly.

		// There is a call to performAjaxValidation() commented in generated controller code.

		// See class documentation of CActiveForm for details on this.

		'enableAjaxValidation'=>true,

		'clientOptions'=>array('validateOnSubmit'=>true),

	)); ?>

then this what I have in my controller for this action :


public function actionUpdateAjaxFormGestionnaire()

    { 

        $this->renderPartial('_ajaxContentFormGestionnaire', array(

                                                    'eventForm'=>$eventForm,

                                                    'gestionnaire'=>$gestionnaire,

                                                   ), false, true);

    }



and this in my _ajaxContentFormGestionnaire view file :


<td class="enableCreate">

 

                    <div class="row">

                        <?php echo $form->labelEx($eventForm,'Nom'); ?>

                        <?php echo $form->textField($eventForm,'Nom',array('size'=>50,'maxlength'=>50)); ?>

                        <?php echo $form->error($eventForm,'Nom'); ?>

                    </div>

 

                    <div class="row">

                        <?php echo $form->labelEx($eventForm,'Prenom'); ?>

                        <?php echo $form->textField($eventForm,'Prenom',array('size'=>50,'maxlength'=>50)); ?>

                        <?php echo $form->error($eventForm,'Prenom'); ?>

                    </div>

 

... ...



But the problem is that the $form widget is not initialized or passed or … in the _ajax file. So I have this error :


Fatal error: Call to a member function labelEx() on a non-object in D:\WWW\VincentM\LayoutColloques\protected\views\admin\_ajaxContentFormGestionnaire.php on line 4

do you have any idea to help me? thx in advance.

Obviously you need to pass form variable ($form in your case) to view.

thx for your response but… Where/How can I do that?

I tried :


$this->renderPartial('_ajaxContentFormGestionnaire', array(

'CActiveForm'=>$form,

'eventForm'=>$eventForm,

'gestionnaire'=>$gestionnaire,  ), false, true);

I have to write something in the calling of the radioButton? in the controller? In the _ajax file?

Moved from General Discussion for Yii 2.0 to General Discussion for Yii 1.1.x.

So… I tried without any success…

In my main view I send the form to the ‘admin’ controller for UpdateAjaxListGestionnaire action :




<?php 

echo CHtml::radioButton('btn', false, array(

	'value'=>'1',

	'id'=>'radioList',

	'name'=>'gestionnaireGroupe',

	'uncheckValue'=>null,

	'onChange'=>CHtml::ajax(array(	

'type'=>'GET', 	

'form'=>serialize($form),											'url'=>array("admin/UpdateAjaxListGestionnaire"),

'update'=>'#gestionnaireCell')))); ?>



My action in my controller "recieve" the form :




$this->renderPartial('_ajaxContentListGestionnaire', array(

    			'form'=>$_GET['form'],

    			'eventForm'=>$eventForm,

    			'gestionnaire'=>$gestionnaire,

    	), false, true);  



var_dump($_GET[‘form’]); Display “Null” on my page… so it is not send to the controller…

and then In my _ajax view, I recieve the form :


<?php $form = unserialize($form); ?>

var_dump($form); Display "bool(fasle)"

what’s wrong?? :(

Try using


CHtml::activeTextField

instead of $form textfield function in _ajaxContentFormGestionnaire.

You can also use other functions like activeLabelEx, error from CHtml class.

I am assuming that $eventForm is form-model or active-record-model instance.

http://www.yiiframework.com/doc/api/1.1/CHtml

Hi, thx for your response.

I tried but It didn’t work …

I’m still on this error so I’ll show you what I have now.

(A little bit of progress… )

In my main view when I show the string ( <?php var_dump($form); ?>) of my $form I have :


object(CActiveForm)#53 (19) { ["action"]=> string(0) "" ["method"]=> string(4) "post" ["stateful"]=> bool(false) ["errorMessageCssClass"]=> string(12) "errorMessage" ["htmlOptions"]=> array(1) { ["id"]=> string(14) "evenement-form" } ["clientOptions"]=> array(1) { ["validateOnSubmit"]=> bool(true) } ["enableAjaxValidation"]=> bool(true) ["enableClientValidation"]=> bool(false) ["focus"]=> NULL ["attributes":protected]=> array(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> { ["EventForm_Titre"]=> array(6) { ["id"]=> string(15) "EventForm_Titre" ["inputID"]=> string(15) "EventForm_Titre" ["errorID"]=> string(19) "EventForm_Titre_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(5) "Titre" ["enableAjaxValidation"]=> bool(true) } ["EventForm_Libelle"]=> array(6) { ["id"]=> string(17) "EventForm_Libelle" ["inputID"]=> string(17) "EventForm_Libelle" ["errorID"]=> string(21) "EventForm_Libelle_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(7) "Libelle" ["enableAjaxValidation"]=> bool(true) } ["EventForm_NumCompteIban"]=> array(6) { ["id"]=> string(23) "EventForm_NumCompteIban" ["inputID"]=> string(23) "EventForm_NumCompteIban" ["errorID"]=> string(27) "EventForm_NumCompteIban_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(13) "NumCompteIban" ["enableAjaxValidation"]=> bool(true) } ["EventForm_DateUltimePaiement"]=> array(6) { ["id"]=> string(28) "EventForm_DateUltimePaiement" ["inputID"]=> string(28) "EventForm_DateUltimePaiement" ["errorID"]=> string(32) "EventForm_DateUltimePaiement_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(18) "DateUltimePaiement" ["enableAjaxValidation"]=> bool(true) } ["EventForm_DateUltimeInscription"]=> array(6) { ["id"]=> string(31) "EventForm_DateUltimeInscription" ["inputID"]=> string(31) "EventForm_DateUltimeInscription" ["errorID"]=> string(35) "EventForm_DateUltimeInscription_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(21) "DateUltimeInscription" ["enableAjaxValidation"]=> bool(true) } ["EventForm_NbMaxAccompagnant"]=> array(6) { ["id"]=> string(27) "EventForm_NbMaxAccompagnant" ["inputID"]=> string(27) "EventForm_NbMaxAccompagnant" ["errorID"]=> string(31) "EventForm_NbMaxAccompagnant_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(17) "NbMaxAccompagnant" ["enableAjaxValidation"]=> bool(true) } ["EventForm_Dvd"]=> array(6) { ["id"]=> string(13) "EventForm_Dvd" ["inputID"]=> string(13) "EventForm_Dvd" ["errorID"]=> string(17) "EventForm_Dvd_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(3) "Dvd" ["enableAjaxValidation"]=> bool(true) } ["EventForm_Acte"]=> array(6) { ["id"]=> string(14) "EventForm_Acte" ["inputID"]=> string(14) "EventForm_Acte" ["errorID"]=> string(18) "EventForm_Acte_em_" ["model"]=> string(9) "EventForm" ["name"]=> string(4) "Acte" ["enableAjaxValidation"]=> bool(true) } } ["summaryID":protected]=> string(18) "evenement-form_es_" ["_summaryAttributes":"CActiveForm":private]=> array(16) { [0]=> string(15) "EventForm_Titre" [1]=> string(17) "EventForm_Libelle" [2]=> string(31) "EventForm_DateUltimeInscription" [3]=> string(27) "EventForm_NbMaxAccompagnant" [4]=> string(25) "EventForm_Id_Gestionnaire" [5]=> string(23) "EventForm_NumCompteIban" [6]=> string(13) "EventForm_Dvd" [7]=> string(14) "EventForm_Acte" [8]=> string(28) "EventForm_DateUltimePaiement" [9]=> string(13) "EventForm_Nom" [10]=> string(16) "EventForm_Prenom" [11]=> string(17) "EventForm_Adr_Rue" [12]=> string(20) "EventForm_Adr_Numero" [13]=> string(24) "EventForm_Adr_CodePostal" [14]=> string(22) "EventForm_Adr_Localite" [15]=> string(18) "EventForm_Adr_Pays" } ["actionPrefix"]=> NULL ["skin"]=> string(7) "default" ["_id":"CWidget":private]=> string(14) "evenement-form" ["_owner":"CWidget":private]=> object(AdminController)#15 (15) { ["layout"]=> string(17) "//layouts/column2" ["menu"]=> array(0) { } ["breadcrumbs"]=> array(0) { } ["defaultAction"]=> string(5) "index" ["_id":"CController":private]=> string(5) "admin" ["_action":"CController":private]=> object(CInlineAction)#16 (4) { ["_id":"CAction":private]=> string(14) "ajoutEvenement" ["_controller":"CAction":private]=> *RECURSION* ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["_pageTitle":"CController":private]=> NULL ["_cachingStack":"CController":private]=> NULL ["_clips":"CController":private]=> NULL ["_dynamicOutput":"CController":private]=> NULL ["_pageStates":"CController":private]=> NULL ["_module":"CController":private]=> NULL ["_widgetStack":"CBaseController":private]=> array(1) { [0]=> *RECURSION* } ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["_widgetStack":"CBaseController":private]=> array(0) { } ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } 

In my view, I send the form to the controller in an data Array :


CHtml::ajax(array(

'type'=>'POST', 'url'=>array("admin/UpdateAjaxListGestionnaire"),								'data'=>array('form'=>serialize($form)),

'update'=>'#gestionnaireCell')) )); ?>

In my controller :


$this->renderPartial('_ajaxContentListGestionnaire', array(

'form'=>$_POST['form'],

'eventForm'=>$eventForm,

'gestionnaire'=>$gestionnaire), false, true); 

and In my _ajaxView, I try to use $form

when I show the string of $form ( var_dump($form); )

without serialisation :


array(11) { ["action"]=> string(0) "" ["method"]=> string(4) "post" ["stateful"]=> string(5) "false" ["errorMessageCssClass"]=> string(12) "errorMessage" ["htmlOptions"]=> array(1) { ["id"]=> string(14) "evenement-form" } ["clientOptions"]=> array(1) { ["validateOnSubmit"]=> string(4) "true" } ["enableAjaxValidation"]=> string(4) "true" ["enableClientValidation"]=> string(5) "false" ["focus"]=> string(0) "" ["actionPrefix"]=> string(0) "" ["skin"]=> string(7) "default" } 

with serialization in main view :


string(3932) "O:11:"CActiveForm":19:{s:6:"action";s:0:"";s:6:"method";s:4:"post";s:8:"stateful";b:0;s:20:"errorMessageCssClass";s:12:"errorMessage";s:11:"htmlOptions";a:1:{s:2:"id";s:14:"evenement-form";}s:13:"clientOptions";a:1:{s:16:"validateOnSubmit";b:1;}s:20:"enableAjaxValidation";b:1;s:22:"enableClientValidation";b:0;s:5:"focus";N;s:13:"ᅵ*ᅵattributes";a:8:{s:15:"EventForm_Titre";a:6:{s:2:"id";s:15:"EventForm_Titre";s:7:"inputID";s:15:"EventForm_Titre";s:7:"errorID";s:19:"EventForm_Titre_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:5:"Titre";s:20:"enableAjaxValidation";b:1;}s:17:"EventForm_Libelle";a:6:{s:2:"id";s:17:"EventForm_Libelle";s:7:"inputID";s:17:"EventForm_Libelle";s:7:"errorID";s:21:"EventForm_Libelle_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:7:"Libelle";s:20:"enableAjaxValidation";b:1;}s:23:"EventForm_NumCompteIban";a:6:{s:2:"id";s:23:"EventForm_NumCompteIban";s:7:"inputID";s:23:"EventForm_NumCompteIban";s:7:"errorID";s:27:"EventForm_NumCompteIban_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:13:"NumCompteIban";s:20:"enableAjaxValidation";b:1;}s:28:"EventForm_DateUltimePaiement";a:6:{s:2:"id";s:28:"EventForm_DateUltimePaiement";s:7:"inputID";s:28:"EventForm_DateUltimePaiement";s:7:"errorID";s:32:"EventForm_DateUltimePaiement_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:18:"DateUltimePaiement";s:20:"enableAjaxValidation";b:1;}s:31:"EventForm_DateUltimeInscription";a:6:{s:2:"id";s:31:"EventForm_DateUltimeInscription";s:7:"inputID";s:31:"EventForm_DateUltimeInscription";s:7:"errorID";s:35:"EventForm_DateUltimeInscription_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:21:"DateUltimeInscription";s:20:"enableAjaxValidation";b:1;}s:27:"EventForm_NbMaxAccompagnant";a:6:{s:2:"id";s:27:"EventForm_NbMaxAccompagnant";s:7:"inputID";s:27:"EventForm_NbMaxAccompagnant";s:7:"errorID";s:31:"EventForm_NbMaxAccompagnant_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:17:"NbMaxAccompagnant";s:20:"enableAjaxValidation";b:1;}s:13:"EventForm_Dvd";a:6:{s:2:"id";s:13:"EventForm_Dvd";s:7:"inputID";s:13:"EventForm_Dvd";s:7:"errorID";s:17:"EventForm_Dvd_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:3:"Dvd";s:20:"enableAjaxValidation";b:1;}s:14:"EventForm_Acte";a:6:{s:2:"id";s:14:"EventForm_Acte";s:7:"inputID";s:14:"EventForm_Acte";s:7:"errorID";s:18:"EventForm_Acte_em_";s:5:"model";s:9:"EventForm";s:4:"name";s:4:"Acte";s:20:"enableAjaxValidation";b:1;}}s:12:"ᅵ*ᅵsummaryID";s:18:"evenement-form_es_";s:31:"ᅵCActiveFormᅵ_summaryAttributes";a:16:{i:0;s:15:"EventForm_Titre";i:1;s:17:"EventForm_Libelle";i:2;s:31:"EventForm_DateUltimeInscription";i:3;s:27:"EventForm_NbMaxAccompagnant";i:4;s:25:"EventForm_Id_Gestionnaire";i:5;s:23:"EventForm_NumCompteIban";i:6;s:13:"EventForm_Dvd";i:7;s:14:"EventForm_Acte";i:8;s:28:"EventForm_DateUltimePaiement";i:9;s:13:"EventForm_Nom";i:10;s:16:"EventForm_Prenom";i:11;s:17:"EventForm_Adr_Rue";i:12;s:20:"EventForm_Adr_Numero";i:13;s:24:"EventForm_Adr_CodePostal";i:14;s:22:"EventForm_Adr_Localite";i:15;s:18:"EventForm_Adr_Pays";}s:12:"actionPrefix";N;s:4:"skin";s:7:"default";s:12:"ᅵCWidgetᅵ_id";s:14:"evenement-form";s:15:"ᅵCWidgetᅵ_owner";O:15:"AdminController":15:{s:6:"layout";s:17:"//layouts/column2";s:4:"menu";a:0:{}s:11:"breadcrumbs";a:0:{}s:13:"defaultAction";s:5:"index";s:16:"ᅵCControllerᅵ_id";s:5:"admin";s:20:"ᅵCControllerᅵ_action";O:13:"CInlineAction":4:{s:12:"ᅵCActionᅵ_id";s:14:"ajoutEvenement";s:20:"ᅵCActionᅵ_controller";r:91;s:14:"ᅵCComponentᅵ_e";N;s:14:"ᅵCComponentᅵ_m";N;}s:23:"ᅵCControllerᅵ_pageTitle";N;s:26:"ᅵCControllerᅵ_cachingStack";N;s:19:"ᅵCControllerᅵ_clips";N;s:27:"ᅵCControllerᅵ_dynamicOutput";N;s:24:"ᅵCControllerᅵ_pageStates";N;s:20:"ᅵCControllerᅵ_module";N;s:29:"ᅵCBaseControllerᅵ_widgetStack";a:1:{i:0;r:1;}s:14:"ᅵCComponentᅵ_e";N;s:14:"ᅵCComponentᅵ_m";N;}s:29:"ᅵCBaseControllerᅵ_widgetStack";a:0:{}s:14:"ᅵCComponentᅵ_e";N;s:14:"ᅵCComponentᅵ_m";N;}"

and with serialization in mainView and unserialize in _ajax view


bool(false)

:confused: so I have things but nothing work like I want…

Any clue plz?

I finnally success to do it like this :

in main view :




<?php 

echo CHtml::radioButton('btn', false, array(

	'value'=>'1',

	'id'=>'radioList',

	'name'=>'gestionnaireGroupe',

	'uncheckValue'=>null,

	'onChange'=>CHtml::ajax(array(	'type'=>'POST', 

	'url' => @Yii::app()->createUrl('admin/UpdateAjaxListGestionnaire', 

                                        array('form'=>serialize($form),

                                              'listGestionnaire'=>serialize($listGestionnaire),)),

       'update'=>'#gestionnaireCell')) )); ?>



In controller :




public function actionUpdateAjaxListGestionnaire()

    {    	

    	$this->renderPartial('_ajaxContentListGestionnaire', 

                             array(

				'form'=>$_GET['form'],

				'listGestionnaire'=>$_GET['listGestionnaire'],

			), 

    			false, 

    			true);  

    }



and in _ajax file :




<?php

$form = unserialize($form);

$listGestionnaire = unserialize($listGestionnaire);

?>



thx to everybody who tried to help me :)

Last question, by this way, the form and the list is send with "GET".

If i’m right, there is a limit of size to send?

so there is a way to send the $form and my list with "POST"?

thx :)