About automatic verification of create() method in ThinkPHP

不言
Release: 2023-03-30 20:30:02
Original
1773 people have browsed it

The following brings you an example of automatic verification of the create() method in ThinkPHP. The content is quite good, so I will share it with you now and give it as a reference.

Automatic verification is a data verification method provided by the ThinkPHP model layer, which can automatically perform data verification when using create to create a data object.

Principle:

##The create() method collects form ($_POST) information and returns it, while triggering automatic verification of the form and filtering illegal fields,

Using the create() method in the controller, (the return value is true/false), will automatically trigger the $_validate attribute in the model class (it is the method in the parent class Model, in the subclass Rewritten in Model), customize the verification rules in $_validate (the verification rules will be explained in detail below), when the create() method has no data and the return value is false, obtain and Return error message!

Using automatic verification must be defined according to the following rule format:

protected $_validate = array(  
 array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]), 
 array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]),
  ......
);
Copy after login

where verification fields, verification rules , the error prompt is required, the verification conditions, additional rules, and verification time are optional!

Validation field (required): form field.

Validation Rules (required): The require field must include email address, url URL address, and number. It can also be used in conjunction with additional rules.

Error prompt (required): Prompt message returned when verification fails.

Verification conditions (optional): There are three types: 0, 1, and 2. 0: Field verification that exists in _POST, default; 1: Verification is required if the verification rules are defined; 2: When the value is not empty Verification.

Additional rules:

##regexfunctioncallbackconfirmequalnotequalin notinlengthbetweennotbetweenexpireip_allowip_denyunique

验证时间(可选):共有1,2,3三种,1:新增数据时候验证;2:编辑数据时候验证;3:全部情况下验证(默认);也可以可以根据业务需要增加其他的验证时间

下面附上代码:以注册为例

前台页面比较简单,代码就不贴出来了,下面是前台注册界面截图

控制器代码:

//注册
 public function register(){
  $user = new \Model\UserModel();

  //两个逻辑:收集,展示
  if (!empty($_POST)) {
  
  //create()方法收集表单($_POST)信息并返回,同时触发表单自动验证,过滤非法字段
  $date = $user->create();
  //通过create()方法的返回值$date判断验证是否成功
  if ($date) { //返回实在数据的时候才进行添加
   //implode()把数组变为字符串
   $date['user_hobby'] = implode(',', $date['user_hobby']);
   $info = $user->add($date);
   if ($info) {
   
   //跳转首页   
   $this->redirect('Index/index');
   }
  }else{

   //把错误信息分配到前台模板
   $error = $user->getError();
   $this->assign('error',$error);
  }
  }
  //调用view视图
  $this->display();
 }
Copy after login

模型类代码:

class UserModel extends Model{
 
 //是否批量处理验证,批量获取全部的错误验证信息
 protected $patchValidate = true; //默认为false

 //自动验证定义
 protected $_validate = array(

  //array(字段,验证规则,错误提示,验证条件,附加规则,验证时间)
  //①用户名验证,不能为空
  array('username','require','用户名不能为空'),
  array('username','','该用户名已经被占用','0','unique'),
  //②密码验证,不为空
  array('password','require','密码不能为空'),
  //③验证确认密码,必须填写,与密码保持一致
  array('password2','require','确认密码必须填写'),
  array('password2','password','两次密码保持一致',0,'confirm'),
  //④邮箱验证
  array('user_email','email','邮箱格式不正确',2),
  //⑤qq验证,数字组成,5-12位
  array('user_qq','number','qq必须是数字'),
  array('user_qq','5,12','位数在5-12位之间',0,'length'),
  //⑥学历验证,必须选一个
  array('user_xueli','2,5','学位必须选择一个',0,'between'),
  //⑦爱好验证,必须选择二个以上
  //因为爱好返回的是数组,附加规则中没有可以直接用的规则,所以需自定义方法,用callback方法验证
  array('user_hobby','check_hobby','爱好必须选两项或以上',1,'callback'),
  );

 //定义方法进行爱好验证
 //参数$arg代表被验证的表单信息
 function check_hobby($arg)
 {
  //判断数组长度是否大于2
  if (count($arg)<2) {
   return false; //会自动输出验证错误信息
  }
  return true;
 }
}
Copy after login

把验证的错误信息在模板中给展示出来(部分代码)

<td style="width:13%; text-align: right;">
 <label for="User_username" class="required">用户名 
 <span>*</span></label>
</td>

<td style="width:87%;">
 <input class="inputBg" size="25" name="username" id="User_username" type="text" value="" />     
 <span style="color:red;"><{$error.username|default:""}></span>
</td>
Copy after login

结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

对THINKPHP的addAll支持的最大数据量的分析

关于ThinkPHP CURD方法之table方法

关于ThinkPHP利用getlist方法实现数据搜索功能的分析

The above is the detailed content of About automatic verification of create() method in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Regular verification, defined The validation rule is a regular expression (default)
Function verification, the defined validation rule is a function name
Method verification, the defined verification rule is a method of the current model class
Verify whether the two fields in the form Same, the defined validation rule is a field name
to verify whether it is equal to a certain value, which value is defined by the previous validation rule
Verify whether it is not equal to a certain value, which is defined by the previous validation rule (new in version 3.1.2)
Verify whether it is within a certain range. The defined verification rule can be an array or a comma-separated string.
Verify whether it is not within a certain range. Within a certain range, the defined verification rule can be an array or a comma-separated string (new in version 3.1.2)
Verification length, defined The validation rule can be a number (representing a fixed length) or a range of numbers (for example, 3,12 represents a range of length from 3 to 12)
Validation range , the defined verification rules represent the range, and you can use strings or arrays, such as 1,31 or array(1,31)
Verification is not within a certain range , the defined verification rules represent the range, you can use strings or arrays (new in version 3.1.2)
Verify whether it is within the validity period, the defined verification rules Indicates the time range, which can be up to the time. For example, you can use 2012-1-15, 2013-1-15 to indicate that the current submission validity period is between 2012-1-15 and 2013-1-15. You can also use the timestamp definition
Verify whether the IP is allowed. The defined verification rules represent a list of allowed IP addresses, separated by commas, such as 201.12.2.5,201.12.2.6
Verify whether the IP is banned. The defined verification rules represent a list of banned IP addresses, separated by commas, such as 201.12.2.5,201.12.2.6
To verify whether it is unique, the system will query the database based on the current value of the field to determine whether the same value exists. When the form data contains the primary key field, unique cannot be used to determine the primary key field itself