Yii2 ActiveForm表单自定义样式

实例:

 <?php

            $form = ActiveForm::begin([
                 ‘fieldConfig‘ => [
                    ‘template‘ => ‘<div class="col-lg-3 control-label color666 fontweight">{label}:</div>
                                                    <div class="col-lg-5" style="padding-left: 15px;padding-right: 15px;">{input}</div>
                                                    <div class="col-lg-4">{error}</div>‘,
                    ‘inputOptions‘ => [‘class‘ => ‘form-control‘],
                ],
                ‘options‘ => [‘class‘ => ‘form-horizontal t-margin20‘,‘id‘=>‘form1‘,‘enctype‘=>"multipart/form-data"],
                ]);
            //\app\helpers\Util::dump($form);
/*打印此处$form   ActiveForm::begin([]) 追踪源码,调用父级的widget的begin方法是实例化ActiveForm  This method creates an instance of the calling class
*/
?>
/*
自定义样式
$form->field()
调用该方法 查看源码 注释写的 options配置是 ActiveField These are properties of [[ActiveField]] 查看 ActiveField  的属性 就知道可以修改哪些样式
/**
     * Generates a form field.
     * A form field is associated with a model and an attribute. It contains a label, an input and an error message
     * and use them to interact with end users to collect their inputs for the attribute.
     * @param Model $model the data model
     * @param string $attribute the attribute name or expression. See [[Html::getAttributeName()]] for the format
     * about attribute expression.
     * @param array $options the additional configurations for the field object. These are properties of [[ActiveField]]
     * or a subclass, depending on the value of [[fieldClass]].
     * @return ActiveField the created ActiveField object
     * @see fieldConfig
     */
    public function field($model, $attribute, $options = [])
    {
        $config = $this->fieldConfig;
        if ($config instanceof \Closure) {
            $config = call_user_func($config, $model, $attribute);
        }
        if (!isset($config[‘class‘])) {
            $config[‘class‘] = $this->fieldClass;
        }
        return Yii::createObject(ArrayHelper::merge($config, $options, [
            ‘model‘ => $model,
            ‘attribute‘ => $attribute,
            ‘form‘ => $this,
        ]));
    }
*/

