ZF2 fileprg与集合中的文件 [英] ZF2 fileprg with files in collection

查看:167
本文介绍了ZF2 fileprg与集合中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 fileprg 插件来处理集合中的文件。
我试图使用 FormCollections ,但在 $ form-> getData()

我用简单的文件输入来测试form和fileprg, (以相同的形式),它的工作已上传/重命名,它在 $ form-> getData()中。

我错过了什么? 在文件中有没有什么特别的要做的集合,以使其工作? \Zend\Mvc\Controller\Plugin\FilePostRedirectGet 您要查看的两个函数是 getNonEmptyUploadData (即提供回调函数)和 traverseInputs (这是一个荣耀 foreach ,它检查每个输入过滤器然后运行它并通过上面的回调)。

为了让这个插件在集合上工作,你将需要扩展这个类并修改上面的函数:

/ **
*遍历InputFilter并对每个Input和相关的值运行一个回调
*
* @param InputFilterInterface $ inputFilter
* @param array $ values
* @param callable $ callback
* @return array | null
* /
protected function对于traverseInputs(InputFilterInterface $ inputFilter,$ values,$ callback)
{
$ returnValues = null;
foreach($ values as $ name => $ value){
if(!$ inputFilter-> has($ name)){
continue;
}

$ input = $ inputFilter-> get($ name);
if($ input instanceof CollectionInputFilter){
$ retVal = null; $ if $($ value)$ {

}
foreach($ value as $ k => $ val){
$ retVal2 = $ this-> traverseInputs($ input-> getInputFilter(),$ val,$ callback);
if($ retVal2)
$ retVal [$ k] = $ retVal2;

} else
$ retVal = $ this-> traverseInputs($ input,$ value,$ callback);

if(null!== $ retVal){
$ returnValues [$ name] = $ retVal;
}
继续;
}

$ retVal = $ callback($ input,$ value);
if(null!== $ retVal){
$ returnValues [$ name] = $ retVal;
}
}
return $ returnValues;

$ b $ **
*遍历InputFilter并只返回FileInputs的数据,这些FileInputs有一个上传
*
* @param InputFilterInterface $ inputFilter
* @param array $ data
* @return array
* /
protected function getNonEmptyUploadData(InputFilterInterface $ inputFilter,$ data)
{
return $ this-> traverseInputs(
$ inputFilter,
$ data,
function($ input,$ value){
$ messages = $ input-> getMessages();
if(is_array($ value)&& $ inputof FileInput&&& empty($ messages)){
$ rawValue = $ value;
if(
( isset($ rawValue ['error'])&& $ rawValue ['error']!== UPLOAD_ERR_NO_FILE)
||(isset($ rawValue [0] ['error'])&& $ rawValue [0] ['error']!== UPLOAD_ERR_NO_FILE)
){
return $ valu e;
}
}
return;
}
);



$ b $ p
$ b

下面是一个diff代码,显示了哪些行被改变了: https://github.com/rafam31/zf2/commit/c481e7404faf93beb4c67a6a4b7490fec15d279b


I can't get fileprg plugin to work with the files in a collection. I am trying to upload multiple files using FormCollections, but in $form->getData() there is no key related to my collection or the files .

I tested the form and fileprg with simple file input (in the same form) and it worked uploaded/renamed and it was in the $form->getData().

what am i missing ? is there anything special to be done with the collections to get it to work ?

解决方案

In the file \Zend\Mvc\Controller\Plugin\FilePostRedirectGet the two functions you want to look at are getNonEmptyUploadData (that is supplying a callback function) and traverseInputs (which is a glorified foreach that checks each input filter then runs it and the value through the above callback).

To allow this plugin to work on collections you will need to extend the class and alter the above functions:

/**
 * Traverse the InputFilter and run a callback against each Input and associated value
 *
 * @param  InputFilterInterface $inputFilter
 * @param  array                $values
 * @param  callable             $callback
 * @return array|null
 */
protected function traverseInputs(InputFilterInterface $inputFilter, $values, $callback)
{
    $returnValues = null;
    foreach ($values as $name => $value) {
        if (!$inputFilter->has($name)) {
            continue;
        }

        $input = $inputFilter->get($name);
         if ($input instanceof InputFilterInterface && is_array($value)) {

            if ($input instanceof CollectionInputFilter) {
                $retVal = null;
                foreach ($value as $k => $val) {
                    $retVal2 = $this->traverseInputs($input->getInputFilter(), $val, $callback);
                    if ($retVal2)
                        $retVal[$k] = $retVal2;
                }
            } else
                $retVal = $this->traverseInputs($input, $value, $callback);

            if (null !== $retVal) {
                $returnValues[$name] = $retVal;
            }
            continue;
        }

        $retVal = $callback($input, $value);
        if (null !== $retVal) {
            $returnValues[$name] = $retVal;
        }
    }
    return $returnValues;
}

/**
 * Traverse the InputFilter and only return the data of FileInputs that have an upload
 *
 * @param  InputFilterInterface $inputFilter
 * @param  array                $data
 * @return array
 */
protected function getNonEmptyUploadData(InputFilterInterface $inputFilter, $data)
{
    return $this->traverseInputs(
        $inputFilter,
        $data,
        function ($input, $value) {
            $messages = $input->getMessages();
            if (is_array($value) && $input instanceof FileInput && empty($messages)) {
                $rawValue = $value;
                if (
                    (isset($rawValue['error']) && $rawValue['error'] !== UPLOAD_ERR_NO_FILE)
                    || (isset($rawValue[0]['error']) && $rawValue[0]['error'] !== UPLOAD_ERR_NO_FILE)
                ) {
                    return $value;
                }
            }
            return;
        }
    );
}

Here is a diff that shows what lines were altered: https://github.com/rafam31/zf2/commit/c481e7404faf93beb4c67a6a4b7490fec15d279b

这篇关于ZF2 fileprg与集合中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