yii\validators\FileValidator::init PHP Method

init() public method

public init ( )
    public function init()
    {
        parent::init();
        if ($this->message === null) {
            $this->message = Yii::t('yii', 'File upload failed.');
        }
        if ($this->uploadRequired === null) {
            $this->uploadRequired = Yii::t('yii', 'Please upload a file.');
        }
        if ($this->tooMany === null) {
            $this->tooMany = Yii::t('yii', 'You can upload at most {limit, number} {limit, plural, one{file} other{files}}.');
        }
        if ($this->wrongExtension === null) {
            $this->wrongExtension = Yii::t('yii', 'Only files with these extensions are allowed: {extensions}.');
        }
        if ($this->tooBig === null) {
            $this->tooBig = Yii::t('yii', 'The file "{file}" is too big. Its size cannot exceed {formattedLimit}.');
        }
        if ($this->tooSmall === null) {
            $this->tooSmall = Yii::t('yii', 'The file "{file}" is too small. Its size cannot be smaller than {formattedLimit}.');
        }
        if (!is_array($this->extensions)) {
            $this->extensions = preg_split('/[\\s,]+/', strtolower($this->extensions), -1, PREG_SPLIT_NO_EMPTY);
        } else {
            $this->extensions = array_map('strtolower', $this->extensions);
        }
        if ($this->wrongMimeType === null) {
            $this->wrongMimeType = Yii::t('yii', 'Only files with these MIME types are allowed: {mimeTypes}.');
        }
        if (!is_array($this->mimeTypes)) {
            $this->mimeTypes = preg_split('/[\\s,]+/', strtolower($this->mimeTypes), -1, PREG_SPLIT_NO_EMPTY);
        } else {
            $this->mimeTypes = array_map('strtolower', $this->mimeTypes);
        }
    }

Usage Example

 public function init()
 {
     parent::init();
     $this->maxFiles = 1;
     $this->maxSize = 20971520;
     if (!empty($this->params)) {
         foreach ($this->params as $prop => $value) {
             if (property_exists($this, $prop)) {
                 $this->{$prop} = $value;
             }
         }
     }
     // turning mime types into strings
     foreach ($this->mimeTypes as $i => $mimeType) {
         if ($mimeType instanceof MimeType) {
             $this->mimeTypes[$i] = $mimeType->name;
         }
     }
     $filesAllowed = !empty($this->mimeTypes);
     $this->mimeTypes = $filesAllowed ? $this->mimeTypes : ['.'];
     /*
      * We placing dot if there is no mimeTypes because for yii FileValidator "no mime types" means
      * that all types is allowed, which is not what we want
      */
     if (!$filesAllowed) {
         $this->wrongExtension = 'File posting is not allowed on this board';
     }
     $this->notArray = 'Attribute is not an array';
 }
All Usage Examples Of yii\validators\FileValidator::init