emagister/zend-form-decorators-bootstrap
Composer 安装命令:
composer require emagister/zend-form-decorators-bootstrap
包简介
Zend_Form decorators for Twitter's Bootstrap UI
README 文档
README
This is a set of Zend_Form decorators and helpers that help to render the markup needed to display any Zend_Form as a Twitter's Bootstrap form.
This is cool stuff, but how?
Here is an example form with some form fields
<?php class My_Bootstrap_Form extends Twitter_Bootstrap_Form_Inline { public function init() { $this->setIsArray(true); $this->setElementsBelongTo('bootstrap'); $this->_addClassNames('well'); $this->addElement('text', 'text', array( 'placeholder' => 'E-mail', 'prepend' => '@', 'class' => 'focused' )); $this->addElement('password', 'password', array( 'placeholder' => 'Password' )); $this->addElement('button', 'submit', array( 'label' => 'Login', 'type' => 'submit', 'buttonType' => 'success', 'icon' => 'ok', 'escape' => false )); } }
## API Documentation ##
Forms
Vertical forms (default style)
<?php class My_Vertical_Form extends Twitter_Bootstrap_Form_Vertical { public function init() { // Do your stuff here } }
Horizontal forms
<?php class My_Horizontal_Form extends Twitter_Bootstrap_Form_Horizontal { public function init() { // Do your stuff here } }
Inline forms
<?php class My_Inline_Form extends Twitter_Bootstrap_Form_Inline { public function init() { // Do your stuff here } }
Search form
Search forms can be rendered in a navigation bar or as a normal
forms. Search forms are @final classes that cannot be extended. Instead it supports several option in order to configure
the look and feel of the form.
-
renderInNavBar (boolean) Renders the form for displaying it in a navigation bar. In this mode no submit button will be rendered. (defaults to false)
-
pullItRight (boolean) This option only takes effect if the renderInNavBar option has been set to true, and its purpose is to align the search form at the right of the navigation bar. (defaults to false)
-
inputName (string) This option specifies a custom name for the search input. (defaults to searchQuery)
-
submitLabel (string) If the submit button will be rendered the label can be customized by passing the submitLabel option. (defaults to Submit)
<?php // Normal search form $searchForm = new Twitter_Bootstrap_Form_Search(array( 'renderInNavBar' => false, 'inputName' => 'q', 'submitLabel' => 'Search!' )); echo $searchForm; // Navigation bar search form $mavbarSearchForm = new Twitter_Bootstrap_Form_Search(array( 'renderInNavBar' => true, 'pullItRight' => true, 'inputName' => 'q', 'submitLabel' => 'Search!' )); echo $navbarSearchForm;
DisplayGroups
The base class Twitter_Bootstrap_Form (so inherently, all its subclasses) sets the custom class Twitter_Bootstrap_Form_DisplayGroup
as a base class for handling virtual field groups. So there is no need for special coding here.
<?php class My_Bootstrap_Form extends Twitter_Bootstrap_Form_Horizontal { public function init() { $this->addElement('text', 'email', array( 'label' => 'E-mail' )); $this->addElement('text', 'password', array( 'label' => 'Password' )); $this->addDisplayGroup( array('email', 'password'), 'login', array( 'legend' => 'Account info' ) ); } }
New form elements
In order to leverage the astonishing look and feel of Twitter Bootstrap buttons, a new special form element has been created in order to set all the appropiate class names.
Submit button
To configure the look and feel of a submit button there are several options supported
-
buttonType (string) This option specifies the button type that will be rendered. Twitter Bootstrap 2.0.0 supports six button types out of the box
- default (obviously, the default button style)
- primary
- info
- success
- warning
- danger
-
disabled (boolean) If true, the submit button will be rendered as disabled.
<?php class My_Bootstrap_Form extends Twitter_Bootstrap_Form_Vertical { public function init() { // All the other form stuff $this->addElement('submit', 'submit', array( 'buttonType' => Twitter_Bootstrap_Form_Element_Submit::BUTTON_SUCCESS, 'disabled' => true, 'label' => 'Send e-mail!' )); } }
Button
This form element is an extension of the submit button in order to provide a way to render HTML inside the button. So it's providing the same features as the submit button plus the hability to render a glyphicon as a button label. Let's see it.
-
icon (string) This option specify the icon used inside the button.
-
whiteIcon (boolean) This option specify whether the icon should be rendered in white color. (default false)
<?php class My_Bootstrap_Form extends Twitter_Bootstrap_Form_Horizontal { public function init() { // All the other form stuff $this->addElement('button', 'submit', array( 'label' => 'Send e-mail!', 'buttonType' => Twitter_Bootstrap_Form_Element_Submit::BUTTON_SUCCESS, 'icon' => 'ok', 'whiteIcon' => true, 'iconPosition' => Twitter_Bootstrap_Form_Element_Button::ICON_POSITION_RIGHT )); } }
Decorators
Actions decorator
The purpose of this decorator is to set the appropiate markup to render the form's submit buttons.
<?php class My_Bootstrap_Form extends Twitter_Bootstrap_Form_Horizontal { public function init() { $this->addElement('button', 'submit', array( 'label' => 'Submit!', 'type' => 'submit' )); $this->addElement('button', 'reset', array( 'label' => 'Reset', 'type' => 'reset' )); $this->addDisplayGroup( array('submit', 'reset'), 'actions', array( 'disableLoadDefaultDecorators' => true, 'decorators' => array('Actions') ) ); } }
Addon decorator
This decorator allows specify some content that will be appended or prepended to the given input. It can render text, a glyphicon or a checkbox. To accomplish this it supports several options that will be set to the form element itself. If you pass an element that is a Zend_Form_Element_Submit element, it will put it there without the span.
-
prepend (string) Prepends the content of the option to the generated field form.
-
append (string) Appends the content of the option to the generated field form.
The content of the both options could be a string containing some text, an array or an instance of the class Zend_Config
or a glyphicon. When
-
'Text' specified, it will render as is.
-
Zend_Config or array specified, it will generate a checkbox. Note that if the key "active" is passed inside the array or inside the Zend_Config instance, it will render the prepended or appended box with green background.
<?php class My_Bootstrap_Form extends Twitter_Bootstrap_Form_Horizontal { public function init() { // Some text $this->addElement('text', 'input1', array( 'label' => 'E-mail', 'prepend' => '@' )); // A checkbox $this->addElement('text', 'input2', array( 'label' => '2nd E-mail', 'prepend' => array( 'name' => 'internalCheckbox1', 'active' => true ) )); // A glyphicon $this->addElement('text', 'input2', array( 'label' => '2nd E-mail', 'prepend' => '<i class="icon-envelope"></i>' )); // A submit button $submitButton = $this->createElement('button', 'addButton', array( 'label' => 'Add' ); $this->addElement('text', 'input2', array( 'label' => '2nd E-mail', 'prepend' => $submitButton )); } }
Fieldsize decorator
In order to specify a size of an input a special decorator is executed at the very begining of the decorator chain to set the appropiate class name. The size of an input can be set by the element's attribute size.
<?php class My_Bootstrap_Form extends Twitter_Bootstrap_Form_Horizontal { public function init() { // Some text $this->addElement('text', 'input1', array( 'label' => 'E-mail', 'dimension' => 5 )); } }
Installation
In order to use Bootstrap 2.0.0, the needed assets need to be installed in your assets directory. Make sure to install also the glyphicons properly. Once all this stuff has been installed, create/modify a layout view script and add a reference to the Bootstrap CSS.
<!-- /application/layouts/scripts/default.phtml --> <?php $this->headLink()->appendStylesheet('/css/bootstrap.css'); ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <?php echo $this->headLink(); ?> </head> <body> <!-- All your HTML stuff --> </body> </html>
So that's it. Now you can start creating forms and rendering them the way shown before and you will get nice forms!
Contributors
- adepretis ad@25th-floor.com
- rafalgalka http://blog.modernweb.pl
- lrobert http://www.leerobert.ca
- marcelaraujo admin@marcelaraujo.me
emagister/zend-form-decorators-bootstrap 适用场景与选型建议
emagister/zend-form-decorators-bootstrap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 210.27k 次下载、GitHub Stars 达 165, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「bootstrap」 「zend」 「twitter」 「zend form」 「zend form decorators」 「twitter bootstrap」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 emagister/zend-form-decorators-bootstrap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 emagister/zend-form-decorators-bootstrap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 emagister/zend-form-decorators-bootstrap 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Handles basic OAuth/OAuth2 authentication along with classes for common services
Simple Twitter API wrapper
Simple Sharing generates social media share links within CP entry pages, allowing you to quickly & easily share entries.
Polyfill for mb_ereg(), mb_eregi(), mb_ereg_match(), and mb_ereg_replace*() functions; primary use case is for mbstring on Windows 7.4+
Zend Framework 1 Http package
Zend Framework 1 Soap package
统计信息
- 总下载量: 210.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 167
- 点击次数: 33
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04