ImageValidator
API reference › yii › validators › ImageValidator
ImageValidator verifies if an attribute is receiving a valid image.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$attributeNames read-only | array | Attribute names. | Validator |
$attributes | array| | Attributes to be validated by this validator. Default: []. | Validator |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$builtInValidators static | array | List of built-in validators (name => class or configuration) Default: ['boolean' => 'yii\. | Validator |
$checkExtensionByMimeType | bool | Whether to check file type (extension) with mime-type. Default: true. | FileValidator |
$clientScript | array|yii\| | Client script class to use for client-side validation. Default: null. | ImageValidator |
$enableClientValidation | bool | Whether to enable client-side validation for this validator. Default: true. | Validator |
$except | array| | Scenarios that the validator should not be applied to. Default: []. | Validator |
$extensions | array| | A list of file name extensions that are allowed to be uploaded. Default: null. | FileValidator |
$isEmpty | callable| | A PHP callable that replaces the default implementation of isEmpty(). Default: null. | Validator |
$maxFiles | int | The maximum file count the given attribute can hold. Default: 1. | FileValidator |
$maxHeight | int| | The maximum width in pixels. Default: null. | ImageValidator |
$maxSize | int| | The maximum number of bytes required for the uploaded file. Default: null. | FileValidator |
$maxWidth | int| | The maximum width in pixels. Default: null. | ImageValidator |
$message | string | The error message used when a file is not uploaded correctly. Default: null. | FileValidator |
$mimeTypes | array| | A list of file MIME types that are allowed to be uploaded. Default: null. | FileValidator |
$minFiles | int | The minimum file count the given attribute can hold. Default: 0. Available since 2.0.14. | FileValidator |
$minHeight | int| | The minimum height in pixels. Default: null. | ImageValidator |
$minSize | int| | The minimum number of bytes required for the uploaded file. Default: null. | FileValidator |
$minWidth | int| | The minimum width in pixels. Default: null. | ImageValidator |
$notImage | string | The error message used when the uploaded file is not an image. Default: null. | ImageValidator |
$on | array| | Scenarios that the validator can be applied to. Default: []. | Validator |
$overHeight | string | The error message used when the image is over $maxHeight. Default: null. | ImageValidator |
$overWidth | string | The error message used when the image is over $maxWidth. Default: null. | ImageValidator |
$sizeLimit read-only | int | The size limit for uploaded files. | FileValidator |
$skipOnEmpty | bool | Whether this validation rule should be skipped if the attribute value is null or an empty string. Default: true. | Validator |
$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 |
$tooBig | string | The error message used when the uploaded file is too large. Default: null. | FileValidator |
$tooFew | string | The error message used if the count of multiple uploads less that minFiles. Default: null. Available since 2.0.14. | FileValidator |
$tooMany | string | The error message used if the count of multiple uploads exceeds limit. Default: null. | FileValidator |
$tooSmall | string | The error message used when the uploaded file is too small. Default: null. | FileValidator |
$underHeight | string | The error message used when the image is under $minHeight. Default: null. | ImageValidator |
$underWidth | string | The error message used when the image is under $minWidth. Default: null. | ImageValidator |
$uploadRequired | string | The error message used when no file is uploaded. Default: null. | FileValidator |
$validationAttributes read-only | array| | List of attribute names. | Validator |
$when | callable| | A PHP callable whose return value determines whether this validator should be applied. Default: null. | Validator |
$whenClient | string| | A JavaScript function name whose return value determines whether this validator should be applied on the client-side. Default: null. | Validator |
$wrongExtension | string | The error message used when the uploaded file has an extension name that is not listed in $extensions. Default: null. | FileValidator |
$wrongMimeType | string | The error message used when the file has an mime type that is not allowed by $mimeTypes property. Default: null. | FileValidator |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
addError() | Adds an error about the specified attribute to the model object. | Validator |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
clientValidateAttribute() | Returns the JavaScript needed for performing client-side validation. | FileValidator |
createValidator() static | Creates a validator object. | Validator |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getAttributeNames() | Returns cleaned attribute names without the ! character at the beginning. | Validator |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getClientOptions() | Returns the client-side validation options. | FileValidator |
getFormattedClientMessage() | Returns the formatted validation message for client-side validation. | Validator |
getSizeLimit() | Returns the maximum size allowed for uploaded files. | FileValidator |
getValidationAttributes() | Returns a list of attributes this validator applies to. | Validator |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | ImageValidator |
isActive() | Returns a value indicating whether the validator is active for the given scenario and attribute. | Validator |
isEmpty() | Checks if the given value is empty. | FileValidator |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
trigger() | Triggers an event. | yii\ |
validate() | Validates a given value. | Validator |
validateAttribute() | Validates a single attribute. | FileValidator |
validateAttributes() | Validates the specified object. | Validator |
formatMessage() protected | Formats a mesage using the I18N, or simple strtr if \ is not available. | Validator |
getMimeTypeByFile() protected | Get MIME type by file path | FileValidator |
validateExtension() protected | Checks if given uploaded file have correct type (extension) according current validator settings. | FileValidator |
validateImage() protected | Validates an image file. | ImageValidator |
validateMimeType() protected | Checks the mimeType of the $file against the list in the $mimeTypes property. | FileValidator |
validateValue() protected | Validates a value. | ImageValidator |
Property details
Section titled “Property details”$maxHeight
Section titled “$maxHeight ”public int|null $maxHeight = nullThe maximum width in pixels.
Defaults to null, meaning no limit.
$maxWidth
Section titled “$maxWidth ”public int|null $maxWidth = nullThe maximum width in pixels.
Defaults to null, meaning no limit.
$minHeight
Section titled “$minHeight ”public int|null $minHeight = nullThe minimum height in pixels.
Defaults to null, meaning no limit.
$minWidth
Section titled “$minWidth ”public int|null $minWidth = nullThe minimum width in pixels.
Defaults to null, meaning no limit.
$notImage
Section titled “$notImage ”public string $notImage = nullThe error message used when the uploaded file is not an image.
You may use the following tokens in the message:
- {attribute}: the attribute name
- {file}: the uploaded file name
$overHeight
Section titled “$overHeight ”public string $overHeight = nullThe error message used when the image is over $maxHeight.
You may use the following tokens in the message:
- {attribute}: the attribute name
- {file}: the uploaded file name
- {limit}: the value of
$maxHeight
$overWidth
Section titled “$overWidth ”public string $overWidth = nullThe error message used when the image is over $maxWidth.
You may use the following tokens in the message:
- {attribute}: the attribute name
- {file}: the uploaded file name
- {limit}: the value of
$maxWidth
$underHeight
Section titled “$underHeight ”public string $underHeight = nullThe error message used when the image is under $minHeight.
You may use the following tokens in the message:
- {attribute}: the attribute name
- {file}: the uploaded file name
- {limit}: the value of
$minHeight
$underWidth
Section titled “$underWidth ”public string $underWidth = nullThe error message used when the image is under $minWidth.
You may use the following tokens in the message:
- {attribute}: the attribute name
- {file}: the uploaded file name
- {limit}: the value of
$minWidth
Method details
Section titled “Method details”init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
Returns
mixed
validateImage()
Section titled “validateImage() ”protected function validateImage(yii\web\UploadedFile $image): array|nullValidates an image file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$image | yii\ | Uploaded file passed to check against a set of rules |
Returns
array|null: The error message and the parameters to be inserted into the error message. Null should be returned if the data is valid.
validateValue()
Section titled “validateValue() ”protected function validateValue(mixed $value): array|nullValidates 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: if the validator does not supporting data validation without a model