-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File Validator: Field 'file' Cannot be empty #15051
Comments
@RayHughes Can you provide us with a code example? |
Here is the code, I pulled it from its classes etc. CommandHandler public function handle(User $user, array $fileData): void
{
$userAvatarUploadValidator = new UserAvatarUploadValidator();
$isInvalid = $userAvatarUploadValidator->validate($fileData);
if ($isInvalid->valid()) {
$message = $isInvalid->current();
throw new InvalidArgumentException($message->getMessage());
}
} Call $fileData = $this->request->getUploadedFiles(true, true);
handle($user, $fileData); Validator class UserAvatarUploadValidator extends Validation
{
public function initialize(): void
{
$this->add(
'avatarFile',
new PresenceOf(
[
'message' => 'Avatar must be provided',
]
)
);
$this->add(
'avatarFile',
new File(
[
'maxSize' => '5M',
'messageSize' => 'Avatar file cannot be larger than 5M',
'allowedTypes' => [
'image/jpeg',
'image/png',
'image/gif'
],
'messageType' => 'Avatar must be a png, gif, or jpg',
"maxResolution" => "600x600",
"messageMaxResolution" => "Avatar must not be bigger than 600x600 pixels",
]
)
);
}
} |
@ruudboon also encounter this issue Here is the code $uploadConfig = [
'maxSize' => '100M',
'messageSize' => ':field exceeds the max filesize (:max)',
'allowedTypes' => [
'image/jpeg',
'image/png',
'image/webp',
'audio/mpeg',
'audio/mp3',
'text/plain',
'audio/mpeg',
'application/pdf',
'audio/mpeg3',
'audio/x-mpeg-3',
'application/x-zip-compressed',
'application/octet-stream',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
],
'messageType' => 'Allowed file types are :types',
];
$validator->add(
'file',
new FileValidator($uploadConfig)
);
//validate this form for password
$validator->validate([
'file' => [
'name' => $file->getName(),
'type' => $file->getType(),
'tmp_name' => $file->getTempName(),
'error' => $file->getError(),
'size' => $file->getSize(),
]
]); |
thnx @RayHughes @kaioken thnx Will have a look at it. |
@ruudboon I believe this is due to the following
Phalcon is verifying this is a HTTP POST , this is an issue for me since I'm not, I'm just attaching the file to directly Thanks |
This validator is intended for checking php file uploads ($_FILES) so I think this behaviour is correct. |
@ruudboon yep that I get, but this behavior worked on 3.4 that why you are seeing use report it as a bug since we already had libraries expecting the result to work with $_FILES and by us passing a file ;) thanks |
Resolved in #15432 |
When using one of the File Validators, a message is set stating that the "field cannot be empty" when it is in fact set. The only work around with this is to write a call back validator or use the $_FILES global.
This feels wrong as its not compatible with an array of
Phalcon\Http|Request\File
set by$this->request->getUploadedFiles(true)
The text was updated successfully, but these errors were encountered: