thinkphp 3.2 =》0623-13_自动验证.=》注册表

来源:互联网 发布:宁知此为归骨所耶翻译 编辑:程序博客网 时间:2024/04/17 03:43

1.控制器=》shopApplication/Home/Controller/UserController.class.php


<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-6-23
 * Time: 下午5:33
 */

namespace Home\Controller;


use Think\Controller;

class UserController extends Controller{
    public function  register(){
        if(IS_POST){
            $userModel = D('User');
            if($userModel->create()){ //验证的功能

            }else{

                //>>1.拼接错误信息
                $errors = $userModel->getError();
                $errStr = '<ul>';
                foreach($errors as $error){
                      $errStr.="<li>$error</li>";
                }
                $errStr.='</ul>';

                $this->error($errStr);
            }


        }else{
            $this->display();
        }
    }
}

----------------**********------------------------**********---------------------------------

2.模型=》shopApplication/Home/Model/UserModel.class.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-6-23
 * Time: 下午5:46
 */

namespace Home\Model;


use Think\Model;

class UserModel extends Model{

    protected $patchValidate    =   true; //开启批量验证..

    /*
     *  array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
     *
     *
     *
     */

    protected $_validate        =   array(
        array('username','require','用户名不能够为空',self::MUST_VALIDATE,),
        array('password','require','密码不能够为空'),
//        array('password2','require','确认密码不能够为空'),
        array('password2','password','确认密码和密码不一致','','confirm'), //如果附加规则是confirm, 那么验证规则中必须写另外一个字段的值
        array('user_email','email','邮箱格式不正确'), //如果附加规则是confirm, 那么验证规则中必须写另外一个字段的值
        array('user_qq','/^[1-9][0-9]{5,11}$/','QQ格式不正确','','regex'), //如果附加规则是confirm, 那么验证规则中必须写另外一个字段的值
        array('user_xueli','2,3,4','没有选择学历','','in'), //如果附加规则是confirm, 那么验证规则中必须写另外一个字段的值
        array('user_hobby','check_hobby','至少选择两个爱好','','callback'), //如果附加规则是confirm, 那么验证规则中必须写另外一个字段的值
    );

    public function check_hobby($hobby){
        if(count($hobby)>=2){
            return true;
        }else{
            return false;
        }
    }

}


