首頁  >  文章  >  後端開發  >  yii2的ActiveForm表單所使用的方法介紹

yii2的ActiveForm表單所使用的方法介紹

藏色散人
藏色散人轉載
2019-04-12 10:02:403956瀏覽

這篇文章帶給大家的內容是關於yii2的ActiveForm表單使用的方法介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

因為目前專案並非前後端分離模式,且用到PHP的yii2框架(所有html程式碼,js較多內嵌在.php檔案內多少採用同步提交【噴墨中.. .】),遂對於前端面上需要用到的yii2小組件一些整理(因是前端若涉及到php寫法錯誤或者風格問題,敬請指點)

使用場景盡量為表單

基礎註冊呼叫小元件

<?php
  use yii\helpers\Html;
  use yii\widgets\ActiveForm;
?>

  //首先注册activeForm小部件,并赋值给$form(php中的声明变量方法用$ 等价于js中的var let)
  //begin 标志小部件开始
  <?php $form = ActiveForm::begin([
      &#39;id&#39; => &#39;login-form&#39;, //声明小部件的id 即form的id
     //声明需要添加的属性 ,例如class , data-x等
      &#39;options&#39; => [&#39;class&#39; => &#39;form-horizontal&#39;],
  ]) ?>

    //注册完小部件后可以在 activeForm小部件声明块中调用小部件的方法
      <?= $form->field($model, &#39;password&#39;)->passwordInput() ?>

//::end标识小部件结束
<?php ActiveForm::end() ?>

1、先列出activeForm的一些基本方法:

  自訂input框:input();

#  文字框:textInput(); 

  密碼框:passwordInput();

  單選框:radio(),radioList(); 

  複選框:checkbox(), checkboxList();

  下拉框:dropDownList(); 

  多選列表:listBox(); 

  隱藏域:hiddenInput(); 

文字域:textarea(['rows'=>3]); 

  檔案上傳:fileInput(); 

widget擴充2f344a32ace7da85628d1feacf48dca8field($model , 'username')->widget(\yii\widgets\MaskedInput::className(), ['mask' => '9999/99/99',]); ?>

##2、下面我就逐一描述下各個方法的基本呼叫以及如何自訂所需(上述各方法中input之前均是描述的是input標籤的類型)

#2.1 input 文字方塊/密碼方塊以及各指定類型方塊

<?php $form=ActiveForm::begin([&#39;id&#39;=>&#39;login&#39;,&#39;class&#39;=>&#39;login&#39;])?>
    <!-- 简易用法 使用activeForm 的 fiedld方法  -->
    <!-- 其中 该方法下有 
        textInout/passwordInput 等一些常用input类型方法
        hint 输入前的提示内容
        error 错误内容 //一般由后台生成
        label 可以更改label内的内容
        在hint,error,label设置class后将会重置了 这些方法内原来属于容器上的class若需要可以原样赋回去
    -->
    <!-- 这里的
        $mode为跟字段有关的数据模型 ,
        第二个参数为关系模型中的字段不存在将报错,
        第三个参数为模板内的一些内容的进行自定义
    -->
    <?= $form->field($model, &#39;username&#39;,[
        &#39;options&#39;=>[],//数组里面可以设置自需属性
        // template 为字符串模板可自定义模板 ,
        // 其中 {label} {input} {hint} {error} 存在是会调用对应封装好的html模板 当然你也可以不写这样就不会生成yii2内置小部件模板
        &#39;template&#39; => &#39;{label} {input} {hint} {error}&#39;,
        // 以下三个分别可以设置label ,input ,hint,error的属性(都是选填项)
        // 其中如果后面有使用->input...,label(...)等将会将这些里面的配置合并值对应的xxxOptions 内
        &#39;labelOptions&#39; => [
            &#39;class&#39;=>&#39;需要在label上添加的类名&#39;
            //....其他属性集
        ],
        &#39;inputOptions&#39; => [],
        &#39;hintOptions&#39; => [],
        &#39;errorOptions&#39; => [],
    ])->textInput([
        // 在options数组内可以设置任意属性
        &#39;class&#39;=>&#39;testClass&#39;,
        &#39;value&#39;=>&#39;测试&#39;
    ])->hint(
        // 设置提示内容,当只有一个参数切为false(boolean)用于显示提示的标签
        &#39;Please enter your name&#39;,
        [
            // 设置任意属性
            &#39;class&#39; => &#39;testHint&#39;
    ])->label(
        // 设置label显示内容,当只有一个参数切为false(boolean)label标签将不会被渲染
        &#39;Name&#39;,
        [
            // 设置任意属性
            &#39;class&#39; =>&#39;testLabel&#39;
    ])->error([
        // 任意属性,当只有一个参数切为false(boolean)用于显示错误的标签
        &#39;class&#39;=>&#39;errors&#39;
    ]) ?>

    <!-- 可自定义类型input 这里只描述了input的参数  其余参数参考上个示例 -->
    <?= $form->field($model, &#39;username&#39;)->input(
        // input内只允许放置两个参数即[type ,options]
        &#39;email&#39;,//该处为指定type="xxxx"的input类型
        [&#39;class&#39;=>&#39;tests&#39;,&#39;value&#39;=>&#39;值&#39;]//可在内部定义任何属性
    ) ?>
                       
<?php ActiveForm::end();?>

2.2 radio 單選框系列

<?php $form=ActiveForm::begin([&#39;id&#39;=>&#39;login&#39;,&#39;class&#39;=>&#39;login&#39;])?>

    <!-- 
        老实说对这个radio方法相当迷惑  一个单选按钮选择而且一旦选择无法取消,无法一次柑橘属性放置多个值 在有radioList方法的前提下觉得相当鸡肋
        第二个参数中false为是否开启label标签若没开启 labelOption 将无效  ,label设置的值直接显示在容器内
     -->
    <?= $form->field($model, &#39;username&#39;)->radio([
        // 隐藏域中的值
        &#39;uncheck&#39; =>&#39;test1&#39;,
        // 定义lebal的内容
        &#39;label&#39; =>&#39;test&#39;,
        // label上的任意属性
        &#39;labelOptions&#39;=>[
            &#39;gs&#39;=>&#39;test&#39;
        ]
    ],false)?>

    <!-- 
        单选框组 

        若要设置默认值,则在对应控制器中将指定字段设置为 需要选择的值
        $model->username = 1;
    -->

    <?= $form->field($model, &#39;username&#39;)->radioList([
        &#39;0&#39;=>&#39;a&#39;,
        &#39;1&#39;=>&#39;b&#39;,
        &#39;2&#39;=>&#39;c&#39;
    ],[
        // tag声改变 class="radio"的父级标签 若tag设置为h3 
        //    则 <div id="loginform-username" key="testKey" role="radiogroup" aria-required="true"> 
        //    => 转为 <h3 id="loginform-username" key="testKey" role="radiogroup" aria-required="true">
        // <div class="form-group field-loginform-username required">
        //     <label class="control-label">Username</label>
        //     <input type="hidden" name="LoginForm[username]" value="">
        //     <div id="loginform-username" key="testKey" role="radiogroup" aria-required="true">
        //         <div class="radio"><label><input type="radio" name="LoginForm[username]" value="0"> a</label></div>
        //         <div class="radio"><label><input type="radio" name="LoginForm[username]" value="1"> b</label></div>
        //         <div class="radio"><label><input type="radio" name="LoginForm[username]" value="2"> c</label></div>
        //     </div>

        //     <p class="help-block help-block-error"></p>
        // </div>
        &#39;tag&#39;=>&#39;h3&#39;,
        // 未选择是默认提交的值
        &#39;unselect&#39;=>&#39;1&#39;,
        // 如果设置了item选项,则忽略此选项
        &#39;encode&#39;=>false,
        // 每个单选块之间的内容 写的是什么字符串输出就什么字符串
        &#39;separator&#39;=>&#39;&#39;,
        // 定义在每个input单选按钮上的属性
        &#39;itemOptions&#39;=>[
            &#39;tess&#39;=>&#39;jzq&#39;
        ],
      //可调用的回调,可用于自定义与$Item中单个项对应的HTML代码的生成。此回调的签名必须是:函数($index、$Label、$name、$check、$value),
      //其中$index是整个列表中单选按钮的基于零的索引;$Label是单选按钮的标签;$name、$value和$check表示单选按钮输入的名称、值和选中状态。
        &#39;item&#39;=>function($index, $label, $name, $checked, $value){
            // 这块跟encode是在下才疏学浅暂时还未明白啥子用处,待弄明白后在补上,若有码友知晓这块具体作用用法,希望不吝赐教,感激
            // echo $index, $label, $name, $checked, $value;
        },
        // 除此yii2有规定属性之外还可自定义任意属性  且上述属性均不是必填
    ])?>

