Yii2 Core Validators basic usage, yii2validators

Source: Internet
Author: User
Tags idn valid email address

Yii2 Core Validators basic usage, yii2validators

Core Validators)

Yii provides a series of common core validators, which are mainly stored in the yii \ validators namespace. To avoid lengthy class names, you can use nicknames to specify the corresponding core validators. For example, you can use 'required' to represent the yii \ validators \ RequiredValidator class, for example:

1 public function rules()2 {3     return [4         [['email', 'password'], 'required'],5     ];6 }

The yii \ validators \ Validator: builtInValidators attribute declares all supported validators nicknames.

public static $builtInValidators = [        'boolean' => 'yii\validators\BooleanValidator',        'captcha' => 'yii\captcha\CaptchaValidator',        'compare' => 'yii\validators\CompareValidator',        'date' => 'yii\validators\DateValidator',        'default' => 'yii\validators\DefaultValueValidator',        'double' => 'yii\validators\NumberValidator',        'each' => 'yii\validators\EachValidator',        'email' => 'yii\validators\EmailValidator',        'exist' => 'yii\validators\ExistValidator',        'file' => 'yii\validators\FileValidator',        'filter' => 'yii\validators\FilterValidator',        'image' => 'yii\validators\ImageValidator',        'in' => 'yii\validators\RangeValidator',        'integer' => [            'class' => 'yii\validators\NumberValidator',            'integerOnly' => true,        ],        'match' => 'yii\validators\RegularExpressionValidator',        'number' => 'yii\validators\NumberValidator',        'required' => 'yii\validators\RequiredValidator',        'safe' => 'yii\validators\SafeValidator',        'string' => 'yii\validators\StringValidator',        'trim' => [            'class' => 'yii\validators\FilterValidator',            'filter' => 'trim',            'skipOnArray' => true,        ],        'unique' => 'yii\validators\UniqueValidator',        'url' => 'yii\validators\UrlValidator',        'ip' => 'yii\validators\IpValidator',    ];

The following describes in detail the main usage and attributes of each validator.

1. yii \ validators \ BooleanValidator this validator checks whether the input is a Boolean value.

1 public function rules () 2 {3 return [4 // check whether 'data _ one' is 0, 1, true, false, when the value is 0 or 1, ignore the data Type 5 ['data _ one', 'boolean'], 6 7 // check whether 'date _ two' is boolean, true and false 8 ['data _ two', 'boolean', 'truevalue '=> true, 'falsevalue' => false, 'strict' => true], 9]; 10}

TrueValue: true value. The default value is '1 '.
FalseValue: false value. The default value is '0 '.
Strict: whether the input value must match trueValue or falseValue strictly. The default value is false.

Note: Because the input data transmitted through HTML forms are of the string type, the strict attribute must be set to false in general.

Ii. yii \ captcha \ CaptchaValidator Verification Code validators

This validator is usually used with yii \ captcha \ CaptchaAction and yii \ captcha \ Captcha to ensure that the entered value is the same as the verification code displayed by the yii \ captcha \ Captcha widget.

1 return [2 // captchaAction directs to the route of yii \ CAPTCHA \ CaptchaAction used to render the captcha image. The default value is 'site/captcha '3 // whether the caseSensitive verification code is case-sensitive. The default value is false4 // skipOnEmpty. If the input is empty, skip verification. The default value is false, that is, you must enter 5 ['verifycode', 'captcha ', 'captchaaction' => 'login/captcha', 'casesensitive '=> false, 'skiponempty' => false], 6];

Iii. yii \ validators \ CompareValidator checks whether the relationship between two specific input values is the same as that specified by the operator attribute.

1 public function rules () 2 {3 return [4 // check whether the value of 'data _ one' is the same as that of 'data _ one_repeat '. 5 // The compareAttribute attribute is not set, by default, the attribute with the suffix '_ repeat' is compared with the attribute with the target attribute. 6 ['data _ one', 'compare'], 7 8 // After the compareAttribute attribute is set, the object to be compared is the target attribute 9 ['data _ two', 'compare', 'compareattribute' => 'data _ three '], 10 11 // compareValue sets the constant value 12 compared with data_one // when both compareValue and compareAttribute are set, compareValue13 ['data _ four ', 'company ', 'operation' => 10, 'operator' => '> ='], 14]; 15}

Operator: comparison operator.
=: Check whether the two values are equal. Non-strict mode.
===: Check whether the two values are full. Strict mode.
! =: Check whether the two values are different. Non-strict mode.
! =: Check whether the two values are incomplete. Strict mode.
>: Check whether the target value to be tested is greater than the specified value.
>=: Check whether the target value to be tested is greater than or equal to the given tested value.
<: Check whether the target value to be tested is smaller than the specified value.
<=: Check whether the target value to be tested is less than or equal to the given tested value.

Iv. yii \ validators \ DefaultValueValidator this validator assigns a default value to a blank target attribute without data verification.

1 public function rules () 2 {3 return [4 // if the value of data_one is null, set it to test 5 ['data _ one', 'default ', 'value' => 'test'], 6 // if the value of data_two is null, set the value $ attribute through the callback function as the target attribute, here is data_two 7 ['data _ two', 'default', 'value' => function ($ model, $ attribute) {8 return 'value'; 9}], 10]; 11}
1 // basic usage of the callback function 2 function foo ($ model, $ attribute) {3 //... calculate $ value... 4 return $ value; 5}

V. yii \ validators \ NumberValidator check whether the input value is a double-precision floating point number.

1 public function rules () 2 {3 return [4 // the validator double is equivalent to number 5 // check whether data_one is a number 6 ['data _ one ', 'double'], 7 // max sets the upper limit min sets the lower limit, all contain the demarcation point. If this parameter is not set, the upper and lower limits of 8 ['data _ two', 'number', 'max '=> 10, 'Min' => 5], 9]; 10} are not checked}

Vi. yii \ validators \ EachValidator verify that each element in the array meets certain rules

1 public function rules () 2 {3 return [4 // verify that all elements in the array data_one are numerical values 5 ['data _ one', 'REACH ', 'rule' => ['integer'], 6 7 // verify that the elements in the array data_two are boolean values 8 ['data _ two', 'each ', 'rule' => ['boolean'], 'allowmessagefromrule' => false, 'message' => 'must be a boolean value'], 9]; 10}

The value to be verified must be an array.
Rule: Specifies a validation rule through an array. The first element of the array is the class name or alias of the validators. The remaining elements are configured as key-value pairs.
AllowMessageFromRule: whether to use the error message returned by the reference validators. The default value is true. If it is set to false, you must set the message attribute.
Note: If the value to be verified is not an array, it will be considered as a verification failure, and the Set message will be returned as an error message.

VII. yii \ validators \ EmailValidator check whether the input value is a valid email address

1 public function rules () 2 {3 return [4 // verify whether email is a valid email address; 5 ['email ', 'email'], 6]; 7}

AllowName: Check whether email addresses with names are allowed (e.g. Michael <John.san@example.com> ). The default value is false.
CheckDNS: Check whether the email domain name exists and whether there are corresponding A or MX records. Sometimes this check may fail due to a temporary DNS failure, even if it is effective. The default value is false.
EnableIDN: whether IDN (internationalized domain names) should be considered during the verification process ). The default value is false. To use the IDN verification function, first install and enable the intl PHP extension. Otherwise, an exception is thrown.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.