Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New nested fieldset example with FilePRG
- Loading branch information
Showing
6 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<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) ?: '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); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters