核心驗證器(Core Validators)
Yii提供了一系列常用的核心驗證器,主要存在於 yiivalidators 命名空間之下。為了避免使用冗長的類別名,可以直接用暱稱來指定對應的核心驗證器。 例如可以用 'required' 暱稱代指 yiivalidatorsRequiredValidator 類,例如:
<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> }
yiivalidatorsValidator::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>
下面詳細介紹每個驗證器的主要用法和屬性。