• YII中的表单挂件


    利用助手(widget)在页面实现表单

    控制器中

    <?php

    class YiiFormController extends Controller

    {

    public function actionIndex()

    {

    $bbsInfoModel = new BbsInfo();//实例化当前要添加数据库中表的模型

    //这个array的参数值是让页面中的表单用的

    $this->render("index",array("bbsInfoModel"=>$bbsInfoModel));//注意这个参数

    }

    }

    ?>

    视图中

    (注:通过查看yiilite.php得知,'CActiveForm' => '/web/widgets/CActiveForm.php')

    <?php

    //表单的开始,并设置相关的form属性

    $form = $this->beginWidget("CActiveForm",array(

    "action"=>"index.php?r=yiiform/add",

    "method"=>"post",

    "htmlOptions"=>array(

    "enctype"=>"multipart/form-data",

    "name"=>"frm",

    "onsubmit"=>"return checkAdd()"

    )

    ));

    ?>

    标 签:<?php echo $form->label($bbsInfoModel,"标签名") ?>//label方法来源于CActiveForm类中的label方法

    文本框:<?php echo $form->textField($bbsInfoModel,"表中的字段名",array("size"=>20,"maxlength"=>4))?>//textField方法来源于CActiveForm类中的textField方法

    <?php $this->endWidget(); ?>//表单的结束

    模型中

    <?php

    class BbsInfo extends CActiveRecord

    {

    //通过调用CActiveRecord父类的方法来获得一个当前模型对象

    public static function model($className=__CLASS__)

    {

    return parent::model($className);

    }

    //指定当前模型对应的表名

    public function tableName()

    {

    //return "bbsInfo";//完整的表名

    return "{{bbsInfo}}";//省略前缀的表名

    }

    //该方法对应yii表单中的标签$form->label(模型对应,"id")

    public function attributeLabels()

    {

    //注意这个返回值,与上边的视图中的label标签对应

    return array(

    "title_id"=>"标题",

    "clickTimes_id"=>"点击次数"

    );

    }

    }

    ?>

  • 相关阅读:
    小事引发的思考
    C++程序设计教程学习(0)-引子
    Cygwin安装
    PATHEXT环境变量简介
    Oracle Real Application Cluster
    SQLNET.AUTHENTICATION_SERVICES参数说明
    用神经网络拟合数据
    用PyTorch自动求导
    用PyTorch做参数估计
    深度学习基础(概念性)
  • 原文地址:https://www.cnblogs.com/zhengyanbin2016/p/5388146.html
Copyright © 2020-2023  润新知