# FileValidator

> FileValidator verifies if an attribute is receiving a valid uploaded file.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [validators](/docs/api/yii/validators/index.md) › FileValidator

| class | `yii\validators\FileValidator` |
| --- | --- |
| Extends | [`Validator`](/docs/api/yii/validators/validator/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/validators/FileValidator.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/validators/FileValidator.php#L27) |

1 subclass:

- [`ImageValidator`](/docs/api/yii/validators/imagevalidator/index.md)

FileValidator verifies if an attribute is receiving a valid uploaded file.

Note that you should enable `fileinfo` PHP extension.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-attributeNames"></span>[`$attributeNames`](/docs/api/yii/validators/validator/index.md#property-attributeNames) read-only | `array` | Attribute names. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="property-attributes"></span>[`$attributes`](/docs/api/yii/validators/validator/index.md#property-attributes) | `array\|string` | Attributes to be validated by this validator. Default: `[]`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-builtInValidators"></span>[`$builtInValidators`](/docs/api/yii/validators/validator/index.md#property-builtInValidators) static | `array` | List of built-in validators (name => class or configuration) Default: `['boolean' => 'yii\validators\BooleanValidator', 'captcha' => 'yii\captcha\CaptchaValidator', 'compare' => 'yii\validators\CompareValidator', 'date' => 'yii\validators\DateValidator', 'datetime' => ['class' => 'yii\validators\DateValidator', 'type' => \yii\validators\DateValidator::TYPE_DATETIME], 'time' => ['class' => 'yii\validators\DateValidator', 'type' => \yii\validators\DateValidator::TYPE_TIME], '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\TrimValidator', 'skipOnArray' => true], 'unique' => 'yii\validators\UniqueValidator', 'url' => 'yii\validators\UrlValidator', 'ip' => 'yii\validators\IpValidator']`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| [`$checkExtensionByMimeType`](#property-checkExtensionByMimeType) | `bool` | Whether to check file type (extension) with mime-type. Default: `true`. | `FileValidator` |
| <span id="property-clientScript"></span>`$clientScript` | `array\|`[`yii\validators\client\ClientValidatorScriptInterface`](/docs/api/yii/validators/client/clientvalidatorscriptinterface/index.md)`\|null` | Client script class to use for client-side validation. Default: `null`. | `FileValidator` |
| <span id="property-enableClientValidation"></span>[`$enableClientValidation`](/docs/api/yii/validators/validator/index.md#property-enableClientValidation) | `bool` | Whether to enable client-side validation for this validator. Default: `true`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="property-except"></span>[`$except`](/docs/api/yii/validators/validator/index.md#property-except) | `array\|string` | Scenarios that the validator should not be applied to. Default: `[]`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| [`$extensions`](#property-extensions) | `array\|string\|null` | A list of file name extensions that are allowed to be uploaded. Default: `null`. | `FileValidator` |
| <span id="property-isEmpty"></span>[`$isEmpty`](/docs/api/yii/validators/validator/index.md#property-isEmpty) | `callable\|null` | A PHP callable that replaces the default implementation of [`isEmpty()`](/docs/api/yii/validators/validator/index.md#method-isEmpty). Default: `null`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| [`$maxFiles`](#property-maxFiles) | `int` | The maximum file count the given attribute can hold. Default: `1`. | `FileValidator` |
| [`$maxSize`](#property-maxSize) | `int\|null` | The maximum number of bytes required for the uploaded file. Default: `null`. | `FileValidator` |
| <span id="property-message"></span>`$message` | `string` | The error message used when a file is not uploaded correctly. Default: `null`. | `FileValidator` |
| [`$mimeTypes`](#property-mimeTypes) | `array\|string\|null` | A list of file MIME types that are allowed to be uploaded. Default: `null`. | `FileValidator` |
| [`$minFiles`](#property-minFiles) | `int` | The minimum file count the given attribute can hold. Default: `0`. Available since 2.0.14. | `FileValidator` |
| [`$minSize`](#property-minSize) | `int\|null` | The minimum number of bytes required for the uploaded file. Default: `null`. | `FileValidator` |
| <span id="property-on"></span>[`$on`](/docs/api/yii/validators/validator/index.md#property-on) | `array\|string` | Scenarios that the validator can be applied to. Default: `[]`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="property-sizeLimit"></span>`$sizeLimit` read-only | `int` | The size limit for uploaded files. | `FileValidator` |
| <span id="property-skipOnEmpty"></span>[`$skipOnEmpty`](/docs/api/yii/validators/validator/index.md#property-skipOnEmpty) | `bool` | Whether this validation rule should be skipped if the attribute value is null or an empty string. Default: `true`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="property-skipOnError"></span>[`$skipOnError`](/docs/api/yii/validators/validator/index.md#property-skipOnError) | `bool` | Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. Default: `true`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| [`$tooBig`](#property-tooBig) | `string` | The error message used when the uploaded file is too large. Default: `null`. | `FileValidator` |
| [`$tooFew`](#property-tooFew) | `string` | The error message used if the count of multiple uploads less that minFiles. Default: `null`. Available since 2.0.14. | `FileValidator` |
| [`$tooMany`](#property-tooMany) | `string` | The error message used if the count of multiple uploads exceeds limit. Default: `null`. | `FileValidator` |
| [`$tooSmall`](#property-tooSmall) | `string` | The error message used when the uploaded file is too small. Default: `null`. | `FileValidator` |
| [`$uploadRequired`](#property-uploadRequired) | `string` | The error message used when no file is uploaded. Default: `null`. | `FileValidator` |
| <span id="property-validationAttributes"></span>[`$validationAttributes`](/docs/api/yii/validators/validator/index.md#property-validationAttributes) read-only | `array\|null` | List of attribute names. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="property-when"></span>[`$when`](/docs/api/yii/validators/validator/index.md#property-when) | `callable\|null` | A PHP callable whose return value determines whether this validator should be applied. Default: `null`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="property-whenClient"></span>[`$whenClient`](/docs/api/yii/validators/validator/index.md#property-whenClient) | `string\|null` | A JavaScript function name whose return value determines whether this validator should be applied on the client-side. Default: `null`. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| [`$wrongExtension`](#property-wrongExtension) | `string` | The error message used when the uploaded file has an extension name that is not listed in [`$extensions`](/docs/api/yii/validators/filevalidator/index.md#property-extensions). Default: `null`. | `FileValidator` |
| [`$wrongMimeType`](#property-wrongMimeType) | `string` | The error message used when the file has an mime type that is not allowed by [`$mimeTypes`](/docs/api/yii/validators/filevalidator/index.md#property-mimeTypes) property. Default: `null`. | `FileValidator` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-addError"></span>[`addError()`](/docs/api/yii/validators/validator/index.md#method-addError) | Adds an error about the specified attribute to the model object. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`clientValidateAttribute()`](#method-clientValidateAttribute) | Returns the JavaScript needed for performing client-side validation. | `FileValidator` |
| <span id="method-createValidator"></span>[`createValidator()`](/docs/api/yii/validators/validator/index.md#method-createValidator) static | Creates a validator object. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getAttributeNames"></span>[`getAttributeNames()`](/docs/api/yii/validators/validator/index.md#method-getAttributeNames) | Returns cleaned attribute names without the `!` character at the beginning. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getClientOptions()`](#method-getClientOptions) | Returns the client-side validation options. | `FileValidator` |
| <span id="method-getFormattedClientMessage"></span>[`getFormattedClientMessage()`](/docs/api/yii/validators/validator/index.md#method-getFormattedClientMessage) | Returns the formatted validation message for client-side validation. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| [`getSizeLimit()`](#method-getSizeLimit) | Returns the maximum size allowed for uploaded files. | `FileValidator` |
| <span id="method-getValidationAttributes"></span>[`getValidationAttributes()`](/docs/api/yii/validators/validator/index.md#method-getValidationAttributes) | Returns a list of attributes this validator applies to. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the object. | `FileValidator` |
| <span id="method-isActive"></span>[`isActive()`](/docs/api/yii/validators/validator/index.md#method-isActive) | Returns a value indicating whether the validator is active for the given scenario and attribute. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| [`isEmpty()`](#method-isEmpty) | Checks if the given value is empty. | `FileValidator` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-validate"></span>[`validate()`](/docs/api/yii/validators/validator/index.md#method-validate) | Validates a given value. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| [`validateAttribute()`](#method-validateAttribute) | Validates a single attribute. | `FileValidator` |
| <span id="method-validateAttributes"></span>[`validateAttributes()`](/docs/api/yii/validators/validator/index.md#method-validateAttributes) | Validates the specified object. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| <span id="method-formatMessage"></span>[`formatMessage()`](/docs/api/yii/validators/validator/index.md#method-formatMessage) protected | Formats a mesage using the I18N, or simple strtr if `\Yii::$app` is not available. | [`Validator`](/docs/api/yii/validators/validator/index.md) |
| [`getMimeTypeByFile()`](#method-getMimeTypeByFile) protected | Get MIME type by file path | `FileValidator` |
| [`validateExtension()`](#method-validateExtension) protected | Checks if given uploaded file have correct type (extension) according current validator settings. | `FileValidator` |
| [`validateMimeType()`](#method-validateMimeType) protected | Checks the mimeType of the $file against the list in the [`$mimeTypes`](/docs/api/yii/validators/filevalidator/index.md#property-mimeTypes) property. | `FileValidator` |
| [`validateValue()`](#method-validateValue) protected | Validates a value. | `FileValidator` |

## Property details

### $checkExtensionByMimeType <span id="property-checkExtensionByMimeType"></span>

```php
public bool $checkExtensionByMimeType = true
```

Whether to check file type (extension) with mime-type.

If extension produced by
file mime-type check differs from uploaded file extension, the file will be considered as invalid.

### $extensions <span id="property-extensions"></span>

```php
public array|string|null $extensions = null
```

A list of file name extensions that are allowed to be uploaded.

This can be either an array or a string consisting of file extension names
separated by space or comma (e.g. "gif, jpg").
Extension names are case-insensitive. Defaults to null, meaning all file name
extensions are allowed.

### $maxFiles <span id="property-maxFiles"></span>

```php
public int $maxFiles = 1
```

The maximum file count the given attribute can hold.

Defaults to 1, meaning single file upload. By defining a higher number,
multiple uploads become possible. Setting it to `0` means there is no limit on
the number of files that can be uploaded simultaneously.

> Note: The maximum number of files allowed to be uploaded simultaneously is
also limited with PHP directive `max_file_uploads`, which defaults to 20.

### $maxSize <span id="property-maxSize"></span>

```php
public int|null $maxSize = null
```

The maximum number of bytes required for the uploaded file.

Defaults to null, meaning no limit.
Note, the size limit is also affected by `upload_max_filesize` and `post_max_size` INI setting
and the 'MAX_FILE_SIZE' hidden field value. See [`getSizeLimit()`](/docs/api/yii/validators/filevalidator/index.md#method-getSizeLimit) for details.

### $mimeTypes <span id="property-mimeTypes"></span>

```php
public array|string|null $mimeTypes = null
```

A list of file MIME types that are allowed to be uploaded.

This can be either an array or a string consisting of file MIME types
separated by space or comma (e.g. "text/plain, image/png").
The mask with the special character `*` can be used to match groups of mime types.
For example `image/*` will pass all mime types, that begin with `image/` (e.g. `image/jpeg`, `image/png`).
Mime type names are case-insensitive. Defaults to null, meaning all MIME types are allowed.

### $minFiles <span id="property-minFiles"></span>

```php
public int $minFiles = 0
```

The minimum file count the given attribute can hold.

Defaults to 0. Higher value means at least that number of files should be uploaded.

**Since**

2.0.14

### $minSize <span id="property-minSize"></span>

```php
public int|null $minSize = null
```

The minimum number of bytes required for the uploaded file.

Defaults to null, meaning no limit.

### $tooBig <span id="property-tooBig"></span>

```php
public string $tooBig = null
```

The error message used when the uploaded file is too large.

You may use the following tokens in the message:

- {attribute}: the attribute name
- {file}: the uploaded file name
- {limit}: the maximum size allowed (see [`getSizeLimit()`](/docs/api/yii/validators/filevalidator/index.md#method-getSizeLimit))
- {formattedLimit}: the maximum size formatted
  with [Formatter::asShortSize()](/docs/api/yii/i18n/formatter/index.md#method-asShortSize)

### $tooFew <span id="property-tooFew"></span>

```php
public string $tooFew = null
```

The error message used if the count of multiple uploads less that minFiles.

You may use the following tokens in the message:

- {attribute}: the attribute name
- {limit}: the value of [`$minFiles`](/docs/api/yii/validators/filevalidator/index.md#property-minFiles)

**Since**

2.0.14

### $tooMany <span id="property-tooMany"></span>

```php
public string $tooMany = null
```

The error message used if the count of multiple uploads exceeds limit.

You may use the following tokens in the message:

- {attribute}: the attribute name
- {limit}: the value of [`$maxFiles`](/docs/api/yii/validators/filevalidator/index.md#property-maxFiles)

### $tooSmall <span id="property-tooSmall"></span>

```php
public string $tooSmall = null
```

The error message used when the uploaded file is too small.

You may use the following tokens in the message:

- {attribute}: the attribute name
- {file}: the uploaded file name
- {limit}: the value of [`$minSize`](/docs/api/yii/validators/filevalidator/index.md#property-minSize)
- {formattedLimit}: the value of [`$minSize`](/docs/api/yii/validators/filevalidator/index.md#property-minSize) formatted
  with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()]

### $uploadRequired <span id="property-uploadRequired"></span>

```php
public string $uploadRequired = null
```

The error message used when no file is uploaded.

Note that this is the text of the validation error message. To make uploading files required,
you have to set [`$skipOnEmpty`](/docs/api/yii/validators/validator/index.md#property-skipOnEmpty) to `false`.

### $wrongExtension <span id="property-wrongExtension"></span>

```php
public string $wrongExtension = null
```

The error message used when the uploaded file has an extension name that is not listed in [`$extensions`](/docs/api/yii/validators/filevalidator/index.md#property-extensions).

You may use the following tokens in the message:

- {attribute}: the attribute name
- {file}: the uploaded file name
- {extensions}: the list of the allowed extensions.

### $wrongMimeType <span id="property-wrongMimeType"></span>

```php
public string $wrongMimeType = null
```

The error message used when the file has an mime type that is not allowed by [`$mimeTypes`](/docs/api/yii/validators/filevalidator/index.md#property-mimeTypes) property.

You may use the following tokens in the message:

- {attribute}: the attribute name
- {file}: the uploaded file name
- {mimeTypes}: the value of [`$mimeTypes`](/docs/api/yii/validators/filevalidator/index.md#property-mimeTypes)

## Method details

### clientValidateAttribute() <span id="method-clientValidateAttribute"></span>

```php
public function clientValidateAttribute(mixed $model, mixed $attribute, mixed $view): string|null
```

Returns the JavaScript needed for performing client-side validation.

Calls [`getClientOptions()`](/docs/api/yii/validators/filevalidator/index.md#method-getClientOptions) to generate options array for client-side validation.

You may override this method to return the JavaScript validation code if
the validator can support client-side validation.

The following JavaScript variables are predefined and can be used in the validation code:

- `attribute`: an object describing the the attribute being validated.
- `value`: the value being validated.
- `messages`: an array used to hold the validation error messages for the attribute.
- `deferred`: an array used to hold deferred objects for asynchronous validation
- `$form`: a jQuery object containing the form element

The `attribute` object contains the following properties:
- `id`: a unique ID identifying the attribute (e.g. "loginform-username") in the form
- `name`: attribute name or expression (e.g. "[0]content" for tabular input)
- `container`: the jQuery selector of the container of the input field
- `input`: the jQuery selector of the input field under the context of the form
- `error`: the jQuery selector of the error tag under the context of the container
- `status`: status of the input field, 0: empty, not entered before, 1: validated, 2: pending validation, 3: validating

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | `mixed` | The data model being validated |
| `$attribute` | `mixed` | The name of the attribute to be validated. |
| `$view` | `mixed` | The view object that is going to be used to render views or view files containing a model form with this validator applied. |

**Returns**

`string|null`: The client-side validation script. Null if the validator does not support client-side validation.

### getClientOptions() <span id="method-getClientOptions"></span>

```php
public function getClientOptions(mixed $model, mixed $attribute): array
```

Returns the client-side validation options.

This method is usually called from [`clientValidateAttribute()`](/docs/api/yii/validators/filevalidator/index.md#method-clientValidateAttribute). You may override this method to modify options
that will be passed to the client-side validation.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | `mixed` | The model being validated |
| `$attribute` | `mixed` | The attribute name being validated |

**Returns**

`array`: The client-side validation options

**Since**

2.0.11

### getSizeLimit() <span id="method-getSizeLimit"></span>

```php
public function getSizeLimit(): int
```

Returns the maximum size allowed for uploaded files.

This is determined based on four factors:

- 'upload_max_filesize' in php.ini
- 'post_max_size' in php.ini
- 'MAX_FILE_SIZE' hidden field
- [`$maxSize`](/docs/api/yii/validators/filevalidator/index.md#property-maxSize)

**Returns**

`int`: The size limit for uploaded files.

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the
given configuration.

**Returns**

`mixed`

### isEmpty() <span id="method-isEmpty"></span>

```php
public function isEmpty(mixed $value, bool $trim = false): bool
```

Checks if the given value is empty.

A value is considered empty if it is null, an empty array, or an empty string.
Note that this method is different from PHP empty(). It will return false when the value is 0.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | The value to be checked |
| `$trim` | `bool` |  |

**Returns**

`bool`: Whether the value is empty

### validateAttribute() <span id="method-validateAttribute"></span>

```php
public function validateAttribute(mixed $model, mixed $attribute): mixed
```

Validates a single attribute.

Child classes must implement this method to provide the actual validation logic.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | `mixed` | The data model to be validated |
| `$attribute` | `mixed` | The name of the attribute to be validated. |

**Returns**

`mixed`

### getMimeTypeByFile() <span id="method-getMimeTypeByFile"></span>

```php
protected function getMimeTypeByFile(string $filePath): string|null
```

Get MIME type by file path

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$filePath` | `string` |  |

**Returns**

`string|null`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md)

**Since**

2.0.26

### validateExtension() <span id="method-validateExtension"></span>

```php
protected function validateExtension(yii\web\UploadedFile $file): bool
```

Checks if given uploaded file have correct type (extension) according current validator settings.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$file` | [`yii\web\UploadedFile`](/docs/api/yii/web/uploadedfile/index.md) |  |

**Returns**

`bool`

### validateMimeType() <span id="method-validateMimeType"></span>

```php
protected function validateMimeType(yii\web\UploadedFile $file): bool
```

Checks the mimeType of the $file against the list in the [`$mimeTypes`](/docs/api/yii/validators/filevalidator/index.md#property-mimeTypes) property.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$file` | [`yii\web\UploadedFile`](/docs/api/yii/web/uploadedfile/index.md) |  |

**Returns**

`bool`: Whether the $file mimeType is allowed

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md)

**Since**

2.0.8

**See also**

- [`$mimeTypes`](/docs/api/yii/validators/filevalidator/index.md#property-mimeTypes)

### validateValue() <span id="method-validateValue"></span>

```php
protected function validateValue(mixed $value): array|null
```

Validates a value.

A validator class can implement this method to support data validation out of the context of a data model.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | The data value to be validated. |

**Returns**

`array|null`: The error message and the array of parameters to be inserted into the error message. if (!$valid) { return [$this->message, [ 'param1' => $this->param1, 'formattedLimit' => Yii::$app->formatter->asShortSize($this->getSizeLimit()), 'mimeTypes' => implode(', ', $this->mimeTypes), 'param4' => 'etc...', ]]; } return null; for this example `message` template can contain `{param1}`, `{formattedLimit}`, `{mimeTypes}`, `{param4}` Null should be returned if the data is valid.

**Throws**

- [`yii\base\NotSupportedException`](/docs/api/yii/base/notsupportedexception/index.md): if the validator does not supporting data validation without a model