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

一、自动验证

注册页面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 }

 

 

 

posted on 2013-07-07 13:32  大钢  阅读(208)  评论(0编辑  收藏  举报

导航