2.3 checkbox多重選取框系列

<?php $form=ActiveForm::begin([&#39;id&#39;=>&#39;login&#39;,&#39;class&#39;=>&#39;login&#39;])?>

    <!-- 
        checbox方法
        该方法与radio 方法近似就不多说了 直接撸代码 具体可参考 radio
     -->
    <?= $form->field($model, &#39;username&#39;)->checkbox([
        // 隐藏域中的值
        &#39;uncheck&#39; =>&#39;test1&#39;,
        // 定义lebal的内容
        &#39;label&#39; =>&#39;test&#39;,
        // label上的任意属性
        &#39;labelOptions&#39;=>[
            &#39;gs&#39;=>&#39;test&#39;
        ]
    ],true)?>

    <!-- 
        checkboxList方法
        多选框
     -->
     <?= $form->field($model, &#39;username&#39;)->checkboxList([
        &#39;1&#39;=>&#39;篮球&#39;,
        &#39;2&#39;=>&#39;足球&#39;,
        &#39;3&#39;=>&#39;游戏&#39;,
        &#39;4&#39;=>&#39;读书&#39;
    ],[
         // tag声改变 class="radio"的父级标签 若tag设置为h3 
        //    则 <div id="loginform-username" key="testKey" role="radiogroup" aria-required="true"> 
        //    => 转为 <h3 id="loginform-username" key="testKey" role="radiogroup" aria-required="true">
        // <div class="form-group field-loginform-username required">
        //     <label class="control-label">Username</label>
        //     <input type="hidden" name="LoginForm[username]" value="">
        //     <div id="loginform-username" key="testKey" role="radiogroup" aria-required="true">
        //         <div class="radio"><label><input type="radio" name="LoginForm[username]" value="0"> a</label></div>
        //         <div class="radio"><label><input type="radio" name="LoginForm[username]" value="1"> b</label></div>
        //         <div class="radio"><label><input type="radio" name="LoginForm[username]" value="2"> c</label></div>
        //     </div>

        //     <p class="help-block help-block-error"></p>
        // </div>
        &#39;tag&#39;=>&#39;h3&#39;,
        // 未选择是默认提交的值
        &#39;unselect&#39;=>&#39;1&#39;,
        // 如果设置了item选项,则忽略此选项
        &#39;encode&#39;=>false,
        // 每个单选块之间的内容 写的是什么字符串输出就什么字符串,建议如无特殊情况 请忽视该字段
        // &#39;separator&#39;=>&#39;,&#39;,
        // 定义在每个input单选按钮上的属性
        &#39;itemOptions&#39;=>[
            &#39;tess&#39;=>&#39;jzq&#39;
        ],
        // 用于替换html指向函数后若不做操作将会输出空
        // &#39;item&#39;=>function($index, $label, $name, $checked, $value){
            // 这块跟encode是在下才疏学浅暂时还未明白啥子用处,待弄明白后在补上,若有码友知晓这块具体作用用法,希望不吝赐教,感激
            // echo $index, $label, $name, $checked, $value;
        // },
        // 除此yii2有规定属性之外还可自定义任意属性  且上述属性均不是必填
    ])?>

                       
