本文实例讲述了Yii2实现自定义独立验证器的方法。分享给大家供大家参考,具体如下:

新建一个文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
/**
 * author   : forecho <caizhenghai@gmail.com>
 * createTime : 2015/7/1 14:54
 * description:
 */
namespace 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 . '必须是一个数组');
    }
  }
}
 
使用的时候:
public function rules()
{
  return [
    ...
    ['kind_ids', 'common\helps\ArrayValidator'], // 自定义验证
    ...
  ];
}

 

相关文章:

  • 2021-09-27
  • 2022-02-26
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2021-06-05
  • 2022-12-23
  • 2021-05-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案