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 use inside the content to create the input fields. This method accepts an array as the parameter to indicate configuration attributes to be applied. The last method, end()
, marks the end of the widget, so this can be rendered with its content.
Now...