I don't know how to write the title to describe the problem that I want to explain, that is, the form generator function of Yii is more flexible.
The default Yii form builder only needs to do this:
The code is as follows: |
Copy code |
1 $ form = new CForm ('application. views. site. Loginform', $ model ); |
The application. views. site. loginForm can also be an array of configurations. However, if the $ model parameter is not passed, the following error occurs: Fatal error: Call to a member function isAttributeSafe ()
For example, if I want to generate a group of forms, but I don't want to rely on the model, what should I do if I can generate a group of forms according to the configuration,
The label of the default form is displayed based on $ model-> attributes, so I did two things:
1. Inherit from CFormInputElement to overwrite the renderLabel method, and display the label as the label of the configured element.
2. Inherit from CForm to overwrite the renderElement method, $ element instanceof UCFormInputElement, overwrite the render method, and output Elements and getButtons cyclically.
Directly run the code:
App/protected/extensions/UCForm. php
The code is as follows: |
Copy code |
<? Php /** * @ Author Ryan <yuansir@live.cn/yuansir-web.com> */ Class UCForm extends CForm { Public function render () { $ Output = $ this-> renderBegin (); Foreach ($ this-> getElements () as $ element) { $ Output. = $ element-> render (); } Foreach ($ this-> getButtons () as $ button) { $ Output. = $ button-> render (); } $ Output. = $ this-> renderEnd (); Return $ output; } Public function renderElement ($ element) { If (is_string ($ element )) { If ($ e = $ this [$ element]) === null & ($ e = $ this-> getButtons ()-> itemAt ($ element )) = null) Return $ element; Else $ Element = $ e; } If ($ element-> getVisible ()) { // UCFormInputElement replaces CFormInputElement If ($ element instanceof UCFormInputElement) { If ($ element-> type = 'ddden ') Return "<div style =" visibility: hidden "> n". $ element-> render (). "</div> n "; Else Return "<div class =" row field _ {$ element-> name} "> n". $ element-> render (). "</div> n "; } Else if ($ element instanceof CFormButtonElement) Return $ element-> render (). "n "; Else Return $ element-> render (); } Return ''; } } |
Here is a simple call example:
The code is as follows: |
Copy code |
<? Php /** * @ Author Ryan <yuansir@live.cn/yuansir-web.com> */ Class PlayerSearchController extends Controller { Public function actionIndex () { $ Config = array ( 'Class' => 'ddd ', 'Action' => '', 'Elements' => array ( '<Br> ', 'Username' => array ( 'Label' => 'User name', // note the label 'Type' => 'text ', 'Maxlength' => 32, 'Value' =>'' ), '<Br> ', 'Password' => array ( 'Label' => 'Nickname AH', // note the label here. 'Type' => 'password ', 'Maxlength' => 32, 'Value' =>'' ), ), 'Buttons' => array ( 'Login' => array ( 'Type' => 'Submit ', 'Label' => 'login ', ), ), ); $ Model = new CFormModel (); $ Form = new UCForm ($ config, $ model ); $ This-> render ('index', compact ('form ')); } } |