• 框架学习七:自动验证、填充、字段映射


    一、自动验证

    注册页面reg.html

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
     3 <head>
     4 <title>新建网页</title>
     5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     6 <meta name="description" content="" />
     7 <meta name="keywords" content="" />
     8 <script type="text/javascript">
     9 
    10 </script>
    11 
    12 <style type="text/css">
    13 </style>
    14 </head>
    15     <body>
    16         <pre>create table user (
    17 id int primary key auto_increment,
    18 username varchar(20) not null default '',
    19 passwd char(32) not null default '',
    20 email varchar(50) not null default '',
    21 gender char(1) not null default '',
    22 age tinyint unsigned not null default 0
    23 )engine myisam charset utf8</pre>
    24         <h1>用户注册页面</h1>
    25         <form action="__URL__/regok" method="post">
    26             用户名:<input type="text" name="dogname" /><br />
    27             密码:<input type="text" name="passwd" /><br />
    28             重复密码:<input type="text" name="repasswd" /><br />
    29             email:<input type="text" name="email" /><br />
    30             年龄:<input type="text" name="age" /><br />
    31             性别:男<input type="radio" name="gender" value="男" />
    32             女:<input type="radio" name="gender" value="女" />
    33             妖:<input type="radio" name="gender" value="妖" />
    34             <input type="submit" value="提交" />
    35         </form>
    36     </body>
    37 </html>

    验证Model:UserModel.class.php

     1 <?php
     2 class UserModel extends Model {
     3     protected $_validate = array(
     4         array('username','require','用户名不能为空'),
     5         array('passwd','6,20','密码长度应在6-20位',1,'length'),
     6         array('repasswd','passwd','2次密码不一致',1,'confirm'),
     7         array('email','email','email不对',1),
     8         array('age','number','年龄应是数字',2),
     9         array('age','1,120','年龄异常',2,'between'),
    10         array('gender',array('男','女'),'性别只能是男或女',1,'in')
    11     );
    12 }
    13 ?>

    Action:UserAction.class.php

     1 <?php
     2 
     3 class UserAction extends Action {
     4     public $height = 180;
     5 
     6     public function regok() {
     7 
     8         $userModel = D('User');
     9         
    10         if($userModel->create() == false) {
    11             echo $userModel->getError();
    12             exit;
    13         }
    14 
    15         if($userModel->add()) {
    16             echo '注册成功';
    17         } else {
    18             echo '注册失败';
    19         }
    20     }
    21 }
    22 ?>

    二、自动填充

     1 <?php
     2 class UserModel extends Model {
     3     protected $_validate = array(
     4         array('username','require','用户名不能为空'),
     5         array('passwd','6,20','密码长度应在6-20位',1,'length'),
     6         array('repasswd','passwd','2次密码不一致',1,'confirm'),
     7         array('email','email','email不对',1),
     8         array('age','number','年龄应是数字',2),
     9         array('age','1,120','年龄异常',2,'between'),
    10         array('gender',array('男','女'),'性别只能是男或女',1,'in')
    11     );
    12 
    13     protected $_auto = array(
    14         // array('regtime','time',1,'function'),
    15         // array('regtime','age',1,'field')
    16         array('regtime','134000000',1)
    17     );
    18 }
    19 ?>

    字段映射

     1 <?php
     2 class UserModel extends Model {
     3     protected $_validate = array(
     4         array('username','require','用户名不能为空'),
     5         array('passwd','6,20','密码长度应在6-20位',1,'length'),
     6         array('repasswd','passwd','2次密码不一致',1,'confirm'),
     7         array('email','email','email不对',1),
     8         array('age','number','年龄应是数字',2),
     9         array('age','1,120','年龄异常',2,'between'),
    10         array('gender',array('男','女'),'性别只能是男或女',1,'in')
    11     );
    12 
    13     protected $_auto = array(
    14         // array('regtime','time',1,'function'),
    15         // array('regtime','age',1,'field')
    16         array('regtime','134000000',1)
    17     );
    18 
    19     protected $_map = array(
    20         'dogname'=>'username',    
    21     );
    22 
    23 }
  • 相关阅读:
    【luogu P1040 加分二叉树】 题解
    【luogu P4711 「化学」相对分子质量】 题解
    【luogu P2319 [HNOI2006]超级英雄】 题解
    【luogu P1640 [SCOI2010]连续攻击游戏】 题解
    【luogu P3369 普通平衡树(Treap/SBT)】 模板 Splay
    【luogu P2234 [HNOI2002]营业额统计】 题解
    【luogu P1494 [国家集训队]小Z的袜子】 题解
    【luogu P3901 数列找不同】 题解
    【luogu P3807 卢卡斯定理】 模板
    JAVA IO 体系
  • 原文地址:https://www.cnblogs.com/zhanghonggang/p/3176274.html
Copyright © 2020-2023  润新知