PHP yii框架FormWidget组件
本篇文章介绍的是PHP yii框架Form组件,方便在view层更好调用此功能,话不多说上代码:
1、先继承yii本身Widget类
<?php
/**
* User: lsh
*/ namespace system\widgets; use system\helpers\SysHelper;
use yii\base\InvalidCallException;
use yii\helpers\ArrayHelper; class Widget extends \yii\base\Widget
{
/**
* 标题
*/
public $title; /**
* 名称
*/
public $name; /**
* 提示
*/
public $placeholder; /**
*
*/
public $readonly; /**
* css class
*/
public $cssClass; /**
* css style
*/
public $cssStyle; /**
* 控件属性数组
*/
public $attr = []; public function init()
{
// $this->initAttr();
} protected function initAttr()
{
//初始化属性
$this->attr = ArrayHelper::merge($this->attr, [
'id' => $this->name,
'name' => $this->name,
'placeholder' => $this->placeholder,
'class' => $this->cssClass,
'style' => $this->cssStyle,
'readonly' => $this->readonly
]);
} /**
* 把属性数组转为html
* @param $attr array 属性数组
* @return string
*/
public function attrToHtml($attr = [])
{
$html = '';
if (is_array($attr)) {
foreach ($attr as $k => $v) {
if ($v !== null && $v !== '') {
//处理布尔型
if (is_bool($v)) {
$v = $v ? 'true' : 'false';
}
$html .= $k . '=\'' . $v . '\' ';
}
}
}
return $html;
}
}
2、重写及继承Widget类
<?php namespace system\widgets; use system\helpers\StringHelper;
use yii\base\ErrorHandler;
use system\helpers\SysHelper; class FormWidget extends Widget
{
/**
* 父form
*/
public $form; /**
* 布局方式
* inline-内联
* horizontal-水平
*/
public $inline;
public $horizontal; /**
* 纵向布局元素栅格参数
*/
public $colClass = 6; /**
* css class
* 默认添加 form-control
*/
public $cssClass = 'form-control'; /**
* 提示
*/
public $tips; /**
* 标题加粗
*/
public $titleBorder = true; /**
* suffix 后缀
*/
public $suffix; /*
* 字段 Label 列宽度
*/
public $colWidth; public function init()
{
if ($this->form) {
$this->inline = $this->form->inline;
$this->horizontal = $this->form->horizontal;
} parent::init();
} /**
* form列布局
* @param $widgetHtml string
* @return string
*/
public function html($widgetHtml)
{
$html = '';
if ($this->title) {
if (property_exists($this, 'validate') && strrpos($this->validate, 'required')) {
$this->title = '<span class="text-red">*</span>' . $this->title;
}
$labelClass = 'control-label';
if ($this->horizontal) {
if(is_numeric($this->horizontal)){
if($this->colWidth){
$labelClass .= ' col-md-'.$this->colWidth;
}else{
$labelClass .= ' col-md-'.$this->horizontal;
} }
else{
$labelClass .= ' col-md-2';
}
}
$html = StringHelper::strFormat('<label class="{1}">{0}:</label>', $this->title, $labelClass);
} if ($this->inline) {
$html .= $widgetHtml;
} elseif ($this->horizontal) {
if($this->suffix){
$html .= StringHelper::strFormat('<div class="{1}">{0}</div><div class="col-md-1">{2}</div>', $widgetHtml, 'col-md-' . $this->colClass, $this->suffix);
}
else{
$html .= StringHelper::strFormat('<div class="{1}">{0}</div>', $widgetHtml, 'col-md-' . $this->colClass);
}
}
else {
$html .= StringHelper::strFormat('<div class="row"><div class="{1}">{0}</div></div>', $widgetHtml, 'col-md-' . $this->colClass);
} if ($this->tips)
$html .= StringHelper::strFormat('<span class="help-block">{0}</span>', $this->tips); if ($this->form)
$html = StringHelper::strFormat('<div class="form-group">{0}</div>', $html); return $html;
} /**
* 子类重构方法,设置控件html
*/
public function getWidget()
{
} /**
* 渲染控件
*/
public function renderItem()
{
$this->initAttr();
$widgetHtml = $this->getWidget();
$html = $this->html($widgetHtml); return $html;
} /**
* 魔术方法, 以string形式返回控件
*/
public function __toString()
{
return $this->run();
} /**
* widget()方法自动调用
*/
public function run()
{
try {
return $this->renderItem();
} catch (\Exception $e) {
ErrorHandler::convertExceptionToError($e);
return '';
}
} public function width($width)
{
if (!empty($this->cssStyle)) {
$this->cssStyle .= ';';
}
$this->cssStyle .= 'width:' . $width . 'px'; return $this;
} public function tips($tips)
{
$this->tips = $tips;
return $this;
} public function inline()
{
$this->inline = true;
return $this;
} public function horizontal()
{
$this->horizontal = true;
return $this;
} public function suffix($suffix){
$this->suffix = $suffix;
return $this;
} public function colWidth($colWidth){
$this->colWidth = $colWidth;
} }
3、以Radio为例,先创建Radio类,继承FormWidget
<?php
namespace system\widgets; use system\helpers\StringHelper;
use system\helpers\SysHelper;
use yii\helpers\ArrayHelper; class Radios extends FormWidget
{
/**
* @var 值
*/
public $value;
/**
* @var 描述
*/
public $desc;
/**
* @var 是否选中
*/
public $isChecked; public function getWidget()
{
$this->attr = ArrayHelper::merge($this->attr, [
'type' => 'radio',
'class' => '',
'value' => $this->value,
'desc' => $this->desc
]);
$attr = $this->attrToHtml($this->attr);
return StringHelper::strFormat('<input {0} {1}/> {2}', $attr, ($this->isChecked ? 'checked' : ''), $this->desc);
} }
在Form类里引入就可以了,如下所示
<?php
namespace system\widgets; use system\helpers\StringHelper;
use Yii;
use yii\helpers\ArrayHelper; class Form extends Widget
{
/**
* Radios类
*/
private $radioClass = 'system\widgets\Radios';
/**
* @param $title string 标题
* @param $name string 控件名称
* @param $desc string 描述
* @param $value string 值
* @param $options array widget radios属性
* @param $isChecked bool 是否选中
* @return Radios|string
*/
public function radios($title, $desc, $name, $value = '0', $isChecked = false, $options = [])
{
return Yii::createObject(ArrayHelper::merge($options, [
'class' => $this->radioClass,
'title' => $title,
'desc' => $desc,
'name' => $name,
'value' => $value,
'isChecked' => $isChecked,
'form' => $this,
]));
}
}
估计有的童鞋看到了一个
StringHelper::strFormat方法,代码如下
/**
* 格式输出字符串
* */
public static function strFormat()
{
$args = func_get_args();
$format = array_shift($args); preg_match_all('/(?=\{)\{(\d+)\}(?!\})/', $format, $matches, PREG_OFFSET_CAPTURE);
$offset = 0;
foreach ($matches[1] as $data) {
$i = $data[0];
$format = substr_replace($format, @$args[$i], $offset + $data[1] - 1, 2 + strlen($i));
$offset += strlen(@$args[$i]) - 2 - strlen($i);
} return $format;
}
4、在view里直接实例化Form类,调用此方法就可以了
<?= $form->radios() ?>
欢迎小伙伴们前来讨论!
PHP yii框架FormWidget组件的更多相关文章
- yii框架的理解
Yii Framework是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web 2.0应用开发所需要的几乎一切功能.Yii是最有效率的PHP框架之一. yii框架里 ...
- Yii 框架学习--01 框架入门
Yii 是一个高性能的,适用于开发 WEB2.0 应用的 PHP 框架. Yii目前有两个主要的版本: 2.0 和 1.1.本文以YII 2.0.7为例. 环境需求 Yii2.0 框架有一些系统上的需 ...
- yii框架详解 之 CWebApplication 运行流程分析
在 程序入口处,index.php 用一句 Yii::createWebApplication($config)->run(); 开始了app的运行. 那么,首先查看 CWebApplicat ...
- yii框架
Yii Framework是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web 2.0应用开发所需要的几乎一切功能.Yii是最有效率的PHP框架之一.Yii是创始人 ...
- YII框架源码分析(百度PHP大牛创作-原版-无广告无水印)
YII 框架源码分析 百度联盟事业部——黄银锋 目 录 1. 引言 3 1.1.Yii 简介 3 1.2.本文内容与结构 3 2.组件化与模块化 4 2.1.框架加载和运行流程 4 ...
- Yii框架中集成phprpc、hprose
在项目开发的过程中有时候会涉及到对外提供接口供第三方程序调用或者是不同程序间需要相互通信,那么最通用的做法是用传统的SOAP方式来实现,用XML的文档格式来作为传输载体.但是这种方式不灵活,支持的数据 ...
- Yii框架tips(转)
yii的一些小的技巧 http://www.yiichina.com/topic/151 db组件 'schemaCachingDuration'=>3600, 为什么不起做用?需要开缓存 如何 ...
- Yii框架tips
db组件 'schemaCachingDuration'=>3600, 为什么不起做用?需要开缓存 如何在页面下边显示sql的查询时间在log组件的routes中加入 array('class' ...
- YII框架开发一个项目的通用目录结构
YII框架开发一个项目的通用目录结构: 3 testdrive/ 4 index.php Web 应用入口脚本文件 5 assets/ 包含公开的资源文件 6 css/ 包含 CSS 文件 7 ima ...
随机推荐
- 【Java】 剑指offer(11) 矩阵中的路径
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字 ...
- 关于postman各功能的说明及用法以及批量执行
这玩意功能还不错,可以学学,在测试接口或者配合写代码测接口时是有帮助作用的.今天也去打听了一下,一下我就做了一下记录. 首先,主界面: 分开记录,写的详细一些. 左侧菜单栏: 主菜单(请求部分); 输 ...
- Beta(7/7)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- [Doc]MongoDB用户创建与启用access-control
文档链接:https://docs.mongodb.com/manual/tutorial/enable-authentication/ Pre 个人总感觉数据库的文档结构不太友好, 不太解决问题.以 ...
- Python基础——切片实例
切片实例 L = list(range(100)) print(L, end=' ') [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1 ...
- Hibernate Session对象核心方法
1. 持久化对象的状态: 站在持久化的角度,Hibernate 把对象分为四种状态:持久化状态,临时状态,游离状态,删除状态 Session 的特定方法能使对象从一个状态转到另一个状态 临时对象: 在 ...
- vue全选反选demo
<template> <div> <div class="xuanze"> <label><input type=" ...
- “扩展域”与"边带权"的并查集
https://www.luogu.org/problemnew/show/P1196 银河英雄传说 #include<bits/stdc++.h> using namespace std ...
- laravel整合workerman做聊天室
测试工具 http://www.blue-zero.com/WebSocket/ 2018年8月6日17:28:24 <?php namespace App\Console\Commands; ...
- [Day22]IO(File、递归)
1.File 1.1 IO概述 (1)当需要把内存中的数据存储到持久化设备上的这个动作称为输出(写)Output操作 (2)当把持久设备上的数据读取到内存中的这个动作称为输入(读)Input操作 1. ...