ZF2上具有集合的FilePrg

阿莱西奥·德·佐蒂(Alessio De Zotti)

我需要使用fileprg插件提交包含一些字段以及最多3张图像的表格,以保存上载的文件,以防出现一些验证错误。

我不想切换到其他解决方案(例如将表单拆分为子表单,或删除集合以支持三个不同的文件字段)。我遇到的主要问题是,当用户提交无效文件(太大或mimetype无效)时,我想报告该错误并允许上传其他文件。相反,发生的事情是该集合从表单中消失了。

如果用户根本不提交文件(即使需要文件),也会发生相同的情况。在使用自己的代码进行测试之后,我决定对cgmartin代码进行测试,无论如何都会发生这种情况。

我知道这可能像该问题的重复部分一样被禁止,但是我在这一问题上停留了几天。

这是我的cgmartin代码的“分支”(实际上,我在控制器中添加了fileprg,视图和表单都保持不变)。谢谢。一种。

public function collectionAction()
{
    $form = new Form\CollectionUpload('file-form');
    $prg = $this->fileprg($form);


    if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
        $this->getServiceLocator()->get('Zend/Log')->debug("Redirect to Get!");
        return $prg; // Return PRG redirect response
    } elseif (is_array($prg)) {


            if ($form->isValid()) {
                //
                // ...Save the form...
                //
                return $this->redirectToSuccessPage($form->getData());
            }

    }

    return array('form' => $form);
}

风景

<div>
 <a href="<?php echo $this->url('fileupload')?>">&laquo; Back to Examples Listing</a>
</div>

<h2><?php echo ($this->title) ?: 'File Upload Examples' ?></h2>

<?php
// Init Form
$form = $this->form;
$form->setAttribute('class', 'form-horizontal');
$form->prepare();

// Configure Errors Helper
$errorsHelper  = $this->plugin('formelementerrors');
$errorsHelper
    ->setMessageOpenFormat('<div class="help-block">')
    ->setMessageSeparatorString('</div><div class="help-block">')
    ->setMessageCloseString('</div>');
?>
<?php echo $this->form()->openTag($form); ?>
<fieldset>
    <legend><?php echo ($this->legend) ?: 'Multi-File Upload with Collections' ?></legend>

    <?php
    $elem = $form->get('text');
    $elem->setLabelAttributes(array('class' => 'control-label'));
    $errors = $elem->getMessages();
    $errorClass = (!empty($errors)) ? ' error' : '';
    ?>
    <div class="control-group<?php echo $errorClass ?>">
        <?php echo $this->formLabel($elem); ?>
        <div class="controls">
            <?php echo $this->formText($elem); ?>
            <?php echo $errorsHelper($elem); ?>
        </div>
    </div>

    <?php
    $collection = $form->get('file-collection');
    foreach ($collection as $elem) {
        $elem->setLabelAttributes(array('class' => 'control-label'));
        $errors = $elem->getMessages();
        $errorClass = (!empty($errors)) ? ' error' : '';
        ?>
        <div class="control-group<?php echo $errorClass ?>">
            <?php echo $this->formLabel($elem); ?>
            <div class="controls">
                <?php echo $this->formFile($elem); ?>
                <?php echo $errorsHelper($elem); ?>
            </div>
        </div>
        <?php
    }
    ?>
    <?php if (!empty($this->tempFiles)) { ?>
    <div class="control-group"><div class="controls">
        <div class="help-block">
            Uploaded: <ul>
            <?php foreach ($this->tempFiles as $tempFile) { ?>
            <li><?php echo $this->escapeHtml($tempFile['name']) ?></li>
            <?php } ?>
            </ul>
        </div>
    </div></div>
    <?php } ?>

    <div class="control-group">
        <div class="controls">
            <button class="btn btn-primary">Submit</button>
        </div>
    </div>

</fieldset>
<?php echo $this->form()->closeTag($form); ?>

表格:

<?php

namespace ZF2FileUploadExamples\Form;

use Zend\InputFilter;
use Zend\Form\Form;
use Zend\Form\Element;

class CollectionUpload extends Form
{
    public $numFileElements = 2;

