没有文件上传错误Zend Framework 3验证

问题描述

我想使用Callback验证,但continue_if_emptyFile不起作用,它返回No file was uploaded错误。但是所有适用于InputSelect的东西。我需要帮助。

$this->add([
    'name' => 'images','required' => false,'allow_empty' => true,'continue_if_empty' => true,'type' => FileInput::class,'filters' => [
        [
            'name' => Rename::class,'options' => [
                'target' => '/var/www/project/public/website/content/tmp/image_' . md5(time()),'randomize' => true,],'validators' => [
        [
            'name' => Callback::class,'options' => [
                'callback' => [$this,'imageValidator'],]);


public function imageValidator($value,$context)
{
     dd($context);
}

解决方法

这帮助我解决了“未上传文件”错误。

在打开表单标签之前,将此行包括在视图模板(.phtml)中:

$ form-> prepare()

完整示例:

<!-- File: upload-form.phtml -->
<?php
    $form->prepare(); // The correct enctype is set here
    $fileElement = $form->get('image-file');
    echo $this->form()->openTag($form);
?>

    <div class="form-element">
        <?= $this->formLabel($fileElement) ?>
        <?= $this->formFile($fileElement) ?>
        <?= $this->formElementErrors($fileElement) ?>
    </div>

    <button>Submit</button>

<?= $this->form()->closeTag() ?>

来源:https://docs.laminas.dev/laminas-form/file-upload/

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...