Home > Backend Development > PHP Tutorial > Yii2 validator (Validator) usage analysis_php example

Yii2 validator (Validator) usage analysis_php example

WBOY
Release: 2016-08-04 08:56:45
Original
1000 people have browsed it

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],
  ];
}

Copy after login

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.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template