Yii2 validator (Validator) usage analysis_php example
The example in this article describes the usage of Yii2 Validator. Share it with everyone for your reference, the details are as follows:
Let’s take a look at the use of validators first.
public function rules() { return [ [['email', 'password'], 'required'], ['password', 'string', 'min'=>6], ]; }
As shown above, the validator is mainly used in rules to verify the attribute values in the current model to check whether it meets certain requirements.
Verifier usage format:
The writing format is: [attributes that need to be verified, validator name, validator parameters].
If the attributes that need to be verified are multiple available arrays, if one attribute is available, a string can also be represented by an array.
Each attribute can have multiple validators. For example, the password above uses two validators: required and string.
Commonly used validators:
Yii2 already has some commonly used validators built-in. All validators inherit from the base class yiivalidatorsValidator. I can roughly sum up the following categories.
Number related:
integer——integer
Used to detect whether the attribute value is an integer, as well as maximum and minimum value detection, etc. (yiivalidatorsNumberValidator)
double——Floating point
Used to detect whether the attribute value is a floating point number, that is, a decimal. (yiivalidatorsNumberValidator)
number——Number
This is exactly the same as the double above, just with 2 names. (yiivalidatorsNumberValidator)
Format related:
date——date
Verify that the attribute value is in the correct date format. (yiivalidatorsDateValidator)
email——mail
Detect if the attribute value is the correct email format. (yiivalidatorsEmailValidator)
url——URL
Used to determine whether the attribute value is the correct url address. (yiivalidatorsUrlValidator)
Perform function processing on values:
filter——filter
This is for processing attribute values. Such as prefixing attribute values, replacing specific strings, etc. (yiivalidatorsFilterValidator)
trim——Trimming
This is for processing attribute values. Just remove the spaces on both sides of the string, or the specified string. (yiivalidatorsFilterValidator)
Uploaded file verification:
file——file
This is mainly to verify the uploaded files, such as format, size, etc. (yiivalidatorsFileValidator)
image——picture
This is similar to the file validator above, but it is specially used to verify images. (yiivalidatorsImageValidator)
Judgment and comparison:
compare——Compare
Used to compare two attribute values, such as equality, greater than, less than comparison, etc. (yiivalidatorsCompareValidator)
in——include (range)
Used to detect whether the attribute value is contained in the specified array. (yiivalidatorsRangeValidator)
exist——existence
Used to detect whether this attribute value already exists in the data table. (yiivalidatorsExistValidator)
unique——uniqueness
This is similar to exist, used to detect whether the value is unique. (yiivalidatorsUniqueValidator)
string——string
Judge the length of attribute values, such as maximum length, minimum length, etc. (yiivalidatorsStringValidator)
boolean——Boolean type
Used to check whether the value of an attribute is a Boolean value. (yiivalidatorsBooleanValidator)
default——Default value
This is used to set default values for attributes. For example, when the attribute value is null, set it to an empty default value. (yiivalidatorsDefaultValueValidator)
required——Required
This is used to check whether the attribute value is empty. (yiivalidatorsRequiredValidator)
captcha——verification code
This is mainly used to verify the verification code when the interface uses it. (yiicaptchaCaptchaValidator)
match——regular expression
This is more powerful, used to detect whether the attribute value matches the given regular rule. Basically everything listed above can be implemented using this. (yiivalidatorsRegularExpressionValidator)
Others:
safe——safe
This does not perform verification, it is only used to specify attribute values and is safe. (yiivalidatorsSafeValidator)
Readers who are interested in more Yii-related content can check out the special topics on this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent PHP Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "Introduction to PHP Object-Oriented Programming" Tutorial", "php string usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to remove jquery from yii2: 1. Edit the AppAsset.php file and comment out the "yii\web\YiiAsset" value in the variable $depends; 2. Edit the main.php file and add the configuration "'yii" under the field "components" \web\JqueryAsset' => ['js' => [],'sourcePath' => null,]," to remove the jquery script.

Building a vue3 project Before creating a project, the first thing we need to explain is that the version we use is Nodejs: v17.5.0pnpm: 7.0.0Vue: 3.2.25. First, we Vite creates a vue3 project demo named FormValidate. Enter the command pnpmcreateviteFormValidate on the command line and press Enter, then select vue and continue to press Enter, indicating that we have initially created the FormValidate (form validation) project. According to the command line prompts, we enter the project root directory, and then use the command pnpminstall to install the dependencies required for the project. Of course Using pnpm here is better than n

How to achieve passwordless Windows on Windows 11 PC Hello open the Settings app. Click Account. Click on the login option. Under "Additional settings," turn on "Require Microsoft account to sign in with Windows Hello." Microsoft Authenticator App Download and install the Microsoft Authenticator app on your phone. It's available on both Android and iOS. Open the Microsoft Authenticator app and log in with your Microsoft account. On your Windows 11 device, open the Settings app. Click Account. Click on the login option. Under Additional Settings, turn on Require Microsoft

This article will introduce you to the yii2 framework, share a few CTF exercises, and use them to learn the yii2 framework. I hope it will be helpful to everyone.

With the continuous development of the Internet, more and more people are beginning to use PHP for website development. In PHP, using frameworks can greatly improve development efficiency, and ThinkPHP is a very popular framework. In ThinkPHP, the validator is a very important component that can help us verify the data submitted by users to ensure the correctness of the data. So this article will introduce how to use validators in ThinkPHP6. 1. What is a validator? A validator is used to perform validation on data.

1. You need to download the windows version of the master branch of yii2-redis with composer 2. Unzip and copy it to vendor/yiisoft 3. Add 'yiisoft/yii2-redis'=>array('name'=>'yiisoft to extensions.php under yiisoft /yii2-redis','version'=>'2.0.

CakePHP is a popular PHP framework that provides powerful data validation capabilities. Data validation is a very important task when developing web applications as it helps us ensure the correctness and security of the input data. CakePHP provides some built-in validators such as required field, number, email and uniqueness validation. However, in specific cases, we may need to create custom validators to suit the needs of our application. In this article we will explore how to use CakeP

The Yii framework is a high-performance, scalable, component-based PHP framework that comes with a large number of powerful tools and functions, making it easy to build efficient web applications. One of the very important components is the validator, which is mainly used to ensure the correctness of the data. In this article, we will take a deep dive into the validator in the Yii framework to understand its purpose, how to use it, and how it is implemented. 1. What is a validator? A validator refers to a functional module used to verify the validity of data. In Yii framework, there are mainly two types of validators: built-in validation