<?php ActiveForm::end();?>

2.4 select下拉清單系列

<?php $form=ActiveForm::begin([&#39;id&#39;=>&#39;login&#39;,&#39;class&#39;=>&#39;login&#39;])?>

    <!-- 
        dropDownList方法
        下拉列表
     -->
     <?= $form->field($model, &#39;username&#39;)->dropDownList([
        //  二维数组直接回报上组标签
        &#39;test&#39;=>[
            &#39;1&#39;=>&#39;篮球&#39;,
            &#39;2&#39;=>&#39;足球&#39;,
        ],
        &#39;3&#39;=>&#39;游戏&#39;,
        &#39;4&#39;=>&#39;读书&#39;
    ],[
        // 设置下拉列表的默认请选择选项
        &#39;prompt&#39;=>[
            &#39;text&#39; => &#39;请选择&#39;, 
            &#39;options&#39; => [&#39;value&#39; => &#39;none&#39;, &#39;class&#39; => &#39;prompt&#39;, &#39;label&#39; => &#39;Select&#39;]
        ],
        &#39;encode&#39;=>false,
        // 对select option的设置条件以及更改内容
        &#39;options&#39;=>[
            // 设置禁止选择项
            &#39;2&#39; => [&#39;disabled&#39; => true],
            //替换或者追加指定key的内容,实际上原内容还在只是假设了 label 属性 和显示了 label的属性值
            &#39;4&#39; => [&#39;label&#39; => &#39;value 2&#39;],
        ],
        &#39;encodeSpaces&#39;=>true
        // 除此yii2有规定属性之外还可自定义任意属性  且上述属性均不是必填
    ])?>

    

        
                       
