码迷,mamicode.com
首页 > 其他好文 > 详细

YII用户注册表单的实现熟悉前台各个表单元素操作方式

时间:2014-10-21 23:13:57      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:用户注册表单的实现熟悉前台各个表单元素操

模还是必须宝航连个基本方法,还有部分label标签映射为汉字:

<?php

/*
 * 用户模型
 * 
 */
class user extends CActiveRecord{
    //获得数据模型
    public static function model($classname =  __CLASS__){
       return parent::model($classname);
    }
    //定义数据表名字
    public function tableName(){
        return "{{user}}";
    }
    //设置标签名字和数据库字段对应
     public function attributeLabels() {
         //parent::attributeLabels();
         return array(
             'username' => '用户名',
              'password' => '密码',
              'user_qq' => 'qq',
              'user_tel' => '手机',
              'user_hobby' => '爱好',
             'user_introduce' => '简介',
             'user_sex' => '性别',
              'user_email' => 'email',
         );
     }
}
?>

关于注册页面控制器:

render方法主要包含两个字段,一个是要渲染的模板,一个是数组,表示要传递过去变量

<?php
	/*
		用户控制器
	*/
		class UserController extends Controller{
			function actionLogin(){
				//$this->renderPartial('login');
				$this->render('login');
				//$this ->renderPartial('login');
			}
			function actionRegister(){
                        //实例化数据模型对象user
                        $user_model = new User();
                        /**
                        * renderPartial不渲染布局
                        * render会渲染布局 
                        */
                        //$this ->renderPartial('register');

                        //性别信息
                        $sex[1] = "男";
                        $sex[2] = "女";
                        $sex[3] = "保密";

                        //定义学历
                        $xueli[1] = "-请选择-";
                        $xueli[2] = "小学";
                        $xueli[3] = "初中";
                        $xueli[4] = "高中";
                        $xueli[5] = "大学";

                        //定义爱好信息
                        $hobby[1] = "篮球";
                        $hobby[2] = "足球";
                        $hobby[3] = "排球";
                        $hobby[4] = "棒球";

                       $this -> render('register',array('user_model'=>$user_model,'sex'=>$sex,'xueli'=>$xueli,'hobby'=>$hobby));
			}
		}
?>
模型表单:

主要是创建form对象,调用小物件来实现 ,调用小物件时把CActiveForm传递进去,不是CActiveRecord偶


         <!--放入view具体内容-->

            <div class="block box">

                <div class="usBox">
                    <div class="usBox_2 clearfix">
                        <div class="logtitle3"></div>
                        <?php $form = $this -> beginWidget('CActiveForm'); ?>
                        <table cellpadding="5" cellspacing="3" style="text-align:left; width:100%; border:0;">
                                <tbody>
                                    <tr>
                                        <td style="width:13%; text-align: right;">
                                            <?php echo $form->label($user_model, 'username'); ?>
                                        </td>

                                        <td style="width:87%;">
                                             <?php echo $form->textField($user_model,'username',array('class'=>'inputBg','id'=>'User_username')); ?>
                                            <!--表单验证失败显示错误信息-->
                                            <?php echo $form ->error($user_model,'username'); ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right">
                                           <?php echo $form->label($user_model, 'password'); ?>
                                        </td>

                                        <td>
                                            <?php echo $form->passwordField($user_model,'password',array('class'=>'inputBg','id'=>'User_password')); ?>
                                            <?php echo $form ->error($user_model,'password'); ?>
                                        </td>
                                    </tr>
<!--                                    <tr>
                                        <td align="right"><label for="User_password2">密码确认</label></td>
                                        <td>
                                            <input class="inputBg" size="25" name="User[password2]" id="User_password2" type="password" />
                                        </td>

                                    </tr>-->
                                    <tr>
                                        <td align="right"><?php echo $form->label($user_model, 'user_email'); ?></td>
                                        <td>
                                            <?php echo $form->textField($user_model,'user_email',array('class'=>'inputBg','id'=>'User_user_email')); ?>
                                        </td>
                                    </tr>
                                    <tr>

                                        <td align="right"><?php echo $form->label($user_model, 'user_qq'); ?></td>
                                        <td>
                                            <?php echo $form->textField($user_model,'user_qq',array('class'=>'inputBg','id'=>'User_user_qq')); ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right"><?php echo $form->label($user_model, 'user_tel'); ?></td>
                                        <td>
                                            <?php echo $form->textField($user_model,'user_tel',array('class'=>'inputBg','id'=>'User_user_tel')); ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <!--radioButtonList($model,$attribute,$data,$htmlOptions=array())-->
                                        <td align="right"><?php echo $form->label($user_model, 'user_sex'); ?></td>
                                        <td>
                                            <?php echo $form->radioButtonList($user_model,'user_sex',$sex,array('separator'=>' ')); ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <!--dropDownList($model,$attribute,$data,$htmlOptions=array())-->
                                        <td align="right"><?php echo $form->label($user_model, 'user_xueli'); ?></td>
                                        <td>
                                            <?php echo $form -> dropDownList($user_model,'user_xueli',$xueli); ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <!--checkBoxList($model,$attribute,$data,$htmlOptions=array())-->
                                        <td align="right"><?php echo $form->label($user_model, 'user_hobby'); ?></td>
                                        <td>
                                            <?php echo $form -> checkBoxList($user_model,'user_hobby',$hobby,array('separator'=>' ')); ?>
                                        </td>
                                    </tr>
                                    <tr>

                                        <!--textArea($model,$attribute,$htmlOptions=array())-->
                                        <td align="right"><?php echo $form->label($user_model, 'user_introduce'); ?></td>
                                        <td>
                                            <?php echo $form -> textArea($user_model,'user_introduce',array('cols'=>50,'rows'=>5)); ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td> </td>

                                        <td align="left">
                                            <input name="Submit" value="" class="us_Submit_reg" type="submit" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2"> </td>
                                    </tr>
                                </tbody>
                            </table>

                        <?php $this->endWidget(); ?>
                    </div>
                </div>
            </div>
            <!--放入view具体内容-->

        </div>

        
模板主要是这几个html控件

主要是这几个样式

label ------》 label()

text ---------》textField()

area -------》 textArea()

checkbox ------》 checkBoxList()

radio ------------> radioButtonList()

password -------->passwordField()

这几个第一个参数都是控制器要传递过来的模型,第二个参数是数据库表字段,第三个是样式设置,为数组形式,第四个是一些特殊分隔符 如单选按和复选框。

关于第四个参数追踪:

bubuko.com,布布扣


就到了下面这个函数定义

bubuko.com,布布扣

再次查找:

bubuko.com,布布扣

找到CHtml类文件中上面引用函数


bubuko.com,布布扣

最后一次查找啦:

bubuko.com,布布扣

就此搞定,这只是简单应用啦!高级还在明天啦!





YII用户注册表单的实现熟悉前台各个表单元素操作方式

标签:用户注册表单的实现熟悉前台各个表单元素操

原文地址:http://blog.csdn.net/buyingfei8888/article/details/40355849

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!