ZF2验证器文件\ Zend \ File \ Transfer \ Adapter \ Http

时间:2013-12-03 16:39:07

标签: zend-framework2

考虑到某个操作,我有HTML代码2输入文件。

<form action="#" id="appForm" name="appForm"
        enctype="multipart/form-data" method="post">
<input type="file" name="picture" id="picture">
<input type="file" name="attach_file" id="attach_file">
</form>

之后,我将此表单提交给控制者的动作。行动中的代码

if($this->getRequest()->isPost()) {
                    $item = array_merge_recursive($this->params()->fromPost(), $this->params()->fromFiles());
        $item['id'] = $this->params()->fromRoute('id');
                    $item['action'] = 'edit';
        $validator = new ArticleValidator($item);
        if($validator->isError()) {
            $error = array_merge($error, $validator->getError());
            $item = $validator->getData();
        }else{
            echo ' save data';
            die();
        }
    }

ArticleValidator中的代码

    namespace Admin\Form;

class ArticleValidator {

protected $_arrError = null;
protected $_arrData;

public function __construct($arrParam = array(), $options = null) {

    // avatar
    if (!empty($arrParam['picture']['name'])) {
        $size = new \Zend\Validator\File\Size(array('min' => 1, 'max' => 2097152));
        $ext = new \Zend\Validator\File\Extension('gif,jpg,jpeg,png');
        $adapterPicture = new \Zend\File\Transfer\Adapter\Http();
        $adapterPicture->setValidators(array($size, $ext), $arrParam['picture']['name']);
        if (!$adapterPicture->isValid()) {
            $message = $adapterPicture->getMessages();
            $this->_arrError['picture'] = current($message);
            $arrParam['picture'] = '';
        }
    }

    // attach_file
    if (!empty($arrParam['attach_file']['name'])) {
        $size = new \Zend\Validator\File\Size(array('min' => 1, 'max' => 5242880));
        $ext = new \Zend\Validator\File\Extension('rar,zip,gz,docx,pdf,xlsx');
        $adapterFile = new \Zend\File\Transfer\Adapter\Http();
        $adapterFile->setValidators(array($size, $ext), $arrParam['attach_file']['name']);
        if (!$adapterFile->isValid()) {
            $message = $adapterFile->getMessages();
            $this->_arrError['attach_file'] = current($message);
            $arrParam['attach_file'] = '';
        }
    }
}

}

当我将有效图像发布到“图片”字段时。 $ this-&gt; _arrError将“上传”文件''。我检查并知道,$ adapterPicture也检查'attach_file'字段。因为我没有将文件发布到'attach_file'中,所以它说没有上传(对于attach_file)。 那么,我如何只检查'picture'字段然后只检查'attach_file'。 PS:我没有使用Zend \ Form。只是一个包含2输入类型=文件的HTML代码。然后,检查文件使用\ Zend \ File \ Transfer \ Adapter \ Http

0 个答案:

没有答案