Home > Article > Backend Development > Three major automatic introductions in ThinkPHP, three major introductions to ThinkPHP_PHP tutorial
This article describes the three major automatics in ThinkPHP in more detail. They are very important applications and are shared with everyone for your reference. The details are as follows:
1. Automatic verification
The format is as follows:
array('验证字段','验证规则','错误提示','验证条件','附加规则','验证时间')
Parameter description:
Validation field: Need to verify form field name
Validation rules: must be used in conjunction with additional rules
Error prompt: If an error occurs, what kind of error prompt is thrown to inform the user
Verification conditions: 0, 1, 2
Additional rules: 1. Use regex to verify 2. Use function to verify 3. Callback 4. Confirm to verify whether the two fields in the form are the same 5. Verify whether it is equal to a certain value 6. Whether in is within a certain range 7. Verification Is it the only one
TP encapsulation: require field must be verified; eamil verification email; url verification url address; currency currency; number number;
Verification time: refers to the verification time of database operation time. Verify Model::MODEL_INSERT when adding data; verify Model::MODEL_UPDATE when editing; verify Model::MODEL_BOTH in all cases;
aoli/Home/Tpl/default/User/reg.html page is as follows:
<form action="__URL__/regadd" method="post"> 用户名:<input type="text" name="username" /><br /> 密码:<input type="password" name="password" /><br /> 重复密码:<input type="password" name="repassword" /><br /> 注册时间:<input type="text" name="createtime" /><br /> 注册IP:<input type="text" name="createip" /><br /> <input type="submit" value="注册" /> </form>
aoli/Home/Lib/Model/UserModel.class.php page is as follows:
<?php class UserModel extends Model{//对应数据库中的表user protected $_validate=array( array('username','require','用户名必填'), array('username','checklen','用户名长度过长或过短',0,'callback'), array('password','require','密码必填'), array('repassword','require','重复密码必填'), array('password','repassword','两次密码不一致',0,'confirm'), array('createtime','number','您输入的不是数字'), array('createip','email','邮箱格式不正确'), ); function checklen($data){ if(strlen($data)>15 || strlen($data)<5){ return false; }else{ return true; } } } ?>
aoli/Home/Lib/Action/UserAction.class.php page is as follows:
<?php class UserAction extends Action { function reg(){ $this->display(); } function regadd(){ $user=D('user'); if($user->create()){ if($user->add()){ $this->success('注册成功'); }else{ $this->error('注册失败'); } }else{ $this->error($user->getError()); } } } ?>
2. Auto-complete (auto-fill)
Autocomplete is also a member method in ThinkPHP. When creating, it is automatically executed
The rules are as follows:
array('填充字段','填充内容','填充条件','附加规则');
A simple example is as follows:
protected $_auto = array ( //array( 'status','1'), // 新增的时候把 status 字段设置为 1 array('password','md5',1,'function') , // 对 password 字段在新增的时候使 md5 函数处理 array('createtime','time',3,'function' ), // 对 create_time 字段在更新的时候写入当前时间戳 );
2. Automatic mapping (field mapping)
Automatic mapping: Map database fields into aliases, and you can use aliases in forms .
A simple example is as follows:
protected $_map = array( 'name' => 'username', 'pass' => 'password', );
The detailed techniques described in this article will be helpful to everyone in learning and using ThinkPHP.
Caching problem, sometimes even if DEBUG is turned on, the runtime folder will be deleted. If you are logged in, please log out and log in again.
You remove $this->error() so that $User->getError() can be output. Because you have already displayed it above, it blocks subsequent display. The error message is displayed after it is obtained through getError().