Yii - Cactiveform, cform, form builder - confusion

2k Views Asked by At

Those are three concepts on Yii that I really don't get what should we use, on what scenarios?

Can anyone be kind enough to clarify those Yii elements, and on what situation should we use them?

2

There are 2 best solutions below

0
On

In documentation of CForm one can read the following:

...we can divide a form in two parts: those that specify each individual form inputs, and those that decorate the form inputs. A CForm object represents the former part...

...and CActiveForm represents the latter.

In other words, CForm specifies elements of the form but CActiveForm (being a widget) renders it.

Looking at the source code we state that CForm can also render() itself and its rendering relies on and is wrapped by CActiveForm widget by introducing its configuration property activeForm, though rendering input elements and buttons is implemented by its own methods renderElements() and renderButtons() relatively. By default their implementations rely on classes using CHtml's static methods what is exactly the same (or almost exactly the same) what CActiveForm's rendering methods do. Of course, default behavior can be overriden by extending the class.

That's why it's the question of a taste which technique to use: CActiveForm widget alone combining form fields' and buttons' declaration with their representation in a view file by calling convenient (required) methods of CActiveForm instance or CForm class declaring form's input specifications in a separate configuration file and customizing its rendering by pointing at appropriate active form widget and/or by overriding default rendering methods. The latter technique allows to reuse a form in several actions easily and is no more than using form builder.

0
On

Check here for live examples of ActiveForm, CForm, et cetera. You can also see the live Model, View & Controller files.