Understanding Forms and MVC in the Yii Framework

March 7, 2014
The Yii Book If you like my writing on the Yii framework, you'll love "The Yii Book"!

HTML forms are one of the key pieces of any website. As is the case with many things, creating forms while using a framework such as Yii is significantly different than creating forms using standard HTML alone. In Chapter 9, “Working with Forms,” of “The Yii Book“, you’ll learn what you need to know to create HTML forms when using the Yii framework. You’ll comprehend the fundamentals of forms in Yii and see a few recipes for common form needs.

This is an excerpt from Chapter 9, “Working with Forms,” of “The Yii Book“.

Before getting into the code, let’s take a minute to think about the MVC architecture. A form is part of the view component, as a form is an aspect of the user interface. Forms, though, are almost always associated with specific models. A contact form may have its own model, not tied to a database table (in which case the model extends CFormModel), and a form for employees or departments will be based upon a model that is tied to a database table (in which case the model extends CActiveRecord, most likely). Whether the model extends CFormModel or CActiveRecord, the important thing to remember is that the form is tied to a model. This is significant because it’s the model that dictates what form elements should exist, controls validation of the form data, and even defines the form’s labels (e.g., “First Name” for the firstName attribute), and so forth.

{TIP} With the MVC approach, forms should be associated with models. There are situations where you might have a form not associated with a model, but that is extremely rare.

When you use Gii to auto-generate CRUD functionality for a model, the framework creates a form for you in a file named _form.php. This file in turn gets included by other view files (any view file in Yii that starts with an underscore is intended to be an include). Naturally, the controller dictates which primary view file gets rendered. Also understand that the same _form.php file is intended to be used whether the form is for creating new records or updating existing ones. Yii will take care of populating the form’s elements with the model’s current value when an update is being performed.

Because forms are normally tied to models, you’ll need access to a model instance when you go to create the form. Before getting to the view and its form, let’s be clear as to how the view accesses the specific model. A controller may have this code:

public function actionCreate() {
    $model=new Page;
    /* Code for validation and redirect upon save. */
    // If not saved, render the create view:
    $this->render('create',array(
        'model'=>$model
    ));
}

The create.php view file will include _form.php, passing along the model instance in the process:

<?php
echo $this->renderPartial('_form', array('model'=>$model));
?>

So now _form.php has access to the model instance and can create the form tied to that model. Once the form view file has access to the model, it can create form elements in one of two ways:

  • Invoking the CHtml class methods directly
  • Using the CActiveForm widget

In the chapter, I explain both approaches, but focus more on the later, which is the current default approach in Yii.

Of course, to be fair, you could create an HTML form using raw HTML, without Yii at all. The downside to that approach is it creates no tie-in between the model’s validation rules, errors, labels, etc., and the form. By creating the form using Yii, labels will be based upon the model definitions (meaning that changing just the model changes reference to attributes everywhere), invalid form values can automatically be highlighted, and much, much more. Plus, it’s not hard to use Yii to create a form, once you understand how.