Yii form builder does not rely on Model

Source: Internet
Author: User
Tags yii

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 '));
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.