表單
1、表單的創建
- 在 yii 中主要通過 yii\widgets\ActiveForm 類來創建表單
- ActiveForm::begin() 不僅創建了一個表單實例,同時也標志着表單的開始。
- 放在 ActiveForm::begin() 和 ActiveForm::end()之間的所有內容都被包裹在 html 的 from 標簽中
- 中間是通過調用 ActiveForm::field() 方法來創建一個 ActiveForm 實例,這個實例會創建表單元素與元素的標簽,以及對應的 js 驗證
- ActiveField 有一個對應的模型和屬性, input 輸入框的 name 屬性會自動的根據屬性名來創建,同時,還會用屬性的驗證規則來驗證用戶輸入的數據
e.g.
<?= $form->field($model,'name')->textInput(['autofocus' => true]) ?>
解析后的標簽為:
<input type="text" id="contactform-name" class="form-control" name="ContactForm[name]" autofocus />
還會自動添加 js 驗證,代碼如下:
jQuery('#contactform-name').yiiActiveForm([{ "id" : "contactform-name", "name" : "name", "container" : ".field-contactform-name", "input" : "#contactform-name", "error" : ".help-block.help-block-error", "validate" : function(attribute,value,messages,deferred,$form){ yii.validation.required(value,messages,{ "messages" : "Name 不能為空", }); }])
2、ActiveField 對象的使用(參考類參考手冊 yii\widgets\ActiveField 中的 methods)
- <?= $form->field($model,'password') -> passwordInput() ?> <!--密碼輸入框-->
- <?= $form->field($model,'username') -> textInput() -> hint('Please enter your name')->label('Name') ?> <!--輸入框增加了一個提示標簽-->
- <?= $form->field($model,'email') -> input('email') ?> <!--創建一個 html5 的郵箱輸入框-->
- <?= $form -> field($model,'name')->label('姓名') ?> <!-- 自定義輸入框的顯示標簽名 -->
3、額外標簽的處理,即與模型對象沒有關系的額外 HTML 標簽(e.g. submit,button, p 等 )
使用 yii\helpers\Html 幫助類中的方法來添加到表單中
e.g. 純文本 :
<?= '<p class="username">'.$user->name.'</p>' ?>
Html 幫助類:
<?= Html::tag('p',Html::encode($user->name),['class'=>'username']) ?>
4、塊兒賦值
- input 中的 name ,實際是以對象名來命名的一個數組,數組的鍵對應模型的屬性 (e.g. name="ContactForm[name]")
- model 執行 load() 方法,就是對每個屬性執行這樣一句賦值:$model -> name = isset($ContactForm['name']) ? $ContactForm[name] : null;
- 塊賦值就是用這一句代碼將用戶所有的輸入數據填充到模型中去
5、dropDownList 的用法:
<?= $form->field($model, 'status')->dropDownList($allStatus,['options' => [$needSelected => ['selected' => 'selected']],['prompt'=>'請選擇狀態']) ?>
$allStatus 是一個 [1=>'已發布'] 形式的關聯數組
$needSelected 為需要默認選中的值,其值滿足 $allStatus 中 key 值中的一個
注:本文為作者(44106-kangaroo) 看完魏羲教你學Yii2.0 視頻后所記,如有轉載請注明出處:http://www.cnblogs.com/chrdai/p/8005492.html
Yii2中各種文本框的使用
文本框:textInput();
密碼框:passwordInput();
單選框:radio()
,radioList();
復選框:checkbox()
,checkboxList();
下拉框:dropDownList();
隱藏域:hiddenInput();
文本域:textarea(['rows'=>3]);
文件上傳:fileInput();
提交按鈕:submitButton();
重置按鈕:resetButtun();
<?php $form = ActiveForm::begin(['action' => ['test/getpost'],'method'=>'post',]); ?>
<? echo $form->field($model, 'username')->textInput(['maxlength' => 20]) ?>
<? echo $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?>
<? echo $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?>
<? echo $form->field($model, 'edu')->dropDownList(['1'=>'大學','2'=>'高中','3'=>'初中'], ['prompt'=>'請選擇','style'=>'width:120px']) ?>
<? echo $form->field($model, 'file')->fileInput() ?>
<? echo $form->field($model, 'hobby')->checkboxList(['0'=>'籃球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?>
<? echo $form->field($model, 'info')->textarea(['rows'=>3]) ?>
<? echo $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?>
<? echo Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<? echo Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?php ActiveForm::end(); ?>
yii2