ThinkPHP框架项目开发专题--自动验证
在WEB项目的开发中,项目的任何模块都会有数据添加的功能--比如用户注册、新闻添加、商品管理等,那么我们就会用到表单来传输数据,而添加数据时无论是前台用户还是后台管理员都避免不了填写无效数据或者错误数据,那么表单数据的验证就是我们在做项目中必不可少的。所以如果大家参与或者独立开发过WEB项目的话一定会发现在添加数据时大量的数据验证--比如用户名是否存在、验证码是否正确、密码是否填写一致、年龄是否是数字等问题,那么就会用到大量的数据库查找、正则表达式的编写等问题困扰大家。
在ThinkPHP中,内置了数据对象的自动验证和自动完成功能来完成模型的业务规则验证,而大多数情况下面,数据对象是由表单提交的$_POST数据创建。需要使用系统的自动验证功能,只需要在Model类里面定义$_validate属性。
$_validate属性的验证因子格式:
array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
*验证字段:表单字段名称,注:这个字段不一定是数据库字段,也可以是表单的一些辅助字段--比如确认密码和验证码等等。
*验证规则: 要进行验证的规则,有时需要结合附加规则。
内置验证规则--包括:require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字,这些验证规则可以直接使用。注:如有其它验证规则则需自己编写方法
*提示信息: 用于验证失败后的提示信息。
验证条件:
0 或者 Model::EXISTS_TO_VAILIDATE--存在字段就验证 (默认)
1 或者 Model::MUST_TO_VALIDATE--必须验证
2 或者 Model::VALUE_TO_VAILIDATE--值不为空的时候验证
附加规则: 配合验证规则使用
regex 使用正则进行验证,表示前面定义的验证规则是一个正则表达式(默认)
function 使用函数验证,前面定义的验证规则是一个函数名 注:系统函数或自定义函数
callback 使用方法验证,前面定义的验证规则是当前Model类的一个方法 注:自定义方法
confirm 验证表单中的两个字段是否相同,前面定义的验证规则是一个字段名
equal 验证是否等于某个值,该值由前面的验证规则定义
in 验证是否在某个范围内 注:前面定义的验证规则必须是一个数组
unique 验证是否唯一,系统会根据字段目前的值查询数据库来判断是否存在相同的值 注:会请求数据库
验证时间:
1 或者 Model:: MODEL_INSERT--新增数据时候验证
2 或者 Model:: MODEL_UPDATE--编辑数据时候验证
3 或者 Model:: MODEL_BOTH--全部情况下验证(默认)
熟悉了$_validate属性后,就要用实例来分析在验证某些数据的时候该怎么样编写验证因子。
如果学习过ThinkPHP的同学们应该在手册中看到过一些关于自动验证的例子,在这里我们将把大多数常用的例子都总结在这里,方便大家来学习和使用,如果有一些同学们常用但是这里没有提到的大家可以集思广益,来完善所有的自动验证数据的方法,这里将不断更新。
实例:
protected $_validate = array(
array('username','require','用户名必须!'), // 数据是否为空 注:默认增加修改都验证
array('username','','用户名已经存在!',0,’unique’,1), // 在新增的时候验证username字段是否唯一
array('password','checkPwd','密码格式不正确',0,’function’), // 密码格式可以用chenkPwd方法自定义
array('repassword','password','确认密码不正确',0,’confirm’), // 验证确认密码是否和密码一致
array('sex','array(0,1,2)','性别必须为0,1,2',0,'in'), // 验证数据是否在一个范围内
array('age','number','年龄必须为数字'), // 验证数据是否为数字
array('email','email','邮箱格式不正确'), // 内置正则验证邮箱
array('email','/^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$/','邮箱格式不正确), // 自定义正则验证数据
array('mypage','url','个人网址格式不正确'), // 内置正则验证URL地址
array('verify','****','验证码不正确',0,'equal'), // 验证数据是否等于某个值 注:****可以是随机验证码
array('salary','currency','薪水验证不正确','0'), // 内置验证货币数据
);
ThinkPHP的自动验证功能几乎可以满足任何数据验证的需求,所以大家在用ThinkPHP做项目中的数据添加并验证的时候,利用自动验证和自动完成功能就不用担心数据的验证这个麻烦的问题了。自动完成功能下次讲解和总结,并且还会陆续发布ThinkPHP的项目模块制作和项目实例,希望大家支持。

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

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

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor