Class CForm
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. That is, a rendering process can be reused to render different CForm objects.
A form can be rendered in different ways. One can call the CForm::render()
method to get a quick form rendering without writing any HTML code; one can also
override CForm::render()
to render the form in a different layout; and one can
use an external view template to render each form element explicitly. In these
ways, the CForm::render()
method can be applied to all kinds of forms and thus
achieves maximum reusability; while the external view template keeps maximum
flexibility in rendering complex forms.
Form input specifications are organized in terms of a form element hierarchy.
At the root of the hierarchy, it is the root CForm object. The root form object
maintains its children in two collections: elements and buttons.
The former contains non-button form elements (CFormStringElement
, CFormInputElement
and CForm); while the latter mainly contains button elements
(CFormButtonElement
). When a CForm object is embedded in the elements collection, it is called a sub-form which can have its own elements and buttons collections and thus form the whole form
hierarchy.
Sub-forms are mainly used to handle multiple models. For example, in a user registration form, we can have the root form to collect input for the user table while a sub-form to collect input for the profile table. Sub-form is also a good way to partition a lengthy form into shorter ones, even though all inputs may belong to the same model.
Form input specifications are given in terms of a configuration array which is used to initialize the property values of a CForm object. The elements and buttons properties need special attention as they are the main properties to be configured. To configure elements, we should give it an array like the following:
'elements'=>array( 'username'=>array('type'=>'text', 'maxlength'=>80), 'password'=>array('type'=>'password', 'maxlength'=>80), )
The above code specifies two input elements: 'username' and 'password'. Note the model object must have exactly the same attributes 'username' and 'password'. Each element has a type which specifies what kind of input should be used. The rest of the array elements (e.g. 'maxlength') in an input specification are rendered as HTML element attributes when the input field is rendered. The buttons property is configured similarly.
If you're going to use AJAX and/or client form validation with the enabled
error summary you have to set CForm::$showErrors
property to true. Please refer
to it's documentation for more details.
For more details about configuring form elements, please refer to CFormInputElement
and CFormButtonElement
.
- CComponent
- CFormElement
- CForm implements ArrayAccess
Copyright: 2008-2013 Yii Software LLC
License: http://www.yiiframework.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 1.1
Located at x2engine/framework/web/form/CForm.php
public
|
#
__construct( mixed $config,
Constructor. If you override this method, make sure you do not modify the method signature, and also make sure you call the parent implementation. |
protected
|
|
public
boolean
|
|
public
boolean
|
|
public
boolean
|
#
validate( )
Validates the models associated with this form. All models, including those
associated with sub-forms, will perform the validation. You may use |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
array
|
|
public
|
#
getElements( )
Returns the input elements of this form. This includes text strings, input
elements and sub-forms. Note that the returned result is a |
public
|
#
setElements( array $elements )
Configures the input elements of this form. The configuration must be an
array of input configuration array indexed by input name. Each input
configuration array consists of name-value pairs that are used to initialize a
|
public
|
#
getButtons( )
Returns the button elements of this form. Note that the returned result is a
|
public
|
#
setButtons( array $buttons )
Configures the buttons of this form. The configuration must be an array of
button configuration array indexed by button name. Each button configuration
array consists of name-value pairs that are used to initialize a |
public
string
|
#
render( )
Renders the form. The default implementation simply calls |
public
string
|
#
renderBegin( )
Renders the open tag of the form. The default implementation will render the open form tag. |
public
string
|
|
public
string
|
#
renderBody( )
Renders the body content of this form. This method mainly renders elements and buttons. If |
public
string
|
|
public
string
|
|
public
string
|
#
renderElement( mixed $element )
Renders a single element which could be an input element, a sub-form, a string, or a button. |
public
|
#
addedElement( string $name,
This method is called after an element is added to the element collection. |
public
|
#
removedElement( string $name,
This method is called after an element is removed from the element collection. |
protected
boolean
|
#
evaluateVisible( )
Evaluates the visibility of this form. This method will check the visibility of the elements. If any one of them is visible, the form is considered as visible. Otherwise, it is invisible. |
protected
string
|
|
public
boolean
|
#
offsetExists( mixed $offset )
Returns whether there is an element at the specified offset. This method is required by the interface ArrayAccess. |
public
mixed
|
|
public
|
|
public
|
#
offsetUnset( mixed $offset )
Unsets the element at the specified offset. This method is required by the interface ArrayAccess. |
__get(),
__isset(),
__set(),
__toString(),
configure(),
getParent(),
getVisible(),
setVisible()
|
public
string
|
$title |
|
#
the title for this form. By default, if this is set, a fieldset may be rendered around the form body using the title as its legend. Defaults to null. |
public
string
|
$description |
|
#
the description of this form. |
public
string
|
$method | 'post' |
#
the submission method of this form. Defaults to 'post'. This property is ignored when this form is a sub-form. |
public
mixed
|
$action | '' |
#
the form action URL (see |
public
string
|
$inputElementClass | 'CFormInputElement' |
#
the name of the class for representing a form input element. Defaults to 'CFormInputElement'. |
public
string
|
$buttonElementClass | 'CFormButtonElement' |
#
the name of the class for representing a form button element. Defaults to 'CFormButtonElement'. |
public
array
|
$attributes | array() |
#
HTML attribute values for the form tag. When the form is embedded within another form, this property will be used to render the HTML attribute values for the fieldset enclosing the child form. |
public
boolean
|
$showErrorSummary | false |
#
whether to show error summary. Defaults to false. |
public
boolean|null
|
$showErrors |
|
#
whether error elements of the form attributes should be rendered. There are three possible valid values: null, true and false. Defaults to null meaning that False value means that the error elements of the form attributes shall not be displayed. True value means that the error elements of the form attributes will be rendered. |
public
string|null
|
$errorSummaryHeader |
|
#
HTML code to prepend to the list of errors in the error summary. See |
public
string|null
|
$errorSummaryFooter |
|
#
HTML code to append to the list of errors in the error summary. See |
public
array
|
$activeForm | array('class'=>'CActiveForm') |
#
the configuration used to create the active form widget. The widget will be
used to render the form tag and the error messages. The 'class' option is
required, which specifies the class of the widget. The rest of the options will
be passed to |
public
|
$root |
#
The top-level form object. |
public
|
$activeFormWidget |
#
The active form widget associated with this form. This method will return the
active form widget as specified by |
public
|
$owner |
#
The owner of this form. This refers to either a controller or a widget by which the form is created and rendered. |
public
|
$model |
#
The model associated with this form. If this form does not have a model, it will look for a model in its ancestors. |
public
array
|
$models |
#
The models that are associated with this form or its sub-forms. |
public
|
$elements |
#
The form elements. |
public
|
$buttons |
#
The form elements. |
$parent,
$visible
|