日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Yii2.0 ActiveForm Input Fields

發布時間:2023/11/29 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Yii2.0 ActiveForm Input Fields 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

之前5月學習Yii2的時候發現的一個不錯的博客內容,這里轉載保存。

  • Use the namespace For ActiveForm
  • Active Form Begin And End
  • Text Input Field
  • TextArea Field
  • Password Input Field
  • HTML5 Email Input Field
  • File Upload
  • Checkbox Button Field
  • Checkbox List Input Field
  • Radio Button Field
  • Radio Button List Field
  • ListBox Field
  • dropDown List Input Field
  • Submit Button
  • ‘yii\widgets\ActiveForm’ class is used to create a form and ‘yii\helpers\Html’ class is used to display the different type of HTML input fields like buttons, textbox, select box etc.

    ActiveForm::begin() - creates a form instance and beginning of the form. ActiveForm::begin() and ActiveForm::end() - All of the content placed between this.

    Use the namespace For ActiveForm

    1 <?php
    2 use?yii\helpers\Html;
    3 use?yii\widgets\ActiveForm;
    4 ?>

    ‘ActiveForm’ namespace is very important to create the a active form and ‘Html’ namespace is very useful to display the different html input fields.

    Active Form Begin And End

    01 <?php
    02 use?yii\helpers\Html;
    03 use?yii\widgets\ActiveForm;
    04 ?
    05 //$form = ActiveForm::begin(); //Default Active Form begin
    06 $form?= ActiveForm::begin([
    07 ????'id'?=>?'active-form',
    08 ????'options'?=> [
    09 ????????????????'class'?=>?'form-horizontal',
    10 ????????????????'enctype'?=>?'multipart/form-data'
    11 ????????????????],
    12 ])
    13 /* ADD FORM FIELDS */
    14 ActiveForm::end();
    15 ?>

    Here we added active form with basic details like form id, class and enctype for file uploads.

    Text Input Field

    1 //Format 1
    2 <?=?$form->field($model,'name'); ?>
    3 //Format 2
    4 <?=?$form->field($model,?'name')->textInput()->hint('Please enter your name')->label('Name') ?>

    Format 1 is a normal text input field. Format 2 is a text input field with hint, label.

    TextArea Field

    The model attribute value will be used as the content in the textarea.

    1 <?=?$form->field($model,?'desc')->textarea(); ?>
    2 <?=?$form->field($model,?'desc')->textarea()->label('Description'); ?>
    3 <?=?$form->field($model,?'desc')->textarea(array('rows'=>2,'cols'=>5)); ?>

    Password Input Field

    1 //Format 1
    2 <?=?$form->field($model,?'password')->input('password') ?>
    3 //Format 2
    4 <?=?$form->field($model,?'password')->passwordInput() ?>
    5 //Format 3
    6 <?=?$form->field($model,?'password')->passwordInput()->hint('Password should be within A-Za-z0-9')->label('Password Hint') ?>

    We added different type of password input field like password with hint, custom lable.

    HTML5 Email Input Field

    1 <?=?$form->field($model,?'email')->input('email') ?>

    File Upload

    fileInput() function is used to create a file input fields and ‘multiple’ parameter is used to upload multiple file in single upload.

    Single File Upload

    1 <?=?$form->field($model,?'uploadFile')->fileInput() ?>

    MultiFile Upload

    1 <?php?echo?$form->field($model,?'uploadFile[]')->fileInput(['multiple'=>'multiple']); ?>

    Checkbox Button Field

    Using below we can create the Checkbox base on model attribute of yii2.0 framework. We added the following options like custom label, disabled, style etc

    01 <!-- CHECKBOX BUTTON DEFAULT LABEL -->
    02 <?=?$form->field($model,?'population')->checkbox(); ?>
    03 <!-- CHECKBOX BUTTON WITHOUT LABEL -->
    04 <?=?$form->field($model,?'population')->checkbox(array('label'=>'')); ?>
    05 <!-- CHECKBOX BUTTON WITH CUSTOM LABEL -->
    06 <?=?$form->field($model,?'population')??? ->checkbox(array('label'=>''))
    07 ????????????????????????????????????????->label('Gender'); ?>
    08 <!-- CHECKBOX BUTTON WITH LABEL OPTIONS, DISABLED AND STYLE PROPERTIES -->
    09 <?=?$form->field($model,?'population')->checkbox(array(
    10 ????????????????????????????????'label'=>'',
    11 ????????????????????????????????'labelOptions'=>array('style'=>'padding:5px;'),
    12 ????????????????????????????????'disabled'=>true????????????????????????????
    13 ????????????????????????????????))
    14 ????????????????????????????????->label('Gender'); ?>

    Checkbox List Input Field

    checkboxList() function is used to display the check box list using array of input argument values.

    1 <?php?echo?$form->field($model,?'name[]')->checkboxList(['a'?=>?'Item A',?'b'?=>?'Item B',?'c'?=>'Item C']); ?>

    Radio Button Field

    The model attribute value will be used to create the redio button.

    01 <!-- RADIO BUTTON DEFAULT LABEL -->
    02 <?=?$form->field($model,?'gender')->radio(); ?>
    03 <!-- RADIO BUTTON WITHOUT LABEL -->
    04 <?=?$form->field($model,?'gender')->radio(array('label'=>'')); ?>
    05 <!-- RADIO BUTTON WITH CUSTOM LABEL -->
    06 <?=?$form->field($model,?'gender')??? ->radio(array('label'=>''))
    07 ????????????????????????????????????????->label('Gender'); ?>
    08 <!-- RADIO BUTTON WITH LABEL OPTIONS -->
    09 <?=?$form->field($model,?'gender')->radio(array(
    10 ????????????????????????????????'label'=>'',
    11 ????????????????????????????????'labelOptions'=>array('style'=>'padding:5px;')))
    12 ????????????????????????????????->label('Gender'); ?>

    Radio Button List Field

    The model attribute value will be used to create the redio button list.

    1 <?=?$form->field($model,?'population')->radioList(array('1'=>'One',2=>'Two')); ?>

    ListBox Field

    Using below we can create the list box base on model attribute of yii2.0 framework. We added the following options like prompt, size, disabled, style etc

    01 <!-- Listbox with prompt text -->
    02 <?=?$form->field($model,?'population')-> listBox(
    03 ????????????array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
    04 ????????????array('prompt'=>'Select')
    05 ????????????); ?>
    06 <!-- Listbox with size -->
    07 <?=?$form->field($model,?'population')-> listBox(
    08 ????????????array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
    09 ????????????array('prompt'=>'Select','size'=>3)
    10 ????????????); ?>
    11 <!-- Listbox with disabled, style properties -->
    12 <?=?$form->field($model,?'population')-> listBox(
    13 ????????????array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
    14 ????????????array('disabled'?=> true,'style'=>'background:gray;color:#fff;'))
    15 ????????????->label('Gender'); ?>

    dropDown List Input Field

    dropDownList() function is used to create HTML ‘select’ tag input field.

    1 //Format 1
    2 <?php?echo?$form->field($model,?'name')->dropDownList(['a'?=>?'Item A',?'b'?=>?'Item B',?'c'?=>?'Item C']); ?>
    3 // Format 2
    4 <?echo?$form->field($model,?'name')->dropDownList($listData, ['prompt'=>'Choose...']);>

    Submit Button

    1 <?= Html::submitButton('Submit', ['class'=>?'btn btn-primary']) ;?>

    轉載于:https://my.oschina.net/kenblog/blog/424043

    總結

    以上是生活随笔為你收集整理的Yii2.0 ActiveForm Input Fields的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。