<?=$form->field($model, ‘product_name‘,[‘labelOptions‘ => [‘class‘ => ‘t-r-pd5‘],‘options‘=>[‘class‘=>‘‘]])->textInput()?>
ActiveField 部分代码:
  1 <?php
  2 /**
  3  * @link http://www.yiiframework.com/
  4  * @copyright Copyright (c) 2008 Yii Software LLC
  5  * @license http://www.yiiframework.com/license/
  6  */
  7
  8 namespace yii\widgets;
  9
 10 use Yii;
 11 use yii\base\Component;
 12 use yii\base\ErrorHandler;
 13 use yii\helpers\ArrayHelper;
 14 use yii\helpers\Html;
 15 use yii\base\Model;
 16 use yii\web\JsExpression;
 17
 18 /**
 19  * ActiveField represents a form input field within an [[ActiveForm]].
 20  *
 21  * @author Qiang Xue <[email protected]>
 22  * @since 2.0
 23  */
 24 class ActiveField extends Component
 25 {
 26     /**
 27      * @var ActiveForm the form that this field is associated with.
 28      */
 29     public $form;
 30     /**
 31      * @var Model the data model that this field is associated with
 32      */
 33     public $model;
 34     /**
 35      * @var string the model attribute that this field is associated with
 36      */
 37     public $attribute;
 38     /**
 39      * @var array the HTML attributes (name-value pairs) for the field container tag.
 40      * The values will be HTML-encoded using [[Html::encode()]].
 41      * If a value is null, the corresponding attribute will not be rendered.
 42      * The following special options are recognized:
 43      *
 44      * - tag: the tag name of the container element. Defaults to "div".
 45      *
 46      * If you set a custom `id` for the container element, you may need to adjust the [[$selectors]] accordingly.
 47      *
 48      * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
 49      */
 50     public $options = [‘class‘ => ‘form-group‘];
 51     /**
 52      * @var string the template that is used to arrange the label, the input field, the error message and the hint text.
 53      * The following tokens will be replaced when [[render()]] is called: `{label}`, `{input}`, `{error}` and `{hint}`.
 54      */
 55     public $template = "{label}\n{input}\n{hint}\n{error}";
 56     /**
 57      * @var array the default options for the input tags. The parameter passed to individual input methods
 58      * (e.g. [[textInput()]]) will be merged with this property when rendering the input tag.
 59      *
 60      * If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly.
 61      *
 62      * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
 63      */
 64     public $inputOptions = [‘class‘ => ‘form-control‘];
 65     /**
 66      * @var array the default options for the error tags. The parameter passed to [[error()]] will be
 67      * merged with this property when rendering the error tag.
 68      * The following special options are recognized:
 69      *
 70      * - tag: the tag name of the container element. Defaults to "div".
 71      * - encode: whether to encode the error output. Defaults to true.
 72      *
 73      * If you set a custom `id` for the error element, you may need to adjust the [[$selectors]] accordingly.
 74      *
 75      * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
 76      */
 77     public $errorOptions = [‘class‘ => ‘help-block‘];
 78     /**
 79      * @var array the default options for the label tags. The parameter passed to [[label()]] will be
 80      * merged with this property when rendering the label tag.
 81      * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
 82      */
 83     public $labelOptions = [‘class‘ => ‘control-label‘];
 84     /**
 85      * @var array the default options for the hint tags. The parameter passed to [[hint()]] will be
 86      * merged with this property when rendering the hint tag.
 87      * The following special options are recognized:
 88      *
 89      * - tag: the tag name of the container element. Defaults to "div".
 90      *
 91      * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
 92      */
 93     public $hintOptions = [‘class‘ => ‘hint-block‘];
 94     /**
 95      * @var boolean whether to enable client-side data validation.
 96      * If not set, it will take the value of [[ActiveForm::enableClientValidation]].
 97      */
 98     public $enableClientValidation;
 99     /**
100      * @var boolean whether to enable AJAX-based data validation.
101      * If not set, it will take the value of [[ActiveForm::enableAjaxValidation]].
102      */
103     public $enableAjaxValidation;
104     /**
105      * @var boolean whether to perform validation when the value of the input field is changed.
106      * If not set, it will take the value of [[ActiveForm::validateOnChange]].
107      */
108     public $validateOnChange;
109     /**
110      * @var boolean whether to perform validation when the input field loses focus.
111      * If not set, it will take the value of [[ActiveForm::validateOnBlur]].
112      */
113     public $validateOnBlur;
114     /**
115      * @var boolean whether to perform validation while the user is typing in the input field.
116      * If not set, it will take the value of [[ActiveForm::validateOnType]].
117      * @see validationDelay
118      */
119     public $validateOnType;
120     /**
121      * @var integer number of milliseconds that the validation should be delayed when the user types in the field
122      * and [[validateOnType]] is set true.
123      * If not set, it will take the value of [[ActiveForm::validationDelay]].
124      */
125     public $validationDelay;
126     /**
127      * @var array the jQuery selectors for selecting the container, input and error tags.
128      * The array keys should be "container", "input", and/or "error", and the array values
129      * are the corresponding selectors. For example, `[‘input‘ => ‘#my-input‘]`.
130      *
131      * The container selector is used under the context of the form, while the input and the error
132      * selectors are used under the context of the container.
133      *
134      * You normally do not need to set this property as the default selectors should work well for most cases.
135      */
136     public $selectors = [];
137     /**
138      * @var array different parts of the field (e.g. input, label). This will be used together with
139      * [[template]] to generate the final field HTML code. The keys are the token names in [[template]],
140      * while the values are the corresponding HTML code. Valid tokens include `{input}`, `{label}` and `{error}`.
141      * Note that you normally don‘t need to access this property directly as
142      * it is maintained by various methods of this class.
143      */
144     public $parts = [];
时间: 2024-10-15 16:20:14

Yii2 ActiveForm表单自定义样式的相关文章

表单自定义样式

总结自定义表单样式的控件,涉及到一些css伪类伪元素知识 css组合选择符 css组合选择符在自定义表单控件中扮演着连接的作用,便于控制元素样式,css3中有四种组合选择符. 后代选择器 :以空格分离,选择后代相匹配的元素:#div p 选择的是#div下所有的p元素 子元素选择器 :以大于号分离,选择子元素相匹配的元素:#div>p 选择的是#div下直接子元素为p元素 相邻兄弟选择器 :以加号分离,选择紧接的元素,二者有相同父级,#div+p选择的是#div相邻的p,只是第一个,因为第一个相

封装表单自定义错误信息。(阻止默认错误信息提示,添加自定义错误信息提示)

1 前台提交信息到后台,两种表单验证: 2 1,form 表单验证 3 2,ajax 无刷新页面提交 4 5 表单验证方法一般有: 6 1,浏览器端验证 7 2,服务器端验证 8 3,ajax验证 9 4,浏览器和服务器双重验证 10 11 html5表单新增类型: 12 email,url,number,range,data(date,month,week,time,datetime,datetime-local),search,color,tel等 13 ======== 14 重点:pat

BPM实例分享:如何设置表单字体样式

系统版本:V10.0 一些业务场景中,时尔需要改变表单字体 那如何设置表单字体样式? 本文将会针对全局表单和单个表单进行阐述! 1.全局表单:  修改WFRES\CSS\MvcSheet.css ,在body节点里面增加 font-family:"字体名" !important;  2.单个表单: 添加以下样式body{    font-family:"字体名" !important;  } PS:若设置后没有变化,按F12,并查看控制整个页面的div,找到相应的C

jQuery_review之一行语句搞定表单焦点样式

众所周知,各种浏览器对于HTML.CSS以及原生JS的支持不尽相同.但是jQuery很好地封装了各种浏览器不同的实现,能够很好地解决跨浏览器的CSS问题.下面就是在review表单操作的时候的一个DEMO,记录在这个地方,方便后面做项目的时候查找使用. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="

yii2 创建ActiveForm(表单)

表单的生成表单中的方法 ActiveForm::begin()方法    ActiveForm::end()方法    getClientOptions()方法    其它方法:errorSummary.validate.validateMultiple 表单中的参数 表单form自身的属性    表单中各个项(field)输入框相关的属性        $fieldConfig        关于验证的属性        关于每个field容器样式的属性    ajax验证    前端js事件

yii2.0表单自带验证码

Yii2.0的自带的验证依赖于GD2或者ImageMagick扩展. 使用步骤如下: 第一步,控制器: 在任意controller里面重写方法 代码折叠,点击查看 <?php namespace frontend\controllers; use Yii; use app\models\login; use app\models\search\UserSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; use yi

表格表单及样式重置、特性

1.表格 用table标签,标签 table的标签基本特性是 display:table; <thead> 表格头部 <tbody> 表格主体 <tfoot> 表格尾部 <tr>行 <th>表格标题 <td>单元格 一般只用<tbody> colspan 属性规定单元格可横跨的列数 只能放在th,td标签中<th colspan='2'> rowspan 属性规定单元格可横跨的行数 只能放在td标签中<t

BootStrap的table表格,栅格系统,form表单的样式

BootStrap BootStrap的简介 1.    什么是Bootstrap 由两个前端设计师开发的一个前端的框架(Html,css,js) 简化了程序员写css的代码 2.    为什么使用BootStrap,有什么特点 l  学习比较简单,有了Html,css和js就能学习 l  响应式布局,可以适应多种的设备 l  移动设备优先 BootStrap的使用 环境的安装 官网上下载包 要想使用bootStrap 必须加载jquery bootStrap的基本模板 1.     Html文

Yii2提交表单提示无法验证

yii2使用gii生成的搜索视图里的表单使用的是get方式,我改为post就提示无法验证,以为是控制器默认访问是get,实际默认是get和post都可以 public function behaviors() { return [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['post'], ], ], ]; } 之所以提示无法验证是因为对于post请求,是有一个CSRF验