Yii2 Tips 24 : All About ActiveForm
Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php namespace common\models; use Yii; use yii\base\Model; class DummyForm extends \backend\models\Branches { public $textInput; public $textInputPlaceholderNolabel; public $textFieldWithHint; public $disabledTextInput; public $readOnlyTextInput; public $autoFocusTextInput; public $select2DropDown; public $dropDownList; public $select2DropDownMultiple; public $radioList; public $checkBox; public $dateTimePicker; public $datePicker; /** * @inheritdoc */ public function rules() { return [ // username and password are both required [['checkBox','dateTimePicker','datePicker'],'safe'], [['textInput'], 'required'], [['autoFocusTextInput','disabledTextInput','readOnlyTextInput'], 'string', 'max' => 100], [['radioList','select2DropDownMultiple','select2DropDown','dropDownList'],'integer'], ]; } public function attributeLabels() { return [ 'textInput' => Yii::t('app', 'Text Input with Initial Value'), 'textFieldWithHint' => Yii::t('app','Text Input with Hint'), 'textInputPlaceholderNolabel' => Yii::t('app','Text Input With Placeholder and Without Label'), 'disabledTextInput' => Yii::t('app','Disabled Text Input'), 'readOnlyTextInput' => Yii::t('app', 'Read Only Text Input with Initial Value'), 'autoFocusTextInput' => Yii::t('app','Auto Focus Text Input'), 'select2DropDown' => Yii::t('app','Dropdown Select2 Widget'), 'dropDownList' => Yii::t('app','Drop Down List'), 'select2DropDownMultiple' => Yii::t('app', 'Select2 with Multiple Select'), 'radioList' => Yii::t('app','Radio Button'), 'checkBox' => Yii::t('app','Check Box List with Label and Pre Checked'), 'dateTimePicker' => Yii::t('app','Date Time Picker'), 'datePicker' => Yii::t('app','Date Picker'), ]; } } |
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php namespace frontend\controllers; use Yii; class TestController extends \yii\web\Controller { public function actionIndex(){ return $this->render('index'); } public function actionForm(){ $model = new \common\models\DummyForm(); $countries = [ '1' => 'Indonesia', '2' => 'Malaysia', '3' => 'Thailand', '4' => 'Singapore', ]; $gender = [ '1' => 'Male', '2' => 'Female', ]; if($model->load(Yii::$app->request->post())){ echo "<pre>"; print_r($model); echo "</pre>"; die(); } return $this->render('_form',[ 'model' => $model, 'countries' => $countries, 'gender' => $gender, ]); } } |
View Form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
<?php use yii\widgets\ActiveForm; use yii\helpers\Html; use kartik\select2\Select2; use kartik\datetime\DateTimePicker; use kartik\date\DatePicker; $this->registerCss(" .hint-block{ color:red; } "); ?> <div class="dummy-form"> <?php $form = ActiveForm::begin(); ?> <?= $form->field($model,'textInput')->textInput()->input('textInput',['value' => 'Initial Value']); ?> <hr> <?= $form->field($model, 'disabledTextInput')->textInput()->input('disabledTextInput', ['disabled' => true, ]) ?> <hr> <?= $form->field($model, 'readOnlyTextInput')->textInput()->input('readOnlyTextInput', ['readOnly' => true, 'value' => 'Initial Value', ]) ?> <hr> <?= $form->field($model, 'autoFocusTextInput')->textInput()->input('autoFocusTextInput', ['autoFocus' => true, ]) ?> <hr> <?= $form->field($model, 'textInputPlaceholderNolabel')->textInput()->input('textInputPlaceholderNolabel', ['placeholder' => 'Text Input With Placeholder and Without Label', ]) ->label(false) ?> <hr> <?= $form->field($model, 'textFieldWithHint')->textInput()->hint('*) Ini adalah text input dengan hint') ?> <hr> <?= $form->field($model, 'dropDownList')->dropDownList([ 'Inactive', 'Active', ], ['prompt' => 'Select Status']) ?> <hr> <?= $form->field($model,'select2DropDown')->widget(Select2::classname(),[ 'data' => $countries, 'language' => 'en', 'options' => ['placeholder' => 'Select Country'], 'pluginOptions' => ['allowClear' => true], ]); ?> <hr> <?= $form->field($model,'select2DropDownMultiple')->widget(Select2::classname(),[ 'data' => $countries, 'language' => 'en', 'options' => ['placeholder' => 'Select Countries','multiple' => true], 'pluginOptions' => ['allowClear' => true], ]); ?> <hr> <?php $model->radioList = '2';?> <?= $form->field($model, 'radioList')->checkboxList($gender, ['item' => function ($index, $label, $name, $checked, $value){ return '<label class="radio-inline">' . Html::radio($name, $checked, ['value' => $value]) . $label . '</label>';}]) ->label('<em>Radio List with Label and PreChecked</em>'); ?> <hr> <?php $model->checkBox = [1,2];?> <?= $form->field($model, 'checkBox')->checkboxList($countries)?> <hr> <?=DateTimePicker::widget([ 'model' => $model, 'attribute' => 'dateTimePicker', 'options' => ['placeholder' => 'Kartik DateTimePicker'], 'pluginOptions' => [ 'format' => 'yyyy-mm-dd hh:ii:ss', 'autoclose' => true, 'todayHighlight' => true ] ]); ?> <hr> <?=$form->field($model, 'datePicker')->label(false)->widget(DatePicker::className(),[ 'model' => $model, 'attribute'=>'datePicker', 'options' => ['placeholder' => 'Kartik DatePicker'], 'pluginOptions' => [ 'format' => 'yyyy-mm-dd', 'autoclose' => true, 'todayHighlight' => true, ] ]); ?> <div class="form-group"> <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> </div> |