Skip to main content

Posts

Yii Forms Basic Tips

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. 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. 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 crea...

Edit View Files in Yii Basics

In the HEAD, you’ll see that external files are linked using <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" /> Whenever you see Yii::app(), that refers to the Web application as a whole. You can access information about the user viewing a page, about the current or previously-viewed page, etc., there. Yii::app()->request specifically references the current page being accessed (or requested). The ->baseUrl part refers to the root URL for the application, like http://www.example.com. You should use Yii::app()->request->baseUrl for references to external files—CSS, JavaScript, images, and so forth—as the relative path to them can become muddled with the changed Yii URLs (like www.example.com/index.php/site/login). If you’re viewing a page like www.example.com/index.php/employee/view/id/1, which is intended to show the employee with an ID of 1, the actionView() method o...

Yii CHtml static class

Avaiable methods: CHtml::link() CHtml::ajaxLink() CHtml::button() CHtml::textField() CHtml::listData() CHtml::dropDownList() CHtml::link() method  public static string link ( string $text , mixed $url = ' # ' , array $htmlOptions = array ( ) ) Generates a hyperlink tag. Example 1: Linking to a controller action <?php echo CHtml :: link ( ' Link Text ' , array ( ' controller/action ' ) ) ; ?> HTML Output: < a href = " index.php?r=controller/action " > Link Text </ a > Example 2: Linking to a controller action with querystring parameters <?php echo CHtml :: link ( ' Link Text ' , array ( ' controller/action ' , ' param1 ' => ' value1 ' ) ) ; ?> HTML Output: < a href = " index.php?r=controller/action&param1=value1 " > Link Text </ a > Example 3: Linking to a control...