일부 유효성 검사 오류가있는 경우 업로드 된 파일을 저장하기 위해 fileprg 플러그인을 사용하여 일부 필드가 포함 된 양식을 최대 3 개 이미지와 함께 제출해야합니다.ZF2에 컬렉션이있는 FilePrg
다른 솔루션 (예 : 하위 양식에서 양식 분할 또는 세 가지 다른 파일 필드를 사용하여 컬렉션 제거)으로 전환하지 않으려합니다. 내가 경험하는 주된 문제는 사용자가 잘못된 파일 (크기가 너무 크거나 유효하지 않은 MIME 형식)을 제출할 때 오류를보고하고 다른 파일을 업로드 할 수있게하려는 것입니다. 그 대신 컬렉션이 양식에서 사라지는 것입니다.
파일이 필요한 경우에도 사용자가 파일을 전혀 제출하지 않으면 동일한 현상이 발생합니다. 내 자신의 코드로 테스트 한 후에 cgmartin 코드를 테스트하기로 결정했는데, 이는 어쨌든 발생합니다.
나는 이것이 this question의 복제처럼 금지 될 수 있다는 것을 알고 있지만,이 문제로 며칠이 걸린다.
이것은 cgmartin 코드의 "포크 (fork)"입니다. 실제로 컨트롤러에서 fileprg를 추가했는데, 뷰와 폼은 변경되지 않았습니다. 감사합니다. . A.
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')?>">« 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;
}
}
안녕 chandlermania, 감사를 지원합니다. 나는 당신이 제안한대로 tryed하지만 불행히도 아무것도 변경되지 않았다. 나에게 이상하게 보인 것은 fileprg없이 예상대로 실제로 작동한다는 것이다. –