Widget inside a CActiveForm widget

Hi all,

Just a simple question:

Is it good practices to make a widget dependent on a model? This way, the widget call would have the model object passed through its parameters.

Here’s how I would call it:




<?php $this->widget('my_widget',array('model'=>$model));?>



It works, but I need to know if this is good practices knowing that the widget always needs a model to work, which means dependency on an application resource.

Thanks a lot.

Alejandro.

We can make it independent.

For example, if we don’t use a model but we want to use just widget, then, based on our need, we can create our own views and actions.

in, widget’s init method, we can set default model, or can hide views.

Thanks a lot for the reply. I’m going to have a look at how to create actions for it.

Alejandro.

About good practices…

Is that widget helping you to encapsulate dynamics (js scripts, css) specific for that model making your view cleaner? Have you seen zii widgets? They are dependent on data providers and that doesn’t mean they follow wrong practices. They automate certain tasks and encapsulate css and js. If you are just rendering HTML and that is not used along other sections of the project, then… I don’t even think is useful…

Hi Antonio,

I appreciate your reply. The widget is just rendering HTML but it is used a lot in other project sections. The solution I will try to implement is to make the widget independent and show/remove HTML fields according to the parameters that the widget will accept. This way, I ensure it doesn’t need a model to run and will only show the HTML code requested.

Thanks again,

Alejandro.

I have one or two widgets which depends on a model, but it handles that internally in the init function. I don’t pass the model to it.

Makes it cleaner IMO.

Of course, it’s hardwired to a particular model/situation, but I can live with that.

If it eases my workflow.

And I know I am going to use it in several projects.

An example is a tag cloud using the taggable behavior.

Since the taggable behavior assumes something about the model, I can make my widget assume the same.

Of course, I could just make it accept an array, and it would become more universal, but what do I gain from that?

Hi there,

Thanks for your help. I ended up doing as you said: Internally defined the model for the widget to use instead of passing it as a parameter. The widget depends on a model, but the views are a lot simpler when I implement it, plus I don’t have to write the same code over and over.

Thank you.

Alejandro.