Brief introduction of three automation in ThinkPHP

  • 2021-07-16 01:55:28
  • OfStack

This article describes the ThinkPHP in more detail of the three automatic, is a very important application, to share for your reference. The details are as follows:

1. Automatic validation

The format is as follows:


array(' Validation field ',' Verification rule ',' Error prompt ',' Verification condition ',' Additional rule ',' Verification time ') 

Parameter description:

Validation field: The form field name needs to be validated
Validation rule: Must be used in conjunction with additional rule 1
Error Prompt: If an error occurs, what kind of error prompt will be thrown to inform the user
Validation criteria: 0, 1, 2
Additional rules: 1. regex uses regular validation 2, function uses function validation 3, callback callback 4, confirm verifies whether two fields in the form are the same 5, verifies whether it is equal to a certain value 6, in is in a certain range 7, and verifies whether it is only 1
TP encapsulation: require fields must be validated; eamil authentication mailbox; url verifies the url address; currency currency; number digits;
Verification time: refers to the verification time of database operation time, and verifies Model when adding data:: MODEL_INSERT; Verify Model when editing:: MODEL_UPDATE; Verify Model in all cases:: MODEL_BOTH;

The aoli/Home/Tpl/default/User/reg. html page reads as follows:


<form action="__URL__/regadd" method="post">
  User name :<input type="text" name="username" /><br />
  Password: <input type="password" name="password" /><br />
  Duplicate password: <input type="password" name="repassword" /><br />
  Registration time: <input type="text" name="createtime" /><br />
  Registration IP : <input type="text" name="createip" /><br />
 <input type="submit" value=" Registration " />
</form>

The aoli/Home/Lib/Model/UserModel. class. php page reads as follows:


<?php
class UserModel extends Model{// Corresponding tables in the database user
  protected $_validate=array(
     array('username','require',' User name required '),
     array('username','checklen',' User name too long or too short ',0,'callback'),
     array('password','require',' Password required '),
     array('repassword','require',' Duplicate password required '),
     array('password','repassword',' Twice password no 1 To ',0,'confirm'),
     array('createtime','number',' You are not entering a number '),
     array('createip','email',' The mailbox format is incorrect '),
  ); 
  function checklen($data){
    if(strlen($data)>15 || strlen($data)<5){
      return false;
    }else{
      return true;
    }
  }
     
 }
?>

The aoli/Home/Lib/Action/UserAction. class. php page reads as follows:


<?php
 class UserAction extends Action {
 function reg(){
   $this->display();
 }
 function regadd(){
   $user=D('user');
   if($user->create()){
     if($user->add()){
       $this->success(' Successful registration ');
     }else{
       $this->error(' Registration failed ');
     }
   }else{
     $this->error($user->getError());
   } 
 } 
}
?>
 

2. Auto Complete (Auto Fill)

Auto-completion is also a member method in ThinkPHP. When create, it executes automatically

The rules are as follows:


array(' Fill in a field ',' Fill in content ',' Filling condition ',' Additional rule ');

A simple example is as follows:


protected $_auto = array ( 
   //array( 'status','1'),  //  When adding, put  status  Field is set to  1
   array('password','md5',1,'function') , //  Right  password  When adding a field, make the  md5  Function processing 
   array('createtime','time',3,'function' ), //  Right  create_time  The current timestamp is written to the field when it is updated 
); 

2. Automatic mapping (field mapping)

Automatic mapping: Map the fields of the database to aliases, and you can use aliases in the form.

A simple example is as follows:


protected $_map = array(  
  'name' => 'username',
  'pass' => 'password',
); 

The detailed skills described in this paper are helpful for everyone to learn and use ThinkPHP.


Related articles: