Skip to content

Using instance Zend_Form_Element instead of directly adding an element to the form. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4c57dd2
FIX: Overwrite function addElement to set decorators for instance tra…
wendrowycz Jun 21, 2016
01fc5f1
FIX: Get string type of element
wendrowycz Jun 21, 2016
988de4d
Update README.md
wendrowycz Jun 21, 2016
34c2dc6
Update README.md
wendrowycz Jun 21, 2016
c4860b3
Update README.md
wendrowycz Jun 21, 2016
153c588
Change composer config
wendrowycz Jun 21, 2016
ee35bc5
FIX: Special decorator for file type
wendrowycz Jun 27, 2016
0d44fd1
Add file decorator
wendrowycz Jun 27, 2016
eae9d4f
Merge tag 'file_decorator' into develop
wendrowycz Jun 27, 2016
402502d
FIX: add SubForm
wendrowycz Jul 21, 2016
5bf07cc
Merge branch 'release/1.0.5'
wendrowycz Jul 21, 2016
449f1cc
Merge tag '1.0.5' into develop
wendrowycz Jul 21, 2016
f3101d8
FIX: defalt decorators for select
wendrowycz Jul 28, 2016
1209c0a
Merge branch 'release/1.0.6'
wendrowycz Jul 28, 2016
40be81a
Add version badge
wendrowycz Jul 28, 2016
8013a6b
Prevent PHP (7) Error
Cruiser13 Aug 5, 2016
1cd71bd
Remove dimensions if any
Cruiser13 Aug 5, 2016
5ba6e23
Prevent PHP (7) Error (#1)
Cruiser13 Aug 8, 2016
b52864a
Comply with Zend_View_Helper_FormReset
Cruiser13 Aug 22, 2016
8a69ff2
Add support for "class" attrib
Cruiser13 Sep 6, 2016
d41827c
Some improvements on reset and dimensions (#2)
Cruiser13 Sep 6, 2016
5e09023
Merge pull request #1 from wendrowycz/master
Cruiser13 Sep 7, 2016
6d71a30
Disable escaping of descriptions
Cruiser13 Sep 13, 2016
111a526
Add file decorators for horizontal forms
Cruiser13 Sep 15, 2016
231fbd7
Add possibility for inline forms with label part 1
Cruiser13 Sep 22, 2016
f12b48f
Add possibility for inline forms with label part 2
Cruiser13 Sep 22, 2016
da13f82
Add possibility to add a class to the form-group
Cruiser13 Sep 26, 2016
f5f75d1
Remove attribute formgroupclass from html output
Cruiser13 Sep 26, 2016
cff2d97
Add info about Inlinelabel and formgroup class
Cruiser13 Sep 26, 2016
41fd7e0
Add HTML element to inject html directly between form elements
Cruiser13 Sep 26, 2016
cad9ff7
Fix info on html element
Cruiser13 Sep 26, 2016
b140461
Remove unneccessary html form element code
Cruiser13 Sep 26, 2016
507d002
Add own composer info for packagist
Cruiser13 Sep 27, 2016
6534f48
lowercase
Cruiser13 Sep 27, 2016
302acb3
Rename to extended
Cruiser13 Sep 27, 2016
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
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
#Twitter Bootstrap 3 Form for Zend Framework 1
#Twitter Bootstrap 3 Form for Zend Framework 1 extended

This library was originally created by @zfbase and forked by @wendrowycz
This is an extended version, adding an HTML element, inline form option with labels, form-group class option and more.

## Form types

The library supports all Bootstrap 3 form types.

### One PHP code
```php
class Application_Form_Example extends Twitter_Bootstrap3_Form_*
{
public function init()
{
$email = new Twitter_Bootstrap3_Form_Element_Email('email');
$email->setLabel('Email')->setAttrib('placeholder', 'Email');

$password = new Zend_Form_Element_Password('password');
$password->setLabel('Password')->setAttrib('placeholder', 'Password');

$checkbox = new Zend_Form_Element_Checkbox('checkbox');
$checkbox->setLabel('Remember me');

$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Sign in');

$this->addElements(array(
$email,
$password,
$checkbox,
$submit

));
}
}
```
#### OR

```php
class Application_Form_Example extends Twitter_Bootstrap3_Form_*
Expand Down Expand Up @@ -127,6 +158,26 @@ The form must be inherited from class `Twitter_Bootstrap3_Form_Inline`.
</form>
```

To use inlineforms with labels you can use the class `Twitter_Bootstrap3_Form_Inlinelabel`.

If you are using inline forms, the dimensions will not be set of course. If you need dimensions, you can add them using the attribute formgroupclass like this:

```php

$count = new Twitter_Bootstrap3_Form_Element_Number('count');
$count->setAttrib('formgroupclass','col-xs-6');

```

This will give you the following output:

```html
<div class="form-group col-xs-6">
<input type="number" name="count" id="count" value="" class="form-control">
</div>
```


## Supported controls

The library supports all Zend Framework 1 and Bootstrap 3 form elementes.
Expand Down Expand Up @@ -425,4 +476,4 @@ $this->addElement('static', 'static', array(
```

### Other decorated elements
The library also contains elements for decorators: `file`, `hidden`, `hash` and `captcha`.
The library also contains elements for decorators: `file`, `hidden`, `hash` and `captcha`.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zfbase/zend1-bootstrap3",
"description": "Twitter Bootstrap v.3 Forms for Zend Framework v.1",
"name": "cruiser13/zend1-bootstrap3",
"description": "Twitter Bootstrap v.3 Forms for Zend Framework v.1 extended",
"require": {
"php": ">=5.3.0",
"zendframework/zendframework1": "~1"
Expand All @@ -15,6 +15,7 @@
"zend",
"zend form",
"zend form decorators",
"zend form elements",
"form",
"twitter",
"bootstrap",
Expand Down
129 changes: 126 additions & 3 deletions library/Twitter/Bootstrap3/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class Twitter_Bootstrap3_Form extends Zend_Form
const DISPOSITION_HORIZONTAL = 'horizontal';
const DISPOSITION_VERTICAL = 'vertical';
const DISPOSITION_INLINE = 'inline';
const DISPOSITION_INLINE_LABEL = 'inlinelabel';

/**
* Disposition type class
Expand All @@ -32,6 +33,7 @@ abstract class Twitter_Bootstrap3_Form extends Zend_Form
self::DISPOSITION_HORIZONTAL => 'form-horizontal',
self::DISPOSITION_VERTICAL => 'form-vertical',
self::DISPOSITION_INLINE => 'form-inline',
self::DISPOSITION_INLINE_LABEL => 'form-inline',
);

/**
Expand Down Expand Up @@ -100,6 +102,12 @@ abstract class Twitter_Bootstrap3_Form extends Zend_Form
* @var array
*/
protected $_simpleElementDecorators;

/**
* Global decorators to apply to all elements type file
* @var array
*/
protected $_fileElementDecorators;

/**
* Global decorators to apply to all elements type checkbox
Expand Down Expand Up @@ -188,7 +196,8 @@ public function loadDefaultElementDecorators()
$this->_checkboxDecorators = $this->getDefaultCheckboxDecorators();
$this->_buttonsDecorators = $this->getDefaultButtonsDecorators();
$this->_imageDecorators = $this->getDefaultImageDecorators();

$this->_fileElementDecorators = $this->getDefaultFileElementDecorators();

return $this;
}

Expand All @@ -212,6 +221,37 @@ public function getDefaultSimpleElementDecorators()
array('Description', array(
'tag' => 'p',
'class' => 'help-block',
'escape' => false,
)),
array('Label', array(
'class' => 'control-label',
)),
array('Container'),
array('FieldSize'),
);
}

/**
* Retrieve all decorators for file type elements
*
* @return array
*/
public function getDefaultFileElementDecorators()
{
return array(
array('File'),
array('Addon'),
array('Feedback_State', array(
'renderIcon' => $this->_renderElementsStateIcons,
'successIcon' => $this->_elementsSuccessIcon,
'warningIcon' => $this->_elementsWarningIcon,
'errorIcon' => $this->_elementsErrorIcon,
)),
array('Errors'),
array('Description', array(
'tag' => 'p',
'class' => 'help-block',
'escape' => false,
)),
array('Label', array(
'class' => 'control-label',
Expand All @@ -233,6 +273,7 @@ public function getDefaultCaptchaDecorators()
array('Description', array(
'tag' => 'p',
'class' => 'help-block',
'escape' => false,
)),
array('Label', array(
'class' => 'control-label',
Expand All @@ -256,6 +297,7 @@ public function getDefaultCheckboxDecorators()
array('Description', array(
'tag' => 'p',
'class' => 'help-block',
'escape' => false,
)),
array('CheckboxControls'),
array('Container'),
Expand All @@ -275,6 +317,7 @@ public function getDefaultButtonsDecorators()
array('Description', array(
'tag' => 'p',
'class' => 'help-block',
'escape' => false,
)),
array('ViewHelper'),
array('Container'),
Expand All @@ -294,6 +337,7 @@ public function getDefaultImageDecorators()
array('Description', array(
'tag' => 'p',
'class' => 'help-block',
'escape' => false,
)),
array('Image'),
array('Errors'),
Expand Down Expand Up @@ -366,6 +410,78 @@ public function createElement($type, $name, $options = null)

return parent::createElement($type, $name, $options);
}

/**
* Add a new element
*
* $element may be either a string element type, or an object of type
* Zend_Form_Element. If a string element type is provided, $name must be
* provided, and $options may be optionally provided for configuring the
* element.
*
* If a Zend_Form_Element is provided, $name may be optionally provided,
* and any provided $options will be ignored.
*
* @param string|Zend_Form_Element $element
* @param string $name
* @param array|Zend_Config $options
* @throws Zend_Form_Exception on invalid element
* @return Zend_Form
*/
public function addElement($element, $name = null, $options = null)
{
if ($element instanceof Zend_Form_Element) {
// type string
$exploderesult = explode('_', $element->getType());
$type = lcfirst(trim(end($exploderesult)));

if (null !== $options && $options instanceof Zend_Config) {
$options = $options->toArray();
}

// Load default decorators
if ((null === $options) || !is_array($options)) {
$options = array();
//Class is maybe not properly transfered to this element. We'll add class if it exists here.
if($element->class){
$options['class'] = $element->class;
}
}

if (!array_key_exists('decorators', $options)) {
$decorators = $this->getDefaultDecoratorsByElementType($type);
if (!empty($decorators)) {
$options['decorators'] = $decorators;
}
}

// Elements type use 'form-control' class
$element_fc = array(
// all input:
'text', 'password', 'dateTime', 'dateTimeLocal', 'date', 'month',
'time', 'week', 'number', 'email', 'url', 'search', 'tel', 'color',
// and other:
'textarea', 'select', 'multiselect',
);
if (in_array($type, $element_fc)) {
if (null === $options) {
$options = array('class' => 'form-control');
} elseif (array_key_exists('class', $options)) {
if (!strstr($options['class'], 'form-control')) {
$options['class'] .= ' form-control';
$options['class'] = trim($options['class']);
}
} else {
$options['class'] = 'form-control';
}
}

$element->setOptions($options);

}

parent::addElement($element, $name, $options);
}

/**
* Retrieve a registered decorator for type element
Expand Down Expand Up @@ -406,8 +522,15 @@ public function getDefaultDecoratorsByElementType($type)
return $this->_simpleElementDecorators;
}
break;
case 'note': case 'static': case 'select': case 'multiselect':
case 'file': case 'textarea': case 'radio': case 'multiCheckbox':
case 'file':
if (is_array($this->_fileElementDecorators)) {
return $this->_fileElementDecorators;
}
break;
case 'html':
return array('ViewHelper');
case 'note': case 'static': case 'select': case 'multiselect':
case 'textarea': case 'radio': case 'multiCheckbox':
if (is_array($this->_simpleElementDecorators)) {
$decorators = $this->_simpleElementDecorators;
$removeI = null;
Expand Down
5 changes: 5 additions & 0 deletions library/Twitter/Bootstrap3/Form/Decorator/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function render($content)

$class = ' ' . $this->getOption('class');
$class .= ' form-group';

//Add possibility to add custom classes to the form-group class by attribute
if($element->getAttrib('formgroupclass')) {
$class .= ' '.$element->getAttrib('formgroupclass');
}

if ($element->hasErrors()) {
$class .= ' has-error';
Expand Down
43 changes: 43 additions & 0 deletions library/Twitter/Bootstrap3/Form/Decorator/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Form
* @subpackage Decorator
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/** Zend_Form_Decorator_Abstract */
require_once 'Zend/Form/Decorator/Abstract.php';

/** Zend_Form_Decorator_Marker_File_Interface */
require_once 'Zend/Form/Decorator/Marker/File/Interface.php';

/** Zend_File_Transfer_Adapter_Http */
require_once 'Zend/File/Transfer/Adapter/Http.php';

/**
* Zend_Form_Decorator_File
*
* Fixes the rendering for all subform and multi file elements
*
* @category Zend
* @package Zend_Form
* @subpackage Decorator
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
class Twitter_Bootstrap3_Form_Decorator_File extends Zend_Form_Decorator_File {}
6 changes: 6 additions & 0 deletions library/Twitter/Bootstrap3/Form/Decorator/ViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function getElementAttribs()
unset($attribs['success']); // Twitter_Bootstrap3_Form_Decorator_Container
unset($attribs['warning']); // Twitter_Bootstrap3_Form_Decorator_Container

//Remove dimensions if any
unset($attribs['dimensionLabel']);
unset($attribs['dimensionControls']);
unset($attribs['dimension']);
unset($attribs['formgroupclass']);

return $attribs;
}
}
27 changes: 27 additions & 0 deletions library/Twitter/Bootstrap3/Form/Element/Html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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_Html extends Zend_Form_Element_Xhtml
{

public $helper='FormHtml';

public function isValid($value){

return true;
}
}
Loading