Home> PHP Framework> ThinkPHP> body text

Introduction to ThinkPHP framework form validation

Release: 2020-05-12 09:30:04
forward
2906 people have browsed it

Introduction to ThinkPHP framework form validation

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,验证规则,错误提示,[验证条件,附加规则,验证时间]), ...... );
Copy after login

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:

Introduction to ThinkPHP framework form validation

Verification time (optional)

  • self::MODEL_INSERT or 1 verify when adding data

  • ##self::MODEL_UPDATE or 2 verify when editing data

  • self::MODEL_BOTH or 3 Verify in all cases (default)

You need to pay attention to the verification time here. It is not the only three cases. You can according to business needs Add additional verification time.

There are two methods of verification: static verification and dynamic verification.

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

After defining the verification rules, It can be automatically called when using the create method to create a data object:

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(); }
Copy after login

Template ZhuCe.html:

 
用户名:

密码:

确认密码:

年龄:

邮箱:

姓名:

<{$error}>
Copy after login

Request the ZhuCe method:

Introduction to ThinkPHP framework form validation

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(); } } }
Copy after login

We can also display the error message directly behind the form, which requires the use of ajax. Take verifying that the user name is not empty as an example:

In the template ZhuCe.html:

 
用户名:

密码:

确认密码:

年龄:

邮箱:

姓名: