Yii2 form 表单用法

表单的创建

  • 在Yii中主要通过 yii\widgets\ActiveForm类来创建表单
  • ActiveForm::begin()不仅创建了一个表单实例,同时也标志着表单的开始
  • 放在 ActiveForm::begin()ActiveForm::end() 之间的所有内容都被包裹在 htmlform 标签中
  • 中间是通过调用 ActiveForm::field() 方法来创建一个 ActiveForm 实例,这个实例会创建表单元素与元素的标签,以及对应的 js 验证
  • ActiveField 有一个对应的模型和属性, input 输入框的 name 属性会自动的根据属性名来创建,同时,还会用属性的验证规则来验证用户输入的数据

e.g.

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>
<?php ActiveForm::end(); ?>

解析后的标签为:

<form id="w0" action="/xx/xx/xx" method="post">
    <input type="text" id="contactform-name" class="form-control" name="ContactForm[name]" autofocus />
</form>

还会自动添加 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 不能为空",
  });
}])

ActiveField 对象的使用

参考类参考手册 yii\widgets\ActiveField 中的方法

自定义表单action链接和method

<?php $form = ActiveForm::begin(['action'=>['test/getpost'], 'method'=>'post',]); ?>

文本输入框

<?= $form->field($model, 'password')->textInput(); ?>

限制输入长度

<?= $form->field($model, 'username')->textInput(['maxlength'=>20]); ?>

关闭输入自动完成功能

<?= $form->field($model, 'username')->textInput(['autocomplete'=>'off']); ?>

输入框只读

<?= $form->field($model, 'username')->textInput(['readonly'=>true]); ?>

密码输入框

<?= $form->field($model, 'password')->passwordInput(); ?>

单选框

<?= $form->field($model, 'sex')->radioList(['1'=>'男', '0'=>'女']); ?>

复选框

<?= $form->field($model, 'hobby')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']); ?>

下拉框

<?= $form->field($model, 'edu')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择']); ?>

隐藏域

<?= $form->field($model, 'userid')->hiddenInput(['value'=>3]); ?>

文本域

<?= $form->field($model, 'info')->textarea(['rows'=>3]); ?>

文件上传

<?= $form->field($model, 'file')->fileInput(); ?>

提示文字

<?= $form->field($model, 'username')->textInput()->hint('Please enter your name'); ?>

设置文字样式

<?= $form->field($model, 'username')->textInput()->hint('Please enter your name', ['style'=>'color:red;']); ?>

html5的邮箱输入框

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

自定义输入框的显示标签名

<?= $form->field($model, 'name')->label('姓名'); ?>

额外标签的处理,即与模型对象没有关系的额外 HTML 标签

e.g.submitbutton, p

使用 yii\helpers\Html 帮助类中的方法来添加到表单中

纯文本 :

<?= '<p class="username">'.$user->name.'</p>' ?>

Html 帮助类

<?= Html::tag('p',Html::encode($user->name),['class'=>'username']) ?>

提交按钮

<?= Html::submitButton('提交', ['class'=>'btn btn-primary', 'name' =>'submit-button']); ?>

重置按钮

<?= Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']); ?>

块儿赋值

  • input 中的 name ,实际是以对象名来命名的一个数组,数组的键对应模型的属性 (e.g. name="ContactForm[name]"
  • model 执行 load() 方法,就是对每个属性执行这样一句赋值:$model->name = isset($ContactForm['name']) ? $ContactForm[name] : null;
  • 块赋值就是用这一句代码将用户所有的输入数据填充到模型中去

dropDownList 的高级用法

<?= $form->field($model, 'status')->dropDownList($allStatus,['options' => [$needSelected => ['selected' => 'selected']],['prompt'=>'请选择状态']) ?>
  • $allStatus 是一个 [1=>'已发布'] 形式的关联数组
  • $needSelected 为需要默认选中的值,其值满足 $allStatuskey 值中的一个
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PeakXin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值