How Do I Handle A Cactiveform In A Widget?

I am having trouble trying to figure out how I handle a (CActiveForm) form in a widget.

I have a widget with a newsletter signup form.

The widget can be in any view so I am having trouble knowing how to submit the form and then return to the view the form widget was on.


  • Some listing page is rendered by the listing controller, with newsletter widget in the sidebar.

  • Someone fills out the form and submits to newsletter signup controller.

  • Let’s say there are newsletter form validation errors…

How do we get back to the listing view? I can save the route and go back but that can’t be right, it’s too cumbersome, I must be using the wrong approach.

Please help if you have a suggestion. I feel pretty strong at yii, I probably just need to be pointed in the right direction.

Thanks in advance.

You can use ajax.

Assume you have a widget view ‘signup’ and a CFormModel ‘NewsletterForm’:

class Newsletter extends CWidget 


    public function run()


        $model = new NewsletterForm(); 




view signup.php


     echo CHtml::activeTextField($model,'email');




         $ajaxUrl = Yii::app()->createUrl('newsletter/signup');

         echo CHtml::ajaxSubmitButton('Signup',$ajaxUrl,array('update'=>'#signup-message')); 



<div id="signup-message"></div>

Newsletter controller:

public function actionSignUp()


  if (Yii::app()->request->isAjaxRequest && isset($_POST['NewsletterForm']))


     $model = new NewsletterForm(); 

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



         ... your signup code ...

         echo 'Registered ....';




       echo CHtml::errorSummary($model);  

       //or custom output with $model->getErrors() ... 







Works like a charm, thanks!