Core Validators
Yii provides a series of commonly used core validators, mainly existing under the yiivalidators namespace. To avoid using lengthy class names, you can directly specify the corresponding core validator by nickname. For example, you can use the nickname 'required' to refer to the yiivalidatorsRequiredValidator class, such as:
1 public function rules() 2 { 3 return [ 4 [['email', 'password'], 'required'], 5 ]; 6 }
yiivalidatorsValidator::builtInValidators attribute declares all supported validator 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 main usage and properties of each validator are introduced in detail below.