    public function __construct($name = null, $options = array())
    {
        parent::__construct($name, $options);
        $this->addElements();
        $this->setInputFilter($this->createInputFilter());
    }

    public function addElements()
    {
        // File Input
        $file = new Element\File('file');
        $file->setLabel('Multi File');

        $fileCollection = new Element\Collection('file-collection');
        $fileCollection->setOptions(array(
             'count'          => $this->numFileElements,
             'allow_add'      => false,
             'allow_remove'   => false,
             'target_element' => $file,
        ));
        $this->add($fileCollection);

        // Text Input
        $text = new Element\Text('text');
        $text->setLabel('Text Entry');
        $this->add($text);
    }

    public function createInputFilter()
    {
        $inputFilter = new InputFilter\InputFilter();

        // File Collection
        $fileCollection = new InputFilter\InputFilter();
        for ($i = 0; $i < $this->numFileElements; $i++) {
            $file = new InputFilter\FileInput($i);
            $file->setRequired(true);
            $file->getFilterChain()->attachByName(
                'filerenameupload',
                array(
                    'target'          => './data/tmpuploads/',
                    'overwrite'       => true,
                    'use_upload_name' => true,
                )
            );
            $fileCollection->add($file);
        }
        $inputFilter->add($fileCollection, 'file-collection');

        // Text Input
        $text = new InputFilter\Input('text');
        $text->setRequired(true);
        $inputFilter->add($text);

        return $inputFilter;
    }
}
钱德勒躁狂症

我看不到将数据设置到表单的位置,这可能就是为什么在呈现表单错误时不对其进行维护的原因。

您可能应该$form->setData($prg);在验证表单之前致电

我的基本逻辑通常是这样的:

if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
    return $prg; // Return PRG redirect response
} elseif ($prg !== false) {
    // Set data to form
    $form->setData($prg);

    // Validate form
    if ($form->isValid()) {
         // ...Save the form...
    } else {
        // Do something else
    }
} else {
      // first time Page is called
}

// Always show form
return new ViewModel(array('form' => $form));

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何自定义呈现具有匹配字段集的ZF2集合

来自分类Dev

ZF2 FileUpload集合

来自分类Dev

ZF2 AbstractTableGateway 上具有多个结果集的条件

来自分类Dev

ZF2中的别名选择-具有计算字段

来自分类Dev

ZF2、Doctrine2、Gedmo - 具有关联的 SoftDelete JTI 实体

来自分类Dev

如何验证输入是否具有zf2格式的浮点值?

来自分类Dev

在ZF2中创建具有依赖项的理论存储库(依赖项注入)

来自分类Dev

具有子路由的子域的ZF2路由器配置

来自分类Dev

ZF2翻译

来自分类Dev

具有单个入口点的zf2网站,URL中没有路由/路径

来自分类Dev

具有MS Server分页和命令的Doctrine2,不能在ZF2下工作

来自分类Dev

zf2段路线在最后位置上的捕获ID

来自分类Dev

ZF2 +组合键上的重复表格验证

来自分类Dev

使用ZF2更改Doctrine上的DefaultEntityListenerResolver

来自分类Dev

为什么要在ZF2上使用服务?

来自分类Dev

在zf2上使用会话(恢复容器)

来自分类Dev

ZF2 +组合键上的重复表格验证

来自分类Dev

使用ZF2更改Doctrine上的DefaultEntityListenerResolver

来自分类Dev

具有关系数据库名称的ZF2 PDO_IBM驱动程序

来自分类Dev

在zf2中获取所有模块名称

来自分类Dev

在zf2中获取所有模块名称

来自分类Dev

带有Symfony的ZF2学说<或null查询

来自分类Dev

ZF2 + Doctrine2 - Fieldset 中集合的 Fieldset 中的 Fieldset 无法正确验证

来自分类Dev

使用字段集或集合时的ZF2验证器上下文

来自分类Dev

ZF2联合+分页

来自分类Dev

What are Hydrators in ZF2?

来自分类Dev

ZF2动态路由

来自分类Dev

zf2“ join as”语法

来自分类Dev

ZF2事件触发