background preloader

Yii framework

Facebook Twitter

Working with Forms: Using Form Builder | The Definitive Guide to Yii. When creating HTML forms, we often find that we are writing a lot of repetitive view code which is difficult to be reused in a different project. For example, for every input field, we need to associate it with a text label and display possible validation errors. To improve the reusability of these code, we can use the form builder feature. 1. Basic Concepts ¶ The Yii form builder uses a CForm object to represent the specifications needed to describe an HTML form, including which data models are associated with the form, what kind of input fields there are in the form, and how to render the whole form. Form input specifications are organized in terms of a form element hierarchy. When users submit a form, the data entered into the input fields of the whole form hierarchy are submitted, including those input fields that belong to the sub-forms. 2.

In the following, we show how to use the form builder to create a login form. First, we write the login action code: 3. Specifying Input Element <? CForm. Look up a class, method, property or event CForm represents a form object that contains form input specifications. The main purpose of introducing the abstraction of form objects is to enhance the reusability of forms. In particular, 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. It relies on the rendering process to accomplish form input decoration. Reusability is mainly achieved in the rendering process. A form can be rendered in different ways. Form input specifications are organized in terms of a form element hierarchy. Sub-forms are mainly used to handle multiple models. Form input specifications are given in terms of a configuration array which is used to initialize the property values of a CForm object. 'elements'=>array( 'username'=>array('type'=>'text', 'maxlength'=>80), 'password'=>array('type'=>'password', 'maxlength'=>80), ) Protected Properties Method Details.

Sample

Tutorials. Yii-1.0.8.v0.3 - yii-1.0-cheatsheet.pdf. Yii – Converting MySql dates to locale dates. It seems strange to me that two systems that have been around for so long and worked so closely together, do not manipulate dates in the same way. It is also so easy to waste hours of time trying to manipulate dates, check that date A is less than date B and so on... Using Events in a Yii model The Yii model, or more accurately the CActiveRecord class, provides a number of hooks with which you can customise your workflow on database reads and updates.

Using the afterFind and beforeSave events enables you to intercept and modify data before passing it on either to User/view layer or back to the database. PHP date class On this basis, you can modify date formats between the two layers as follows: You could, of course, add locale dependant formatting if required. It is worth noting that; i) afterSave will leave your date in MySql format. Ii) Your events must return true otherwise this can cause you problems such as the model->save() not working for no apparent reason. Yii Framework.