Yii不依赖Model的表单生成器用法实例
程序员文章站
2022-07-12 07:50:45
本文实例讲述了yii不依赖model的表单生成器用法。分享给大家供大家参考。具体实现方法如下:
默认的yii的表单生成器只需要这样就可以了:
复制代码 代码如下:$fo...
本文实例讲述了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
复制代码 代码如下:
<?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 代替 cforminputelement
if ($element instanceof ucforminputelement)
{
if ($element->type === 'hidden')
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 '';
}
}
/**
* @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 代替 cforminputelement
if ($element instanceof ucforminputelement)
{
if ($element->type === 'hidden')
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 '';
}
}
再来个简单的调用示例:
复制代码 代码如下:
<?php
/**
* @author ryan <yuansir@live.cn/yuansir-web.com>
*/
class playersearchcontroller extends controller
{
public function actionindex()
{
$config = array(
'class' => 'ddd',
'action'=>'',
'elements' => array(
'<br><br>',
'username' => array(
'label'=>'用户名啊',//注意这里的label
'type' => 'text',
'maxlength' => 32,
'value' => ''
),
'<br><br>',
'password' => array(
'label'=>'昵称啊',//注意这里的label
'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'));
}
}
/**
* @author ryan <yuansir@live.cn/yuansir-web.com>
*/
class playersearchcontroller extends controller
{
public function actionindex()
{
$config = array(
'class' => 'ddd',
'action'=>'',
'elements' => array(
'<br><br>',
'username' => array(
'label'=>'用户名啊',//注意这里的label
'type' => 'text',
'maxlength' => 32,
'value' => ''
),
'<br><br>',
'password' => array(
'label'=>'昵称啊',//注意这里的label
'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'));
}
}
希望本文所述对大家基于yii框架的php程序设计有所帮助。