核心验证器(Core Validators)
Yii提供了一系列常用的核心验证器,主要存在于 yii\validators 命名空间之下。为了避免使用冗长的类名,可以直接用昵称来指定相应的核心验证器。 比如可以用 'required' 昵称代指 yii\validators\RequiredValidator 类,比如:
<span style="color: #008080;">1</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> rules() </span><span style="color: #008080;">2</span> <span style="color: #000000;">{ </span><span style="color: #008080;">3</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> [ </span><span style="color: #008080;">4</span> [['email', 'password'], 'required'], <span style="color: #008080;">5</span> <span style="color: #000000;"> ]; </span><span style="color: #008080;">6</span> }
yii\validators\Validator::builtInValidators 属性声明了所有被支持的验证器昵称。
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$builtInValidators</span> =<span style="color: #000000;"> [ </span>'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' =><span style="color: #000000;"> [ </span>'class' => 'yii\validators\NumberValidator', 'integerOnly' => <span style="color: #0000ff;">true</span>,<span style="color: #000000;"> ]</span>, 'match' => 'yii\validators\RegularExpressionValidator', 'number' => 'yii\validators\NumberValidator', 'required' => 'yii\validators\RequiredValidator', 'safe' => 'yii\validators\SafeValidator', 'string' => 'yii\validators\StringValidator', 'trim' =><span style="color: #000000;"> [ </span>'class' => 'yii\validators\FilterValidator', 'filter' => 'trim', 'skipOnArray' => <span style="color: #0000ff;">true</span>,<span style="color: #000000;"> ]</span>, 'unique' => 'yii\validators\UniqueValidator', 'url' => 'yii\validators\UrlValidator', 'ip' => 'yii\validators\IpValidator',<span style="color: #000000;"> ];</span>
下面详细介绍每一款验证器的主要用法和属性。