<?php ActiveForm::end();?>

#2.5 widget 小工具

<?php $form=ActiveForm::begin([&#39;id&#39;=>&#39;login&#39;,&#39;class&#39;=>&#39;login&#39;])?>

    <!-- 
        小部件
        用于强制输入正确内容的input部件
     -->
    <?= $form->field($model, &#39;username&#39;,[
        &#39;template&#39;=>&#39;<h2>test</h2> {label} {input} {error}&#39;
    ])->widget(\yii\widgets\MaskedInput::className(), [
        // 指定input类型
        // &#39;type&#39;=>&#39;time&#39;,
        // 指定必须输入的类型
        &#39;mask&#39; => &#39;999-9999-9999&#39;,
        &#39;options&#39;=>[&#39;class&#39; => &#39;form-control test&#39;]
    ]); ?>

    <!-- 
        用于生成带图片验证的input小部件
        -->
    <?= $form->field($model, &#39;verifyCode&#39;)->widget(Captcha::className(), [
        &#39;captchaAction&#39; => &#39;login/captcha&#39;,
        &#39;options&#39; => [
            &#39;class&#39; => &#39;two&#39;,
            &#39;id&#39;=>&#39;two&#39;,
            &#39;placeholder&#39; => &#39;请输入验证码&#39;,
        ],
        &#39;template&#39; => &#39;{input}{image}&#39;,
        &#39;imageOptions&#39; => [
            &#39;alt&#39; => &#39;images&#39;,
        ]
    ])?>

  --------------------- 最后一个并未实测   -------------------------------
<!-- 自定义小部件 需在widget文件定义源文件 --> <?= $form->field($model, &#39;username&#39;)->widget(&#39;WidgetClassName&#39;, [ // configure additional widget properties here ]) ?> <?php ActiveForm::end();?>

## 以上是這段時間使用的一篇小總結如有用法錯誤敬請指點###

以上是yii2的ActiveForm表單所使用的方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除