Home Backend Development PHP Tutorial ThinkPHP框架项目开发课题-自动验证

ThinkPHP框架项目开发课题-自动验证

Jun 13, 2016 pm 01:07 PM
array email model thinkphp validate

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的项目模块制作和项目实例,希望大家支持。

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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.

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.

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.

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.

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

See all articles