当前位置:首页 > CMS教程 > 其它CMS > 列表

Yii不依赖Model的表单生成器用法实例

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-02 21:04:23 浏览: 评论:0 

这篇文章主要介绍了Yii不依赖Model的表单生成器用法,以实例形式对比分析了不依赖Model的表单生成器实现方法,是非常实用的技巧,需要的朋友可以参考下

本文实例讲述了Yii不依赖Model的表单生成器用法。分享给大家供大家参考。具体实现方法如下:

默认的Yii的表单生成器只需要这样就可以了:

$form = new CForm('application.views.site.loginForm', $model);

这里的application.views.site.loginForm也可以是配置数组。但是如果$model参数不传的话是会报错的:Fatal error: Call to a member function isAttributeSafe()

比如我要生成一个组表单,但是我不想依赖于model,根据配置就可以生成一组表单该怎么办,默认生成的表单的label是根据$model->attributes来显示的,所以我做了2件事:

1.继承CFormInputElement覆盖renderLabel方法,将label显示成自己配置的element的label

2.继承CForm覆盖renderElement方法,$element instanceof UCFormInputElement,并覆盖render方法,将Elements和getButtons循环输出

直接上代码:

app/protected/extensions/UCForm.php

代码如下:

  1. <?php 
  2. /** 
  3.  * @author Ryan <yuansir@live.cn/yuansir-web.com> 
  4.  */ 
  5. class UCForm extends CForm 
  6. { 
  7.  public function render() 
  8.  { 
  9.   $output = $this->renderBegin(); 
  10.   foreach ($this->getElements() as $element) 
  11.   { 
  12.    $output .= $element->render(); 
  13.   } 
  14.   foreach ($this->getButtons() as $button) 
  15.   { 
  16.    $output .= $button->render(); 
  17.   } 
  18.   $output .= $this->renderEnd(); 
  19.   return $output; 
  20.  } 
  21.  public function renderElement($element) 
  22.  { 
  23.   if (is_string($element)) 
  24.   { 
  25.    if (($e = $this[$element]) === null && ($e = $this->getButtons()->itemAt($element)) === null) 
  26.     return $element; 
  27.    else 
  28.     $element = $e; 
  29.   } 
  30.   if ($element->getVisible()) 
  31.   { 
  32.    //UCFormInputElement 代替 CFormInputElement 
  33.    if ($element instanceof UCFormInputElement) 
  34.    { 
  35.     if ($element->type === 'hidden') 
  36.      return "<div style="visibility:hidden">n" . $element->render() . "</div>n"; 
  37.     else 
  38.      return "<div class="row field_{$element->name}">n" . $element->render() . "</div>n"; 
  39.    } 
  40.    else if ($element instanceof CFormButtonElement) 
  41.     return $element->render() . "n"; 
  42.    else 
  43.     return $element->render(); 
  44.   } 
  45.   return ''; 
  46.  } 
  47. } 

再来个简单的调用示例:

  1. <?php 
  2. /** 
  3.  * @author Ryan <yuansir@live.cn/yuansir-web.com> 
  4.  */ 
  5. class PlayerSearchController extends Controller 
  6. { 
  7.  public function actionIndex() 
  8.  { 
  9.   $config = array( 
  10.       'class' => 'ddd', 
  11.       'action'=>'', 
  12.       'elements' => array( 
  13.    '<br><br>', 
  14.    'username' => array( 
  15.        'label'=>'用户名啊',//注意这里的label 
  16.        'type' => 'text', 
  17.        'maxlength' => 32, 
  18.        'value' => '' 
  19.    ), 
  20.    '<br><br>', 
  21.    'password' => array( 
  22.        'label'=>'昵称啊',//注意这里的label 
  23.        'type' => 'password', 
  24.        'maxlength' => 32, 
  25.        'value' => '' 
  26.    ), 
  27.       ), 
  28.       'buttons' => array( 
  29.    'login' => array( 
  30.        'type' => 'submit', 
  31.        'label' => 'Login', 
  32.    ), 
  33.       ), 
  34.   ); 
  35.   $model = new CFormModel(); 
  36.   $form = new UCForm($config, $model); 
  37.   $this->render('index', compact('form')); 
  38.  } 
  39. } 

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

Tags: Yii Model表单生成器

分享到: