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:
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:
##regex
Regular verification, defined The validation rule is a regular expression (default)
function
Function verification, the defined validation rule is a function name
callback
Method verification, the defined verification rule is a method of the current model class
confirm
Verify whether the two fields in the form Same, the defined validation rule is a field name
equal
to verify whether it is equal to a certain value, which value is defined by the previous validation rule
notequal
Verify whether it is not equal to a certain value, which is defined by the previous validation rule (new in version 3.1.2)
in
Verify whether it is within a certain range. The defined verification rule can be an array or a comma-separated string.
notin
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)
length
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)
between
Validation range , the defined verification rules represent the range, and you can use strings or arrays, such as 1,31 or array(1,31)
notbetween
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)
expire
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
ip_allow
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
ip_deny
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
unique
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!
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
To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.
ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.
Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.
"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.
Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.
ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.
ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.
Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.