YII2完整的表單組件使用

 1.普通的input框:php

<?= $form->field($model,'licenseNo',[html

      'options'=>['class'=>'Net-content-tab-div clearfix'],web

      'labelOptions'=>['class'=>'','for'=>'N-yyzz','label'=>'<span class=\'red\'>*</span><span class="is-red">營業執照號</span>'],yii

      'errorOptions'=>['class'=>'notice-warn'],ui

      'template'=>'{label}{input}{error}'spa

  ])->textInput([orm

      'class'=>'tab-divbox-input2',htm

      'placeholder'=>'請填寫營業執照號',get

      'id'=>'N-yyzz',input

      'maxlength'=>30,

])
?>
生成的html:

<!--營業執照號-->

<div class="Net-content-tab-div clearfix field-N-yyzz required has-error">

<label class="" for="N-yyzz"><span class="red">*</span><span class="is-red">營業執照號</span></label>

<input id="N-yyzz" class="tab-divbox-input2" name="OpenModel[licenseNo]" maxlength="30" placeholder="請填寫營業執照號" type="text">

<div class="notice-warn" style="display: block;">營業執照號必須填寫</div>

</div>

2.<select>型的表單組件
 <?=$form->field($model,'storeType',[

      'options'=>['class'=>'slectdiv'],

      'labelOptions'=>['class'=>'Net-content-tab-select'],

      'errorOptions'=>['class'=>'notice-warn'],

      'template'=>'{label}{input}<div>{error}</div>'

 ])->dropDownList($category,[

      'class'=>'Net-content-tab-select',

      'id'=>'select1'

])->label(false);

?>
生成的html:

<div class="slectdiv field-select1 required">

    <select id="select1" class="Net-content-tab-select" name="OpenModel[storeType]">

        <option value="">請選擇</option>

        <option value="專營店">專營店</option>

        <option value="直營店">直營店</option>

        <option value="旗艦店">旗艦店</option>

    </select>
    <div><div class="notice-warn" style="display: none;"></div></div>

</div>

3.radio型的表單組件
 <?php echo $form->field($model, 'categoryId',[

    'options'=>['class'=>'slectdiv'],

    'labelOptions'=>['class'=>'Net-content-tab-div-label'],

    'errorOptions'=>['class'=>'notice-warn'],

    'template'=>'{label}{input}<div>{error}</div>'

])->radioList($categoryItem)->label(false)
?>
生成的html:
<div class="selectdiv">
    <input name="OpenModel[categoryId]" value="" type="hidden">
    <div id="openmodel-categoryid">
        <label><input name="OpenModel[categoryId]" value="1" type="radio"> 家居家裝</label>
    </div>
    <div><div class="notice-warn" style="display: none;"></div></div>
</div>

4.驗證碼組件的使用
 <!-- 圖形驗證碼 -->

<?= $formOne

->field($model, 'captcha',[

'options'=>['class'=>'formdiv clearfix'],

'labelOptions'=>['class'=>'formone','label'=>'驗證碼: '],

'errorOptions'=>['class'=>'notice-warn'],

'template'=>'{label}{input}{error}'

])

->widget(Captcha::className(), [

'options'=>['class'=>'notice-con-formBox-inputStyle3'],

'template' => '{input}{image}',

'captchaAction'=>[

'/member/account/captcha'

]

]) ?>
控制器:
 public function actions()

{

return [

'error' => [

'class' => 'yii\web\ErrorAction',

],

// 新添加的

'captcha' => [

'class' => 'yii\captcha\CaptchaAction',

'backColor' => 0xFFFFFF,

'foreColor' => 0x2040A0,

'height' => '30',

'width'=>'80',

'minLength' => 4,

'maxLength' => 4,

'offset' => 3,

],

];

}

相關文章
相關標籤/搜索