Should a widget ever handle post data?

I was wondering if there is consensus on whether a widget should ever handle and change post data.

I have something that I would like to use in many places, but it needs to have a form. If I made it a widget, I would still need a subclass of CFormModel to validate it, a widget code file, and a template.

Also, I couldn’t find any examples of other widgets that post data back and save it to the DB, so that makes me think it would be better to just use some actions in the controller with sub templates in the views directory.

Any thoughts?

Thanks!

Corey

Is not a bad idea, I think.

You can immagine a stuff like this "Fast Reply" implemented like a widget.

In the widget->run() you can put the code like the actionCreate of a standard controller, and in the view the html like in all other views.

In my thoughts the page with




class somewidget extends CWidget

{

   public function run()

   {

      ...

   }

}



can be used like Controller.

I have used a widget with post data many times. Does anyone think it’s a bad idea? Why would you NOT do it?

Places I have used it include comment form widgets, vote widgets (via AJAX) and search widgets.

a widget is a kind of controller

see portlets, login portlet in extension repository

I guess I was asking if it was standard to have the widget rely on a formModel object for validation as well. All the examples I could find had just the widget class file and a view, and I was making sure I Wasn’t doing something that would come back to bite me later. Thanks for the feedback!

I guess I was asking if it was standard to have the widget rely on a formModel object for validation as well. All the examples I could find had just the widget class file and a view, and I was making sure I Wasn’t doing something that would come back to bite me later. Thanks for the feedback!

I’m also do it.

As I can handle so many operation such as :- language selection, login.

Because some of these operations cannot be in any of controller and would be best as separating in their own controller (widget).

@mbi: If a widget is like a controller, then it certainly is OK to use POST to send data to it. Is this what you were trying to imply?

Since I can handle the login in the init() and run() functions, and the display in the renderContent() function, I can make the widgets self-contained in one file in most cases. The thing that feels weird is form validation. I still have to have a separate file with my FormModel object, and I would really like to keep my widget in one file.

I guess I have to choose between a widget with strong form validation and a widget in one file. Is this correct, or can I pass some sort of config array to the CForm constructor that would allow me to specify validation?

Ideally, I would love to be able to do something like:




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

			'id'=>'job-form',

			'validation' => array(

                                                                 array( 'fied1, field2', 'numeric'),

                                                                 array( 'fied3', 'required'),       

                                                                 .................

                                                             ),

		));


$this->endWidget();



Is there a good way to do something like this without specifically declaring a FormModel object?