Using ActiveForm

Now we will create an HTML form in view to send data from view to controller. We could build a form in the standard way using the form tag and input fields, but Yii2 provides helper classes that simplify the building of a form and its content.

For this purpose, we will use ActiveForm, a widget that builds an interactive HTML form for one or multiple data models.

As for any Yii2 widget, we will indicate with the begin()static method, the moment we start using it, and with the end()static method, the moment we stop using it, from yii\widgets\ActiveForm. The code between these methods will be placed in the form:

$form = ActiveForm::begin();
... content here ...
ActiveForm::end();

The first method, begin(), returns an object that we can ...

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.