Yii2 表单

来源:互联网 发布:.com.au域名注册 编辑:程序博客网 时间:2024/04/29 19:13

参考
http://www.yiiframework.com/doc-2.0/guide-input-forms.html

结构

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. namespace app\models\test;  
  3.   
  4. use yii\db\ActiveRecord;  
  5.   
  6. class Dynasty extends ActiveRecord //或者 \yii\base\Model  
  7. {  
  8.     public $username;  
  9.     public $password;  
  10. }  
  11.   
  12.   
  13. <?php  
  14. namespace app\controllers;  
  15.   
  16. use yii\web\Controller;  
  17. use app\models\test\Dynasty;  
  18.   
  19. class TestController extends Controller  
  20. {  
  21.     public function actionIndex()  
  22.     {  
  23.         return $this->render('index', [  
  24.             'model' => new Dynasty(),  
  25.         ]);  
  26.     }  
  27. }  
  28.   
  29.   
  30. <?php  
  31. use yii\helpers\Html;  
  32. use yii\widgets\ActiveForm;  
  33. ?>  
  34. <?php $form = ActiveForm::begin(['id' => 'login-form','options' => ['class' => 'class_name'],'action'=>'test/index','method'=>'get',]); ?>  
  35.   
  36.     <?= $form->field($model'username') ?>  
  37.   
  38.     <?= $form->field($model'password')->passwordInput() ?>  
  39.   
  40.     <div class="form-group">  
  41.         <?= Html::submitButton('Login') ?>  
  42.     </div>  
  43.   
  44. <?php ActiveForm::end(); ?>  

规则

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 密码  
  2. <?= $form->field($model'password')->passwordInput() ?>  
  3. 标签与提示  
  4. <?= $form->field($model'username')->textInput()->hint('请输入你的用户名')->label('用户名') ?>  
  5. <?= $form->field($model'username[]',['inputOptions'=>['value'=>'abc','class'=>'form-control']]) ?>//默认值  
  6. 邮箱  
  7. <?= $form->field($model'username')->input('email') ?>  
  8. 上传  
  9. <?= $form->field($model'username')->fileInput(['multiple'=>'multiple']) ?>  
  10. 多选列表  
  11. <?= $form->field($model'username[]')->checkboxList(['a' => '多选a''b' => '多选b''c' => '多选c']) ?>  
  12. 单个选择框  
  13. <?= $form->field($model'username')->checkbox([],false)->label('已审核') ?> ?>  
  14. 下拉列表  
  15. <?= $form->field($model'username[]')->dropDownList(['a' => '多选a''b' => '多选b''c' => '多选c']) ?>  
  16. <?= $form->field($model'username[]')->dropDownList(['a' => '多选a''b' => '多选b''c' => '多选c'], ['prompt' => '多选b']) ?>  
  17. 隐藏框  
  18. <?= $form->field($model'username')->hiddenInput(['1']) ?>  
  19. ListBox  
  20. <?= $form->field($model'username[]')->listBox(['a' => '多选a''b' => '多选b''c' => '多选c']) ?>  
  21. 单选列表  
  22. <?= $form->field($model'username[]')->radioList(['a' => '单选a''b' => '单选b''c' => '单选c']) ?>  
  23. 多行文本  
  24. <?= $form->field($model'username')->textarea() ?>  
  25. widget扩展  
  26. <?= $form->field($model'username')->widget(\yii\widgets\MaskedInput::className(), ['mask' => '9999/99/99',]); ?>  


验证

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. namespace app\models\test;  
  3.   
  4. use yii\db\ActiveRecord;  
  5.   
  6. class Dynasty extends ActiveRecord  
  7. {  
  8.     public $username;  
  9.     public $password;  
  10.     public $date;  
  11.     public function rules()  
  12.     {  
  13.         return [  
  14.             ['username''required''message' => '用户名不能为空.'],  
  15.             ['username''length''min'=>3, 'max'=>12],  
  16.             ['date','default','value'=>date('Y-m-d H:i:s')],  
  17.                         ['verifyCode''captcha'],//验证码  
  18.         ];  
  19.     }  
  20. }  
  21.   
  22. 为空检测  
  23. [['username''password'], 'required''message'=>'不能为空'],  
  24. 邮箱检测  
  25. ['username''email'],  
  26.   
  27. <?= $form->field($model'body')->textArea(['rows' => 6]) ?>  
  28. <?= $form->field($model'verifyCode')->widget(Captcha::className(), [  
  29.     'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',  
  30. ]) ?>  
  31. <div class="form-group">  
  32.     <?= Html::submitButton('Submit', ['class' => 'btn btn-primary''name' => 'contact-button']) ?>  
  33. </div>  

接收数据

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public function actionIndex()  
  2. {  
  3.     $model = new Dynasty();  
  4.     if ($model->load(Yii::$app->request->post()) && $model->validate()) {  
  5.         echo $model->username;  
  6.         return $this->render('index', ['model' => $model]);  
  7.     }else{  
  8.   
  9.     }  
  10.   
  11. }  

上传


CONTROLLER

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. namespace app\controllers;  
  3.   
  4. use yii;  
  5. use yii\web\Controller;  
  6. use app\models\test\Dynasty;  
  7. use yii\web\UploadedFile;  
  8.   
  9. class TestController extends Controller  
  10. {  
  11.     public function actionIndex()  
  12.     {  
  13.        if (Yii::$app->request->isPost) {  
  14.             $model = new Dynasty();  
  15.             $model->file = UploadedFile::getInstance($model'file');  
  16.             if ($model->file && $model->validate()) {  
  17.                 $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension); //uploads 相对网站根目录  
  18.             }  
  19.         return $this->render('index', ['model' => $model]);  
  20.         }  


MODEL

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. namespace app\models\test;  
  3.   
  4. //use yii\base\Model;  
  5. use yii\db\ActiveRecord;  
  6. use yii\web\UploadedFile;  
  7.   
  8. class Dynasty extends ActiveRecord//Model  
  9. {  
  10.     public $username;  
  11.     public $password;  
  12.     public $file;  
  13.     public function rules()  
  14.     {  
  15.         return [  
  16.             ['file''file''extensions' => 'gif, jpg, png'],  
  17.         ];  
  18.     }  
  19. }  


VIEW

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php $form = ActiveForm::begin(['id' => 'login-form','options' =>  ['enctype' => 'multipart/form-data']]); ?>  
  2.   
  3.     <?= $form->field($model'username') ?>  
  4.   
  5.     <?= $form->field($model'file')->fileInput() ?>  
  6.   
  7.     <?= $form->field($model'password')->passwordInput() ?>  
  8.   
  9.   
  10.     <div class="form-group">  
  11.         <?= Html::submitButton('Login') ?>  
  12.     </div>  
  13.   
  14. <?php ActiveForm::end(); ?>  
0 0