Verify the form registered to the test table
Verify the form before registration:
Verify that the user name is not empty, twice The entered password must be consistent (equality verification), the age must be between 18 and 50 (range verification), and the email format must be regular verification.
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.
Data verification can perform verification operations on data types, business rules, security judgments, etc.
There are two methods of data validation:
Static method: Define validation rules through the $_validate attribute in the model class.
Dynamic method: Use the validate method of the model class to dynamically create automatic validation rules.
No matter what method is used, the definition of verification rules is a unified rule, and the definition format is:
array( array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]), array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]), ...... );
Validation field (required)
The name of the form field that needs to be verified. This field is not necessarily a database field, but can also be some auxiliary fields of the form, such as confirming passwords and verification codes, etc. When there are individual validation rules that have nothing to do with fields, the validation fields can be set at will. For example, expire validity rules have nothing to do with form fields. If field mapping is defined, the validation field name here should be the actual data table field rather than the form field.
Verification rules (required)
The rules for verification need to be combined with additional rules. If additional rules for regular verification are used, the system also has some built-in rules. Commonly used regular verification rules can be used directly as verification rules, including: require field, email address, url address, currency, and number.
Prompt information (required)
Used to define prompt information after verification failure
Verification conditions (optional)
Includes the following situations:
self::EXISTS_VALIDATE or 0, verify if the field exists (default)
self ::MUST_VALIDATE or 1 Must verify
self::VALUE_VALIDATE or 2 Verify when the value is not empty
Additional rules ( Optional)
Use with verification rules, including the following rules:
Verification time (optional)
self::MODEL_INSERT or 1 verify when adding data
1. Static verification
Predefine the automatic verification rules of the model in the model class, which we call static definition. When verifying, add verification conditions to the Model of the test table: Create a new testModel.class.php, and define the $_validate attribute in the model class as follows:Copy after login
assign("error",$cw); $this->display(); } else { $model = new \Home\Model\testModel(); //$model = D("test"); //动态验证可以用D方法 if(!$model->create()) { $e = $model->getError(); $url = "ZhuCe/cw/{$e}"; $this->error("注册失败!",$url,1); } else { $model->add(); }
2. Dynamic verification
If you adopt dynamic verification, it is more flexible. You can use different verification rules when operating the same model according to different needs, such as the above The static verification method can be changed to:display(); } else { //$model = new \Home\Model\testModel(); $model = D("test"); //动态验证可以用D方法 //动态验证 $rules = array( array('uid','require','用户名不能为空') ); //调用validate()加入验证规则 $r = $model->validate($rules)->create();//若验证失败返回false,成功返回注册的test表数组信息 //var_dump($r); if(!$r) { echo $model->getError(); //若验证失败则输出错误信息 } else { $model->add(); } } }