Introduction to the methods of using yii2's ActiveForm form

藏色散人
Release: 2023-04-05 19:52:01
forward
3954 people have browsed it

This article brings you an introduction to the method of using the ActiveForm form of yii2. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Because the current project is not in front-end and back-end separation mode, and uses PHP's yii2 framework (all html codes and js are mostly embedded in .php files, they are submitted more or less synchronously [Inkjet... .]), so I will sort out the yii2 widgets that need to be used on the front end (because if the front end involves PHP writing errors or style issues, please give me some advice)

Use scenarios try to use forms

Basic registration call widget

<?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() ?>
Copy after login

1. First, list some basic methods of activeForm:

Custom input box: input();

Text box :textInput();

Password box: passwordInput();

Radio button: radio(),radioList();

Check box: checkbox(), checkboxList();

Drop-down box: dropDownList();

Multi-select list: listBox();

Hidden field: hiddenInput();

Text area: textarea(['rows'=>3]);

File upload: fileInput();

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

2. Below I will describe the basic calls of each method one by one and how to customize what is needed (the type of input tag is described before input in each of the above methods)

##2.1 input text box/password box and each specified type box

<?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();?>
Copy after login

2.2 radio radio button series

<?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有规定属性之外还可自定义任意属性  且上述属性均不是必填
    ])?>
Copy after login

2.3 checkbox multi-select box series

<?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();?>
Copy after login

2.4 select drop-down list series

<?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();?>
Copy after login

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();?>
Copy after login

The above are used during this period A small summary, please give me some pointers if there are any usage errors

The above is the detailed content of Introduction to the methods of using yii2's ActiveForm form. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!