Skip to content

Commit

Permalink
New nested fieldset example with FilePRG
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmartin committed Jan 25, 2013
1 parent 38c76a5 commit 1f0691f
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 0 deletions.
1 change: 1 addition & 0 deletions autoload_classmap.php
Expand Up @@ -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',
Expand Down
10 changes: 10 additions & 0 deletions config/module.config.php
Expand Up @@ -94,6 +94,16 @@
),
),
),
'fieldset' => array(
'type' => 'Literal',
'options' => array(
'route' => '/fieldset',
'defaults' => array(
'controller' => 'fileupload_prgexamples',
'action' => 'fieldset',
),
),
),
),
),

Expand Down
45 changes: 45 additions & 0 deletions src/ZF2FileUploadExamples/Controller/PrgExamples.php
Expand Up @@ -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;
}
}
87 changes: 87 additions & 0 deletions src/ZF2FileUploadExamples/Form/FieldsetUpload.php
@@ -0,0 +1,87 @@
<?php

namespace ZF2FileUploadExamples\Form;

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

class FieldsetUpload extends Form
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
$this->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;
}
}
102 changes: 102 additions & 0 deletions view/zf2-file-upload-examples/examples/fieldset.phtml
@@ -0,0 +1,102 @@
<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) ?: 'Single File Upload' ?></legend>

<?php
$elem = $form->get('fieldset')->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
$elem = $form->get('fieldset')->get('file');
$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); ?>
<?php if (!empty($this->tempFiles['file'])) { ?>
<!--
Note: You might not want to render the file input in this
case either, depending on your use-case.
-->
<div class="help-block">
Uploaded: <ul>
<?php foreach ($this->tempFiles['file'] as $tempFile) { ?>
<li><?php echo $this->escapeHtml($tempFile['name']) ?></li>
<?php } ?>
</ul>
</div>
<?php } ?>
</div>
</div>
</fieldset>

<fieldset>
<legend>Not-nested Elements</legend>
<?php
$elem = $form->get('file2');
$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); ?>
<?php if (!empty($this->tempFiles['file2'])) { ?>
<!--
Note: You might not want to render the file input in this
case either, depending on your use-case.
-->
<div class="help-block">
Uploaded: <ul>
<?php foreach ($this->tempFiles['file2'] as $tempFile) { ?>
<li><?php echo $this->escapeHtml($tempFile['name']) ?></li>
<?php } ?>
</ul>
</div>
<?php } ?>
</div>
</div>

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

</fieldset>
<?php echo $this->form()->closeTag($form); ?>
1 change: 1 addition & 0 deletions view/zf2-file-upload-examples/examples/index.phtml
Expand Up @@ -5,6 +5,7 @@
<li><a href="<?php echo $this->url('fileupload/collection')?>">Multiple File Upload with Collections</a></li>
<li><a href="<?php echo $this->url('fileupload/partial')?>">Single File Upload with partial validation</a></li>
<li><a href="<?php echo $this->url('fileupload/prg/multi-html5')?>">Post-Redirect-Get Plugin Example</a></li>
<li><a href="<?php echo $this->url('fileupload/prg/fieldset')?>">FilePRG Plugin Example with nested Fieldset</a></li>
<li><a href="<?php echo $this->url('fileupload/progress/session')?>">AJAX File Upload with Session Progress</a></li>
<li><a href="<?php echo $this->url('fileupload/progress/session_partial')?>">Partial AJAX File Upload with Session Progress</a></li>
</ul>

0 comments on commit 1f0691f

Please sign in to comment.