Yii2.0 ActiveForm 表单应用整理
来源:互联网 发布:阿里云服务器能翻墙么 编辑:程序博客网 时间:2024/05/16 10:26
今天打算整理用YII框架自己ActiveForm的表单生成方式
如下图所示, 就是一个表单,我自己没有写form,input...
在model文件中:
【下面是rules规则】
public function rules() { return [ [['user_id', 'scope_id', 'cate_id', 'report_status', 'created_at', 'updated_at'], 'integer'], [['report_com_name', 'report_com_address', 'report_com_desc', 'scope_id', 'cate_id', 'report_content', 'report_name', 'report_mobile'], 'required'], [['report_com_url', 'report_com_address'], 'string', 'max' => 200], [['report_content', 'report_com_desc'], 'string', 'max' => 1000], [['report_com_name'], 'string', 'max' => 100], [['report_name'], 'string', 'max' => 30], [['report_mobile'], 'string', 'max' => 11], [['report_email'], 'string', 'max' => 200], ['report_mobile', function ($attribute, $params) { //这是自己写的规则,和框架里的不一样,不能像他们一下,一个数据没填,就直接提示,而自己写的规则需要点击提交后才能去验证 if (strlen($this->$attribute) != 11) { $this->addError($attribute, '手机号码必须是11位'); } }], ]; }// 属性标签
public function attributeLabels() { return [ 'report_id' => '报道ID', 'user_id' => '用户ID', 'scope_id' => '行业分类', 'cate_id' => '报道方向', 'report_com_name' => '公司名称', 'report_com_url' => '公司网址', 'report_com_address' => '公司地址', 'report_com_desc' => '公司简介', 'report_content' => '简述报道内容', 'report_name' => '联系人姓名', 'report_mobile' => '联系方式', 'report_email' => '邮箱', // 'report_status' => 'Report Status', // 'created_at' => 'Created At', // 'updated_at' => 'Updated At', ]; }【模板文件-表单部分代码】
<?php $form = ActiveForm::begin([ 'options' => ['enctype' => 'multipart/form-data', 'class' => ''], 'method' => 'post', ]);?><div class="formitem cf"> <?php echo $form->field($model, 'report_com_name', ['template' => "\n{label}{input}\n<div class='error'>{error}</div>"]) ->textInput(['placeholder' => "请输入您公司名称"]) ->label("<i class=\"star\">*</i>{$labels['report_com_name']}", ['class' => "fl"]) ?></div><div class="formitem cf"> <?php echo $form->field($model, 'report_com_url', ['template' => "\n{label}{input}"]) ->textInput(['placeholder' => "请输入您公司网址"]) ->label("{$labels['report_com_url']}", ['class' => "fl"]); ?></div><div class="formitem cf"> <?php echo $form->field($model, 'report_com_address', ['template' => "\n{label}{input}\n<div class='error'>{error}</div>"]) ->textInput(['placeholder' => "请输入您公司地址"]) ->label("<i class=\"star\">*</i>{$labels['report_com_address']}", ['class' => "fl"]); ?></div><div class="formitem cf"> <?php echo $form->field($model, 'report_com_desc', ['template' => "\n{label}{input}\n<div class='error'>{error}</div>"]) ->textarea(['placeholder' => "请输入您公司简介"]) ->label("<i class=\"star\">*</i>{$labels['report_com_desc']}", ['class' => "fl"]); ?></div><div class="formitem cf"><?php echo $form->field($model, 'scope_id', ['template' => "\n{label}{input}\n<div class='error'>{error}</div>"]) ->dropDownList($scopeDatas) ->label("<i class=\"star\">*</i>{$labels['scope_id']}", ['class' => "fl"]);?></div><div class="formitem cf"> <?php echo $form->field($model, 'cate_id', ['template' => "\n{label}{input}\n<div class='error'>{error}</div>"]) ->dropDownList($cateDatas) ->label("<i class=\"star\">*</i>{$labels['cate_id']}", ['class' => "fl"]); ?></div><div class="formitem cf"> <?php echo $form->field($model, 'report_content', ['template' => "\n{label}{input}\n<div class='error'>{error}</div>"]) ->textarea(['placeholder' => "请输入报道内容"]) ->label("<i class=\"star\">*</i>{$labels['report_content']}", ['class' => "fl"]); ?></div><div class="formitem cf"> <?php echo $form->field($model, 'report_name', ['template' => "\n{label}{input}\n<div class='error'>{error}</div>"]) ->textInput(['placeholder' => "请输入联系人姓名"]) ->label("<i class=\"star\">*</i>{$labels['report_name']}", ['class' => "fl"]); ?></div><div class="formitem cf"> <?php echo $form->field($model, 'report_mobile', ['template' => "\n{label}{input}\n<div class='error'>{error}</div>"]) ->textInput(['placeholder' => "请输入联系方式"]) ->label("<i class=\"star\">*</i>{$labels['report_mobile']}", ['class' => "fl"]); ?></div><div class="formitem cf"> <?php echo $form->field($model, 'report_email', ['template' => "\n{label}{input}"]) ->textInput(['placeholder' => "请输入您的邮箱"]) ->label("{$labels['report_email']}", ['class' => "fl"]); ?></div> <div class="formitem cf"> <label class="fl"></label> <div class="fl"> <?= Html::submitButton('提交认证', ['class' => 'coarsebtn subbtn']) ?> </div></div><?php ActiveForm::end() ?>【当表单数据填写完毕时,在控制器方法中处理方式】
//寻求报道
public function actionSeekReport() { $mEnumScode = new EnumScode(); $mEnumCate = new EnumCate(); $model = new Report(); $scopeData = $mEnumScode->getScopeData(); $cateData = $mEnumCate->getData(); $cateDatas['0'] = '请选择'; $scopeDatas['0'] = '请选择'; if ($scopeData && is_array($scopeData)) { foreach ($scopeData as $key => $value) { if ($value['scope_id'] && $value['scope_name']) { $scopeDatas[$value['scope_id']] = $value['scope_name']; } } } if ($cateData && is_array($cateData)) { foreach ($cateData as $key => $value) { if ($value['cate_id'] && $value['cate_name']) { $cateDatas[$value['cate_id']] = $value['cate_name']; } } } // 提交数据处理 $model->user_id = $this->user->id; //说明: 代码: $model->load(Yii::$app->request->post() 作用: 将表单提交的数据给了model对象 代码: $model->save() 作用: 表单提交验证完后(具体验证就是:model里面的rules方法中写的规则),就进行save保存处理 if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['demands']); } return $this->render('/user/demands/seek_report', [ 'scopeDatas' => $scopeDatas, 'cateDatas' => $cateDatas, 'model' => $model, ]); }
具体学习网址: http://www.getyii.com/doc-2.0/guide/start-forms.html
感想:
我以前都是我们通常的form表单方式,这个用到了activeForm表单方式,还是我了解的太少了,我以为只有后台才会用到这种情况,因为用gii自动生成的curd文件中的form文件有这种方式,原来它们都是通用,只是看你怎么灵活的运用,还有就是我平时做添加表操作,都是单独在model方法中写一个add方法,而现在是利用框架自带的特性,省去了好多的代码。感觉不错....
这是我同事给我说的这种方式,我真学到了,其实啊,在工作中只要你好好干,你会学到好多你不知道的东西,以前当我有任务时,我老认为工作是为公司,有一点时间就想休息,现在啊,想的不是了,我现在就是认为你工作是一方面,还有一方面就是你把它当做你自己的事,你会有不一样的体验,更有不一样的心情,只要每天心情开心了,效率才好,学的才多!所以,加油!现在你是很笨,或许人家给你定位你技术很low,但是你要证明一下,总有一天,你会让他们另眼相看!
1 0
- Yii2.0 ActiveForm 表单应用整理
- YII2.0使用ActiveForm表单
- YII2.0 Activeform表单组件的使用方法
- YII2.0 Activeform表单组件的使用方法
- yii2.0 Activeform表单部分组件使用方法
- yii2.0 Activeform表单部分组件使用方法
- yii2.0 Activeform表单部分组件使用方法
- yii2.0 Activeform表单部分组件使用方法
- YII2.0 Activeform表单组件的使用方法
- YII2.0 Activeform表单组件的使用方法
- yii2常用ActiveForm表单
- Yii2 ActiveForm 简单应用
- Yii2创建表单(ActiveForm)
- YII2.0 Activeform
- YII2.0 Activeform
- yii2.0使用ActiveForm
- [ActiveForm] -- Yii2.0源码分析之——创建表单(ActiveForm)
- YII2 ActiveForm表单回显详解
- How to config ReaderView of Firefox to print best fitable PDF.
- 线程返回值以及线程锁
- C#界面设计之文本阅读器的设计
- 通过Html启动IOS的APP
- 数据库分表处理设计思想和实现
- Yii2.0 ActiveForm 表单应用整理
- Struts2 Result详解
- 程序源入坑之路3
- 趣味编程_8棋皇后
- 使用lldb 和debugsever 调试程序
- 磁盘相关装载记录
- iptables常用shell模板配置
- mongodb3.0副本集配置
- spring源码剖析(六)AOP实现原理剖析