Detailed YII2 how to implement a custom standalone validator

Source: Internet
Author: User
Tags yii
Yii2 How do I implement a custom standalone validator? This paper mainly introduces the method of implementing custom independent validator in Yii2, and analyzes the implementation and usage of YII2 custom independent validator with instance form. Need friends can refer to, hope to help you.

The examples in this article describe how YII2 implements a custom independent validator. Share to everyone for your reference, as follows:

Create a new file:

<?phpnamespace common\helps;use yii\validators\validator;class Arrayvalidator extends Validator{public  function ValidateAttribute ($model, $attribute)  {    if (!is_array ($model, $attribute)) {      $this Adderror ($model, $attribute, $attribute. ' must be an array ');}}}  

When used:

Public Function Rules () {  return [    ...    ] [' Kind_ids ', ' common\helps\arrayvalidator '],//custom validation    ...  ];}

Related recommendations:

Solve the problem of yii CFileCache getting no value

YII2 integrated Fast Search for efficient Chinese word search

How Yii Filters Bad code

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.