diff --git a/autoload_classmap.php b/autoload_classmap.php index 8e357fc..3f7ecf3 100644 --- a/autoload_classmap.php +++ b/autoload_classmap.php @@ -6,6 +6,7 @@ 'ZF2FileUploadExamples\Controller\PrgExamples' => __DIR__ . '/src/ZF2FileUploadExamples/Controller/PrgExamples.php', 'ZF2FileUploadExamples\Controller\ProgressExamples' => __DIR__ . '/src/ZF2FileUploadExamples/Controller/ProgressExamples.php', 'ZF2FileUploadExamples\Form\CollectionUpload' => __DIR__ . '/src/ZF2FileUploadExamples/Form/CollectionUpload.php', + 'ZF2FileUploadExamples\Form\FieldsetUpload' => __DIR__ . '/src/ZF2FileUploadExamples/Form/FieldsetUpload.php', 'ZF2FileUploadExamples\Form\MultiHtml5Upload' => __DIR__ . '/src/ZF2FileUploadExamples/Form/MultiHtml5Upload.php', 'ZF2FileUploadExamples\Form\ProgressUpload' => __DIR__ . '/src/ZF2FileUploadExamples/Form/ProgressUpload.php', 'ZF2FileUploadExamples\Form\SingleUpload' => __DIR__ . '/src/ZF2FileUploadExamples/Form/SingleUpload.php', diff --git a/config/module.config.php b/config/module.config.php index a2b127d..66f2180 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -94,6 +94,16 @@ ), ), ), + 'fieldset' => array( + 'type' => 'Literal', + 'options' => array( + 'route' => '/fieldset', + 'defaults' => array( + 'controller' => 'fileupload_prgexamples', + 'action' => 'fieldset', + ), + ), + ), ), ), diff --git a/src/ZF2FileUploadExamples/Controller/PrgExamples.php b/src/ZF2FileUploadExamples/Controller/PrgExamples.php index 72dafd7..6223c16 100644 --- a/src/ZF2FileUploadExamples/Controller/PrgExamples.php +++ b/src/ZF2FileUploadExamples/Controller/PrgExamples.php @@ -50,4 +50,49 @@ public function multiHtml5Action() $view->setTemplate('zf2-file-upload-examples/examples/single'); return $view; } + + /** + * Example of a single File element within a nested Fieldset + * w/ the Post-Redirect-Get plugin. + * + * @return array|ViewModel + */ + public function fieldsetAction() + { + $tempFiles = array(); + + $form = new Form\FieldsetUpload('file-form'); + $prg = $this->fileprg($form); + if ($prg instanceof \Zend\Http\PhpEnvironment\Response) { + return $prg; // Return PRG redirect response + } elseif (is_array($prg)) { + if ($form->isValid()) { + + // + // ...Save the form... + // + + return $this->redirectToSuccessPage($form->getData()); + } else { + // Form not valid, but file uploads might be valid + $file1Errors = $form->get('fieldset')->get('file')->getMessages(); + if (empty($file1Errors)) { + $tempFiles['file'] = $form->get('fieldset')->get('file')->getValue(); + } + $file2Errors = $form->get('file2')->getMessages(); + if (empty($file2Errors)) { + $tempFiles['file2'] = $form->get('file2')->getValue(); + } + } + } + + $view = new ViewModel(array( + 'title' => 'Post/Redirect/Get Plugin Example', + 'legend' => 'Nested Fieldset Elements', + 'form' => $form, + 'tempFiles' => $tempFiles, + )); + $view->setTemplate('zf2-file-upload-examples/examples/fieldset'); + return $view; + } } diff --git a/src/ZF2FileUploadExamples/Form/FieldsetUpload.php b/src/ZF2FileUploadExamples/Form/FieldsetUpload.php new file mode 100644 index 0000000..cfa65b2 --- /dev/null +++ b/src/ZF2FileUploadExamples/Form/FieldsetUpload.php @@ -0,0 +1,87 @@ +addElements(); + $this->setInputFilter($this->createInputFilter()); + } + + public function addElements() + { + $fieldset = new Fieldset('fieldset'); + + // File Input + $file = new Element\File('file'); + $file + ->setLabel('Multi-File Input 1') + ->setAttributes(array('multiple' => true)); + $fieldset->add($file); + + // Text Input + $text = new Element\Text('text'); + $text->setLabel('Text Entry'); + $fieldset->add($text); + + $this->add($fieldset); + + // File Input 2 + $file2 = new Element\File('file2'); + $file2 + ->setLabel('Multi-File Input 2') + ->setAttributes(array('multiple' => true)); + $this->add($file2); + } + + public function createInputFilter() + { + $inputFilter = new InputFilter\InputFilter(); + $fieldsetFilter = new InputFilter\InputFilter(); + + // File Input + $file = new InputFilter\FileInput('file'); + $file->setRequired(true); + $file->getFilterChain()->attachByName( + 'filerenameupload', + array( + 'target' => './data/tmpuploads/', + 'overwrite' => true, + 'use_upload_name' => true, + ) + ); + //$file->getValidatorChain()->addByName( + // 'fileextension', array('extension' => 'txt') + //); + $fieldsetFilter->add($file); + + // Text Input + $text = new InputFilter\Input('text'); + $text->setRequired(true); + $fieldsetFilter->add($text); + + $inputFilter->add($fieldsetFilter, 'fieldset'); + + // File Input 2 + $file = new InputFilter\FileInput('file2'); + $file->setRequired(false); // Make this one optional + $file->getFilterChain()->attachByName( + 'filerenameupload', + array( + 'target' => './data/tmpuploads/', + 'overwrite' => true, + 'use_upload_name' => true, + ) + ); + + return $inputFilter; + } +} \ No newline at end of file diff --git a/view/zf2-file-upload-examples/examples/fieldset.phtml b/view/zf2-file-upload-examples/examples/fieldset.phtml new file mode 100644 index 0000000..a474a16 --- /dev/null +++ b/view/zf2-file-upload-examples/examples/fieldset.phtml @@ -0,0 +1,102 @@ +
+ « Back to Examples Listing +
+ +

title) ?: 'File Upload Examples' ?>

+ +form; +$form->setAttribute('class', 'form-horizontal'); +$form->prepare(); + +// Configure Errors Helper +$errorsHelper = $this->plugin('formelementerrors'); +$errorsHelper + ->setMessageOpenFormat('
') + ->setMessageSeparatorString('
') + ->setMessageCloseString('
'); +?> +form()->openTag($form); ?> +
+ legend) ?: 'Single File Upload' ?> + + get('fieldset')->get('text'); + $elem->setLabelAttributes(array('class' => 'control-label')); + $errors = $elem->getMessages(); + $errorClass = (!empty($errors)) ? ' error' : ''; + ?> +
+ formLabel($elem); ?> +
+ formText($elem); ?> + +
+
+ + get('fieldset')->get('file'); + $elem->setLabelAttributes(array('class' => 'control-label')); + $errors = $elem->getMessages(); + $errorClass = (!empty($errors)) ? ' error' : ''; + ?> +
+ formLabel($elem); ?> +
+ formFile($elem); ?> + + tempFiles['file'])) { ?> + +
+ Uploaded:
    + tempFiles['file'] as $tempFile) { ?> +
  • escapeHtml($tempFile['name']) ?>
  • + +
+
+ +
+
+
+ +
+ Not-nested Elements + get('file2'); + $elem->setLabelAttributes(array('class' => 'control-label')); + $errors = $elem->getMessages(); + $errorClass = (!empty($errors)) ? ' error' : ''; + ?> +
+ formLabel($elem); ?> +
+ formFile($elem); ?> + + tempFiles['file2'])) { ?> + +
+ Uploaded:
    + tempFiles['file2'] as $tempFile) { ?> +
  • escapeHtml($tempFile['name']) ?>
  • + +
+
+ +
+
+ +
+
+ +
+
+ +
+form()->closeTag($form); ?> diff --git a/view/zf2-file-upload-examples/examples/index.phtml b/view/zf2-file-upload-examples/examples/index.phtml index 82449a7..f529108 100644 --- a/view/zf2-file-upload-examples/examples/index.phtml +++ b/view/zf2-file-upload-examples/examples/index.phtml @@ -5,6 +5,7 @@
  • Multiple File Upload with Collections
  • Single File Upload with partial validation
  • Post-Redirect-Get Plugin Example
  • +
  • FilePRG Plugin Example with nested Fieldset
  • AJAX File Upload with Session Progress
  • Partial AJAX File Upload with Session Progress