yii2表单

来源:互联网 发布:vscode前端必备插件 编辑:程序博客网 时间:2024/05/16 07:05
<?php  
namespace app\models\test;  
  
use yii\db\ActiveRecord;  
  
class Dynasty extends ActiveRecord //或者 \yii\base\Model  
{  
    public $username;  
    public $password;  
}  
  
  
<?php  
namespace app\controllers;  
  
use yii\web\Controller;  
use app\models\test\Dynasty;  
  
class TestController extends Controller  
{  
    public function actionIndex()  
    {  
        return $this->render('index', [  
            'model' => new Dynasty(),  
        ]);  
    }  
}  
  
  
<?php  
use yii\helpers\Html;  
use yii\widgets\ActiveForm;  
?>  
<?php $form = ActiveForm::begin(['id' => 'login-form','options' => ['class' => 'class_name'],'action'=>'test/index','method'=>'get',]); ?>  
  
    <?= $form->field($model, 'username') ?>  
  
    <?= $form->field($model, 'password')->passwordInput() ?>  
  
    <div class="form-group">  
        <?= Html::submitButton('Login') ?>  
    </div>  
  
<?php ActiveForm::end(); ?>  


规则


密码  
<?= $form->field($model, 'password')->passwordInput() ?>  
标签与提示  
<?= $form->field($model, 'username')->textInput()->hint('请输入你的用户名')->label('用户名') ?>  
<?= $form->field($model, 'username[]',['inputOptions'=>['value'=>'abc','class'=>'form-control']]) ?>//默认值  
邮箱  
<?= $form->field($model, 'username')->input('email') ?>  
上传  
<?= $form->field($model, 'username')->fileInput(['multiple'=>'multiple']) ?>  
多选列表  
<?= $form->field($model, 'username[]')->checkboxList(['a' => '多选a', 'b' => '多选b', 'c' => '多选c']) ?>  
单个选择框  
<?= $form->field($model, 'username')->checkbox([],false)->label('已审核') ?> ?>  
下拉列表  
<?= $form->field($model, 'username[]')->dropDownList(['a' => '多选a', 'b' => '多选b', 'c' => '多选c']) ?>  
<?= $form->field($model, 'username[]')->dropDownList(['a' => '多选a', 'b' => '多选b', 'c' => '多选c'], ['prompt' => '多选b']) ?>  
隐藏框  
<?= $form->field($model, 'username')->hiddenInput(['1']) ?>  
ListBox  
<?= $form->field($model, 'username[]')->listBox(['a' => '多选a', 'b' => '多选b', 'c' => '多选c']) ?>  
单选列表  
<?= $form->field($model, 'username[]')->radioList(['a' => '单选a', 'b' => '单选b', 'c' => '单选c']) ?>  
多行文本  
<?= $form->field($model, 'username')->textarea() ?>  
widget扩展  
<?= $form->field($model, 'username')->widget(\yii\widgets\MaskedInput::className(), ['mask' => '9999/99/99',]); ?>  


验证


<?php  
namespace app\models\test;  
  
use yii\db\ActiveRecord;  
  
class Dynasty extends ActiveRecord  
{  
    public $username;  
    public $password;  
    public $date;  
    public function rules()  
    {  
        return [  
            ['username', 'required', 'message' => '用户名不能为空.'],  
            ['username', 'length', 'min'=>3, 'max'=>12],  
            ['date','default','value'=>date('Y-m-d H:i:s')],  
                        ['verifyCode', 'captcha'],//验证码  
        ];  
    }  
}  
  
为空检测  
[['username', 'password'], 'required', 'message'=>'不能为空'],  
邮箱检测  
['username', 'email'],  
  
<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>  
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [  
    'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',  
]) ?>  
<div class="form-group">  
    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>  
</div>  


接收数据


public function actionIndex()  
{  
    $model = new Dynasty();  
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {  
        echo $model->username;  
        return $this->render('index', ['model' => $model]);  
    }else{  
  
    }  
  
}  


上传


CONTROLLER


<?php  
namespace app\controllers;  
  
use yii;  
use yii\web\Controller;  
use app\models\test\Dynasty;  
use yii\web\UploadedFile;  
  
class TestController extends Controller  
{  
    public function actionIndex()  
    {  
       if (Yii::$app->request->isPost) {  
            $model = new Dynasty();  
            $model->file = UploadedFile::getInstance($model, 'file');  
            if ($model->file && $model->validate()) {  
                $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension); //uploads 相对网站根目录  
            }  
        return $this->render('index', ['model' => $model]);  
        }  




MODEL
[php] view plain copy 在CODE上查看代码片派生到我的代码片
<?php  
namespace app\models\test;  
  
//use yii\base\Model;  
use yii\db\ActiveRecord;  
use yii\web\UploadedFile;  
  
class Dynasty extends ActiveRecord//Model  
{  
    public $username;  
    public $password;  
    public $file;  
    public function rules()  
    {  
        return [  
            ['file', 'file', 'extensions' => 'gif, jpg, png'],  
        ];  
    }  
}  




VIEW


<?php $form = ActiveForm::begin(['id' => 'login-form','options' =>  ['enctype' => 'multipart/form-data']]); ?>  
  
    <?= $form->field($model, 'username') ?>  
  
    <?= $form->field($model, 'file')->fileInput() ?>  
  
    <?= $form->field($model, 'password')->passwordInput() ?>  
  
  
    <div class="form-group">  
        <?= Html::submitButton('Login') ?>  
    </div>  
  
<?php ActiveForm::end(); ?>  
0 0
原创粉丝点击