加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP yii框架FormWidget组件

发布时间:2020-05-25 23:02:03 所属栏目:PHP 来源:互联网
导读:本篇文章介绍的是PHP yii框架Form组件,方便在view层更好调用此功能,话不多说上代码:1、先继承yii本身Widget类?php/*** User: lsh*/namespace systemwidgets;use systemhelpersSysHelper;use yiibaseInvalidCallException;use yiihelpersArrayHelper;class

本篇文章介绍的是PHP yii框架Form组件,方便在view层更好调用此功能,话不多说上代码:1、先继承yii本身Widget类
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类
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 = '' . $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('
attr = ArrayHelper::merge($this->attr,[            'type' => 'radio',            'class' => '',            'value' => $this->value,            'desc' => $this->desc        ]);        $attr = $this->attrToHtml($this->attr);        return StringHelper::strFormat(' {2}',$attr,($this->isChecked ? 'checked' : ''),$this->desc);    }}在Form类里引入就可以了,如下所示

    /**     * Radios类     */     private $radioClass = 'systemwidgetsRadios';
    /**     * @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类,调用此方法就可以了
radios() ?>欢迎小伙伴们前来讨论!

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读