3. 视图     =》shopApplication/Home/View/User/register.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta name="Generator" content="YONGDA v1.0" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="Keywords" content="YONGDA商城" />
        <meta name="Description" content="YONGDA商城" />

        <title>YONGDA商城 - Powered by YongDa</title>

        <link href="__CSS__/style.css" rel="stylesheet" type="text/css" />

    </head>
    <body class="index_body">
        <div class="block clearfix" style="position: relative; height: 98px;">
            <a href="#" name="top"><img class="logo" src="__IMG__/logo.gif"></a>

            <div id="topNav" class="clearfix">
                <div style="float: left;">
                    <font id="ECS_MEMBERZONE">

                        <div id="append_parent"></div>
                        欢迎光临本店&nbsp;
                        <a href="./index.php?r=user/login"> 登录</a>
                        <a href="./index.php?r=user/register">注册</a>
                    </font>
                </div>
                <div style="float: right;">

                    <a href="#">查看购物车</a>
                    |
                    <a href="#">选购中心</a>
                    |
                    <a href="#">标签云</a>
                    |
                    <a href="#">报价单</a>
                </div>

            </div>
            <div id="mainNav" class="clearfix">
                <a href="#" class="cur">首页<span></span></a>
                <a href="#">GSM手机<span></span></a>
                <a href="#">双模手机<span></span></a>
                <a href="#">手机配件<span></span></a>
                <a href="#">优惠活动<span></span></a>

                <a href="#">留言板<span></span></a>
            </div>
        </div>

        <div class="header_bg">
            <div style="float: left; font-size: 14px; color:white; padding-left: 15px;">
            </div>  

            <form id="searchForm" method="get" action="#">
                <input name="keywords" id="keyword" type="text" />

                <input name="imageField" value=" " class="go" style="cursor: pointer; background: url('__IMG__/sousuo.gif') no-repeat scroll 0% 0% transparent; width: 39px; height: 20px; border: medium none; float: left; margin-right: 15px; vertical-align: middle;" type="submit" />

            </form>
        </div>
        <div class="blank5"></div>
        <div class="header_bg_b">
            <div class="f_l" style="padding-left: 10px;">
                <img src="__IMG__/biao6.gif" />
                北京市区,现在下单(截至次日00:30已出库),<b>明天上午(9-14点)</b>送达 <b>免运费火热进行中!</b>

            </div>
            <div class="f_r" style="padding-right: 10px;">
                <img style="vertical-align: middle;" src="__IMG__/biao3.gif">
                    <span class="cart" id="ECS_CARTINFO">
                        <a href="#" title="查看购物车">您的购物车中有 0 件商品,总计金额 ¥0.00元。</a></span>
                    <a href="#"><img style="vertical-align: middle;" src="__IMG__/biao7.gif"></a>

            </div>
        </div>

        <div class="block block1">  

            <div class="block box">
                <div class="blank"></div>
                <div id="ur_here">
                    当前位置: <a href="#">首页</a> <code>&gt;</code> 用户注册
                </div>
            </div>
            <div class="blank"></div>


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

            <div class="block box">

                <div class="usBox">
                    <div class="usBox_2 clearfix">
                        <div class="logtitle3"></div>
                        <form id="yw0" action="{:U()}" method="post">
                            <table cellpadding="5" cellspacing="3" style="text-align:left; width:100%; border:0;">
                                <tbody>
                                    <tr>
                                        <td style="width:13%; text-align: right;"><label for="User_username" class="required">用户名
                                        <span class="required">*</span></label>
                                        </td>

                                    </tr>
                                    <tr>
                                        <td align="right">
                                            <label for="User_password" class="required">密码 <span class="required">*</span></label>
                                        </td>

                                        <td>
                                            <input class="inputBg" size="25" name="password" id="User_password" type="password" value="" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right"><label for="User_password2">密码确认</label></td>
                                        <td>
                                            <input class="inputBg" size="25" name="password2" id="User_password2" type="password" />
                                        </td>

                                    </tr>
                                    <tr>
                                        <td align="right"><label for="User_user_email">邮箱</label></td>
                                        <td>
                                            <input class="inputBg" size="25" name="user_email" id="User_user_email" type="text" value="" />
                                        </td>
                                    </tr>
                                    <tr>

                                        <td align="right"><label for="User_user_qq">qq号码</label></td>
                                        <td>
                                            <input class="inputBg" size="25" name="user_qq" id="User_user_qq" type="text" value="" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="right"><label for="User_user_tel">手机</label></td>
                                        <td>
                                            <input class="inputBg" size="25" name="user_tel" id="User_user_tel" type="text" value="" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <!--radioButtonList($model,$attribute,$data,$htmlOptions=array())-->
                                        <td align="right"><label for="User_user_sex">性别</label></td>
                                        <td>
                                            <input id="ytUser_user_sex" type="hidden" value="" name="user_sex" />
                                            <span id="User_user_sex">
                                            <input id="User_user_sex_0" value="1" checked="checked" type="radio" name="user_sex" />
                                            <label for="User_user_sex_0">男</label>&nbsp;
                                            <input id="User_user_sex_1" value="2" type="radio" name="user_sex" />
                                            <label for="User_user_sex_1">女</label>&nbsp;
                                            <input id="User_user_sex_2" value="3" type="radio" name="user_sex" />
                                            <label for="User_user_sex_2">保密</label></span>                                
                                        </td>
                                    </tr>
                                    <tr>
                                        <!--dropDownList($model,$attribute,$data,$htmlOptions=array())-->
                                        <td align="right"><label for="User_user_xueli">学历</label></td>
                                        <td>
                                            <select name="user_xueli" id="User_user_xueli">
                                                <option value="1" selected="selected">-请选择-</option>
                                                <option value="2">小学</option>

                                                <option value="3">初中</option>
                                                <option value="4">高中</option>
                                                <option value="5">大学</option>
                                            </select>                                <div class="errorMessage" id="User_user_xueli_em_" style="display:none"></div>                            </td>
                                    </tr>
                                    <tr>
                                        <!--checkBoxList($model,$attribute,$data,$htmlOptions=array())-->
                                        <td align="right"><label for="User_user_hobby">爱好</label></td>

                                        <td>
                                            <span id="User_user_hobby">
                                            <input id="User_user_hobby_0" value="1" type="checkbox" name="user_hobby[]" />
                                            <label for="User_user_hobby_0">篮球</label>&nbsp;
                                            <input id="User_user_hobby_1" value="2" type="checkbox" name="user_hobby[]" />
                                            <label for="User_user_hobby_1">足球</label>&nbsp;
                                            <input id="User_user_hobby_2" value="3" type="checkbox" name="user_hobby[]" />
                                            <label for="User_user_hobby_2">排球</label>&nbsp;
                                            <input id="User_user_hobby_3" value="4" type="checkbox" name="user_hobby[]" />
                                            <label for="User_user_hobby_3">棒球</label>
                                            </span>                                
                                        </td>
                                    </tr>
                                    <tr>

                                        <!--textArea($model,$attribute,$htmlOptions=array())-->
                                        <td align="right"><label for="User_user_introduce">简介</label></td>
                                        <td>
                                            <textarea cols="50" rows="5" name="user_introduce" id="User_user_introduce"></textarea>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>

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

                        </form>        </div>
                </div>
            </div>
            <!--放入view具体内容-->

        </div>

        <div class="blank"></div>
        <div class="block">
            <a href="#" target="_blank" title="YONGDA商城"><img alt="YONGDA商城" src="__IMG__/di.jpg"></a>
            <div class="blank"></div>
        </div>

       


        
    </body>
</html>



0 0
原创粉丝点击