Skip to content

BaseFileValidator

BaseFileValidator is the base validator when validating files.

It provides checking of the size and extension of a file for implementing validators. Parameters: ‘min_size’ The minimum file size in byte, default 1 ‘max_size’ The maximum file size in byte ‘extension’ list of valid extensions (delimited by ’ ’) ‘mime_type’ A regular expression checked against the MIME type of the file as returned by the fileinfo extension. The mime type string to match against is something like “application/pdf”. ‘mime_type_include_charset’ Whether the regex in parameter ‘mime_type’ should be matched against a string containing the charset info (as defined in RFC 2045), e.g. “text/csv; charset=iso-8859-1”. Errors: ‘upload_failed’ The upload of the file failed ‘min_size’ ‘max_size’ ‘extension’ The file doesn’t have the required extension ‘mime_type’ The MIME type check failed

abstract class BaseFileValidator extends Validator

ExtendsValidator
Since1.0.0
SourceValidator/BaseFileValidator.php
MethodDescription
getAcceptedParameters(): array<int, string>Returns the base Validator parameters plus ‘min_size’, ‘max_size’, ‘extension’, ‘mime_type’ and ‘mime_type_include_charset’, shared by every file validator.
[`initialize(Context $context, array<string, mixed> $parameters = [], array<intstring, mixed> $arguments = [], array<string, string> $errors = []): void`](#initialize)
validate(): boolValidates the input

public static function getAcceptedParameters(): array<int, string>

Returns the base Validator parameters plus ‘min_size’, ‘max_size’, ‘extension’, ‘mime_type’ and ‘mime_type_include_charset’, shared by every file validator.

‘min_size’ and ‘max_size’ bound the uploaded file’s size in bytes, the minimum defaulting to 1 so a zero-byte upload fails. ‘extension’ is a list of acceptable filename extensions, given either as an array or as a space-delimited string, matched case-insensitively against the client filename. ‘mime_type’ is a PCRE matched against the type fileinfo detects from the file’s own content, and requires the fileinfo extension to be loaded; ‘mime_type_include_charset’ makes that match run against the type with the charset appended (“text/csv; charset=iso-8859-1”) rather than the bare type. Subclasses merge their own names onto this set.

Returns array``<``int``, ``string``> — The accepted parameter names.

public function initialize(Context $context, array<string, mixed> $parameters = [], array<int|string, mixed> $arguments = [], array<string, string> $errors = []): void

Initialize this validator.

An array of error messages.

ParameterTypeDescription
$contextContextThe Context.
$parametersarray``<``string``, ``mixed``>An array of validator parameters.
$arguments`array<int“string, mixed>`
$errorsarray``<``string``, ``string``>An array of error messages.

protected function validate(): bool

Validates the input

Returns bool — The file is valid according to given parameters.

These come from an ancestor and are documented where they are declared.

MethodDeclared inDescription
appendParameter()ParameterHolderAppend a parameter.
appendParameterByRef()ParameterHolderAppend a parameter by reference.
clearParameters()ParameterHolderClear all parameters associated with this request.
execute()ValidatorExecutes the validator.
getArguments()ValidatorReturns all arguments which should be validated.
getBase()ValidatorReturns the base path of this validator.
getBaseKeys()ValidatorReturns the “keys” in the path of the base
getContext()ValidatorRetrieve the current application context.
getDependencyManager()ValidatorReturns the depency manager of the parent container if any.
getFlatParameterNames()ParameterHolderRetrieve an array of flattened parameter names.
getLastKey()ValidatorReturns the last “keys” in the path of the base
getMutatedRequest()ValidatorThe WebRequest this validator ended execute() with.
getName()ValidatorReturns the name of this validator.
getParameter()ParameterHolderRetrieve a parameter.
getParameterNames()ParameterHolderRetrieve an array of parameter names.
getParameters()ParameterHolderRetrieve an array of parameters.
getParentContainer()ValidatorRetrieve the parent container.
hasParameter()ParameterHolderIndicates whether or not a parameter exists.
mapErrorCode()ValidatorConverts string severity codes into integer values (see severity constants) critical -> Validator::CRITICAL error -> Validator::ERROR notice -> Validator::NOTICE none -> Validator::NONE success -> not allowed to be specified by the user.
removeParameter()ParameterHolderRemove a parameter.
reset()ValidatorReturns the validator to its uninitialized state for reuse across requests.
setErrorMessage()ValidatorSets an error message override for the given index (the empty string is the default/generic message).
setParameter()ParameterHolderSet a parameter.
setParameterByRef()ParameterHolderSet a parameter by reference.
setParameters()ParameterHolderSet an array of parameters.
setParametersByRef()ParameterHolderSet an array of parameters by reference.
setParentContainer()ValidatorSets the parent container.
shutdown()ValidatorShuts the validator down.