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

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
