Table of Contents
thinkPHP form automatic verification function, thinkphp form verification
Home Backend Development PHP Tutorial thinkPHP form automatic verification function, thinkphp form verification_PHP tutorial

thinkPHP form automatic verification function, thinkphp form verification_PHP tutorial

Jul 13, 2016 am 10:11 AM
thinkphp

thinkPHP form automatic verification function, thinkphp form verification

Last night our boss asked me to work on the form automatic verification function, and I spent a long time worrying about it and borrowed a lot of knowledge from official websites before I came up with it, eh , anyway, let me share my own results!

thinkphp defines automatic verification functions and regular expressions for us in the Model base class. We only need to establish the $_validate attribute under the model class of the corresponding database table.

1. We find the Model base class and can see protected $_validate = array(); // Automatically verify that it is of array type. Define it in the corresponding data model file below;

2,

public <span>function</span><span> CheckVerify($verify) {

   </span><span>if</span> (md5($verify) != Session::get('verify')) <span>return</span> <span>false</span><span>;
        </span><span>return</span> <span>true</span><span>;
}</span>
Copy after login

//Automatic verification

   protected $_validate =<span> array(
        array(</span>"title", "require", "标题必须!"<span>),
        array(</span>'categoryId', 'require', "类别必须!"<span>),
        array(</span>'content', 'require', "内容必须!"<span>),
        array(</span>'verify', 'require','验证码必须!'<span>),
        array(</span>'verify', 'CheckVerify', '验证码错误!', 0, 'callback'<span>)
    );</span>
Copy after login


3. Format description: array (validation fields, validation rules, error prompts, validation conditions, additional rules, validation time),

4. Parameter explanation:

Verification field: 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.
Verification rules: The rules for verification need to be combined with additional rules (required). The official rules included are as follows (you can also add them yourself):

 1 $validate =<span> array(
 </span>2 
 3             'require'=> '/.+/'<span>,
 </span>4 
 5             'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/'<span>,
 </span>6 
 7             'url' => '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/'<span>,
 </span>8 
 9             'currency' => '/^\d+(\.\d+)?$/'<span>,
</span>10 
11             'number' => '/^\d+$/'<span>,
</span>12 
13             'zip' => '/^[1-9]\d{5}$/'<span>,
</span>14 
15             'integer' => '/^[-\+]?\d+$/'<span>,
</span>16 
17             'double' => '/^[-\+]?\d+(\.\d+)?$/'<span>,
</span>18 
19             'english' => '/^[A-Za-z]+$/'<span>,
</span>20 
21         );<br /><br />
Copy after login

Prompt information: Definition of prompt information after verification failure (required),
Verification conditions: (optional)
There are three rules for verification conditions:
Model::EXISTS_TO_VAILIDATE or 0 field exists Just verify (default)
Model::MUST_TO_VALIDATE or 1 Must verify
Model::VALUE_TO_VAILIDATE or 2 Verify when the value is not empty
Additional rules:
regex regular verification, indicating the previous verification The rule is a regular expression;
function uses function verification, indicating that the previous verification is a function name;
callback uses method verification, indicating that the verification rule is a method of the Model class;
confirm verifies that in the form Whether two fields are equal, the verification rule is a field name;
equal verifies whether it is equal to a certain value, which is defined by the previous verification rule;
in verifies whether it is within a certain range, defined previously is an array;
unique is used to verify whether it is unique. The system will query the database based on the current value of the field to determine if the same value exists;
At the same time, the system also has built-in some commonly used regular verification rules, which can be used in this section , including: require field must be email;
currency, number. These verification rules can be used directly;

Verification time: (optional)

01.Model:: MODEL_INSERT or 1 to verify when adding data
02.Model:: MODEL_UPDATE or 2 to verify when editing data
03.Model:: MODEL_BOTH or 3 to verify in all cases (default)

5. Official example:

protected $_validate  =<span>  array(   
array(</span>'verify','require','验证码必须!'), <span>//</span><span>默认情况下用正则进行验证   </span>
array(name,'','帐号名称已经存在!',0,&rsquo;unique&rsquo;,1), <span>//</span><span> 在新增的时候验证name字段是否唯一   </span>
array('value',array(1,2,3),'值的范围不正确!',2,&rsquo;<span>in</span>&rsquo;), <span>//</span><span> 当值不为空的时候判断是否在一个范围内   </span>
array('repassword','password','确认密码不正确',0,&rsquo;confirm&rsquo;), <span>//</span><span> 验证确认密码是否和密码一致   </span>
array('password','checkPwd','密码格式不正确',0,&rsquo;<span>function</span>&rsquo;)<span>//</span><span> 自定义函数验证密码格式  </span>
);
Copy after login

I don’t know if it meets the requirements of our boss, but it’s still coming out. It’s so hard! ! I spent the whole night looking for codes, cases, and comfort! !

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/930046.htmlTechArticlethinkPHP form automatic verification function, thinkphp form verification Last night our boss asked me to work on the form automatic verification function, and I was worried about it for a long time. I borrowed a lot of knowledge from official websites before I came out, eh, always...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

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.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

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.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

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 Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"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.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

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.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

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.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

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 Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

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.

See all articles