Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,5 +424,70 @@ $this->addElement('static', 'static', array(
));
```

### File

Include support for file upload fields. Make shure to add the file "patch.css" to your html for correct styling!

```html
<div class="form-group">
<label class="control-label required" for="ID">Label Text</label>
<input type="hidden" id="MAX_FILE_SIZE" value="134217728" name="MAX_FILE_SIZE">
<div class="spacer"></div>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Btn Text<input type="file" btntext="Btn Text" id="ID" name="ID">
</span>
</span>
<input type="text" readonly="" class="form-control">
</div>
<p class="help-block">Help-Text</p>
</div>
```

```php
$this->addElement('file', 'ID', array(
'label' => 'Label Text',
'description' => 'Help-Text',
'required' => true,
'btnText' => 'Btn Text'
));

// Optional settings
$uploadField = $this->getElement('ID');
$uploadField->setDestination('path/to/uploads-dir');
$uploadField->addValidator('Count', false, 1); // just one file
$uploadField->addValidator('Size', false, 1048576); // 1MB upload limit
$uploadField->addValidator('Extension', false, 'jpg,png,gif'); // jpg, png and gif
```

Add this little jQuery snippet for better handling (http://codepen.io/claviska/pen/vAgmd)

```js
$(document).ready(function() {
// Custom bootstrap file upload
$(document).on('change', '.btn-file :file', function() {
var input = $(this);
var numFiles = input.get(0).files ? input.get(0).files.length : 1;
var label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label]);
});

$('.btn-file :file').on('fileselect', function(event, numFiles, label) {
var input = $(this).parents('.input-group').find(':text');
var log = numFiles > 1 ? numFiles + ' files selected' : label;

if (input.length) {
input.val(log);
} else {
if (log) {
console.log(log);
}
}

});
});
```

### Other decorated elements
The library also contains elements for decorators: `file`, `hidden`, `hash` and `captcha`.
6 changes: 1 addition & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"name": "zfbase/zend1-bootstrap3",
"name": "sideshow-systems/zend1-bootstrap3",
"description": "Twitter Bootstrap v.3 Forms for Zend Framework v.1",
"require": {
"php": ">=5.3.0",
"zendframework/zendframework1": "~1"
},
"autoload": {
"psr-0": { "Twitter_Bootstrap3_": "library/" }
},
Expand Down
25 changes: 25 additions & 0 deletions library/Twitter/Bootstrap3/Form/Element/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Twitter Bootstrap v.3 Form for Zend Framework v.1
*
* @category Forms
* @package Twitter_Bootstrap3_Form
* @subpackage Element
* @author Ilya Serdyuk <ilya.serdyuk@youini.org>
*/

/**
* Email form element
*
* @category Forms
* @package Twitter_Bootstrap3_Form
* @subpackage Element
*/
class Twitter_Bootstrap3_Form_Element_File extends Zend_Form_Element_File
{
/**
* Default form view helper to use for rendering
* @var string
*/
public $helper = 'formFile';
}
52 changes: 52 additions & 0 deletions library/Twitter/Bootstrap3/View/Helper/FormFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Twitter Bootstrap v.3 Form for Zend Framework v.1
*
* @category Forms
* @package Twitter_Bootstrap3_View
* @subpackage Helper
* @author Ilya Serdyuk <ilya.serdyuk@youini.org>
*/

/**
* Helper to generate a "text" element
*
* @category Forms
* @package Twitter_Bootstrap3_View
* @subpackage Helper
*/
class Twitter_Bootstrap3_View_Helper_FormFile extends Zend_View_Helper_FormFile
{

public function formFile($name, $attribs = null) {
$info = $this->_getInfo($name, null, $attribs);
extract($info); // name, id, value, attribs, options, listsep, disable

// is it disabled?
$disabled = '';
if ($disable) {
$disabled = ' disabled="disabled"';
}

$buttonText = (isset($attribs['btnText']) && !empty($attribs['btnText'])) ? $attribs['btnText'] : 'Upload file';

// build the element
$xhtml = '<div class="spacer"></div>'
. '<div class="input-group">'
. '<span class="input-group-btn">'
. '<span class="btn btn-primary btn-file">'
. $buttonText
. '<input type="file"'
. ' name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id) . '"'
. $disabled
. $this->_htmlAttribs($attribs)
. $this->getClosingBracket()
. '</span>'
. '</span>'
. '<input type="text" class="form-control" readonly>'
. '</div>';

return $xhtml;
}
}
21 changes: 21 additions & 0 deletions library/Twitter/Bootstrap3/patch.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@ input[type=range]:after {
padding-left: 0.5em;
content: attr(data-value);
}

/** file upload button **/
.btn-file {
position: relative;
overflow: hidden;
}
.btn-file input[type=file] {
position: absolute;
top: 0px;
right: 0px;
min-width: 100%;
min-height: 100%;
font-size: 100px;
text-align: right;
filter: alpha(opacity=0);
opacity: 0;
outline: none;
background: white;
cursor: inherit;
display: block;
}