Dojo 表单元素和装饰器
建立在 dijit 视图助手 之上的
有三个选项用来在你的表单里使用 Dojo 表单元素:
Example #1 在你的表单里开启 Dojo “等等” 你会说,“我已经用我自己定制的表单类扩展了 Zend_form!怎么 Dojo-enable 它?”
首先而且是最简单的是,修改从
第二个方法是在定制表单的
class My_Form_Custom extends Zend_Form
{
public function init()
{
// Dojo-enable the form:
Zend_Dojo::enableForm($this);
// ... continue form definition from here
// Dojo-enable all sub forms:
foreach ($this->getSubForms() as $subForm) {
Zend_Dojo::enableForm($subForm);
}
}
}
dijit-specific 表单装饰器和元素的用法和使用任何其它表单装饰漆或元素一样。 Dijit-Specific 表单装饰器大多数表单元素可以使用 DijitElement 装饰器,它从元素抓取 dijit 参数, 然后和其它元数据一起传递给由元素指定的视图助手。对于装饰表单、子表单和显示组, 有一组和不同的布局 dijits 对应的装饰器。
所有的 jijit 装饰器寻找给定被装饰元素的 DijitElement 装饰器
就像 视图助手装饰器,
DijitElement 也在元素里有个
每个元素有个独一无二的 ID (从元素的 标准用法是在装饰器链附加这个装饰器为第一个装饰器,不需要另外的选项。 Example #2 DijitElement 装饰器用法
$element->setDecorators(array(
'DijitElement',
'Errors',
'Label',
'ContentPane',
));
DijitForm 装饰器DijitForm 装饰器和 表单装饰器 非常类似;事实上,它们可以交互使用,因为它们使用同样的视图助手名('form')。 因为 dijit.form.Form 不要求任何 dijit 参数来配置,主要的不同之处是 dijit 表单视图助手 要求传递一个 DOM ID 来确保程序生成 dijit。装饰器通过传递表单名为表示符来保证它。 DijitContainer-based 装饰器
下列的装饰器从
Example #3 DijitContainer 装饰器用法
// Use a TabContainer for your form:
$form->setDecorators(array(
'FormElements',
array('TabContainer', array(
'id' => 'tabContainer',
'style' => 'width: 600px; height: 500px;',
'dijitParams' => array(
'tabPosition' => 'top'
),
)),
'DijitForm',
));
// Use a ContentPane in your sub form (which can be used with all but
// AccordionContainer):
$subForm->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'ContentPane',
));
Dijit-Specific 表单元素
每个被提供视图助手的 dijit 表单都有相应的
Dijit 参数存储在 另外,dijit-specific 元素实现一个不同的装饰器列表,相应如下:
$element->addDecorator('DijitElement')
->addDecorator('Errors')
->addDecorator('HtmlTag', array('tag' => 'dd'))
->addDecorator('Label', array('tag' => 'dt'));
实际上,DijitElement 装饰器用来替换标准的 ViewHelper 装饰器。 最后,基本的 Dijit 元素确保在视图中设置 Dojo 视图助手路径。
一个提供 下列 dijit 元素随标准的 Zend Framework 版本发行。 按钮虽然不是从 标准 Button 元素中派生, 它确实实现了相同的功能,并可以随时替换它。展示一下下列函数:
另外,只有 Example #4 按钮 dijit 元素用法范例
$form->addElement(
'Button',
'foo',
array(
'label' => 'Button Label',
)
);
检查框虽然不是从 标准检查框元素 里派生, 但它确实实现了相同的功能。展示一下下列函数:
Example #5 检查框 dijit 元素用法范例
$form->addElement(
'CheckBox',
'foo',
array(
'label' => 'A check box',
'checkedValue' => 'foo',
'uncheckedValue' => 'bar',
'checked' => true,
)
);
组合框(ComboBox)和 FilteringSelect正如在组合框 dijit 视图助手文档 里的注释, 组合框是介于选择和文本输入之间的混合体,可以从列表中选择也可以输入内容到列表里。 FilteringSelect 也一样,但不允许任意输入。
组合框和 FilteringSelect 表单元素提供访问器和增变器来检查和设置选择选项和指定 dojo.data 数据存储(如果使用的话)。
它们从 DijitMulti 继承,允许你通过
缺省地,如果没有 dojo.data 存储和元素一起注册,该元素就注册
一个 Example #6 使用组合框 dijit 元素做选择输入的范例
$form->addElement(
'ComboBox',
'foo',
array(
'label' => 'ComboBox (select)',
'value' => 'blue',
'autocomplete' => false,
'multiOptions' => array(
'red' => 'Rouge',
'blue' => 'Bleu',
'white' => 'Blanc',
'orange' => 'Orange',
'black' => 'Noir',
'green' => 'Vert',
),
)
);
Example #7 带数据存储的组合框 dijit 元素用法范例
$form->addElement(
'ComboBox',
'foo',
array(
'label' => 'ComboBox (datastore)',
'storeId' => 'stateStore',
'storeType' => 'dojo.data.ItemFileReadStore',
'storeParams' => array(
'url' => '/js/states.txt',
),
'dijitParams' => array(
'searchAttr' => 'name',
),
)
);
上述例子也可以使用 货币文字框货币文字框主要用来支持货币输入。货币可以是本地化的,并且支持带小数的和不带小数的数值。 在内部,货币文字框由 NumberTextBox、 ValidationTextBox 和 TextBox 派生, 所有对这些类可用的方法也对它可用。另外,也可用下列约束方法:
Example #8 货币文字框 dijit 元素用法范例
$form->addElement(
'CurrencyTextBox',
'foo',
array(
'label' => 'Currency:',
'required' => true,
'currency' => 'USD',
'invalidMessage' => 'Invalid amount. Include dollar sign, commas, and cents.',
'fractional' => false,
)
);
日期文字框日期文字框提供一个下拉式日历用来选择日期,也有一个客户端的日期校验和格式化。 在内部,日期文字框从 ValidationTextBox 和 TextBox 派生, 所有对这些类可用的方法也对它可用。另外,下列方法也可以独立使用:
Example #9 日期文字框 dijit 元素用法范例
$form->addElement(
'DateTextBox',
'foo',
array(
'label' => 'Date:',
'required' => true,
'invalidMessage' => 'Invalid date specified.',
'formatLength' => 'long',
)
);
水平滑尺(HorizontalSlider)水平滑尺提供了一个滑尺 UI 小部件用来在一个范围内选择数值。 在内部,它设置一个隐藏元素的值然后由表单提交。 水平滑尺从 abstract Slider dijit element 派生。另外,有许多方法用来设置和配置滑尺刻度和刻度标签。
Example #10 水平滑尺 dijit 元素用法范例 下面创建一个整数范围为 -10到10的水平滑尺。顶上有 20%、 40%、 60% 和 80%标记。 底下有 0 、50% 和 100%。每次改变,隐藏元素的值就更新一次。
$form->addElement(
'HorizontalSlider',
'horizontal',
array(
'label' => 'HorizontalSlider',
'value' => 5,
'minimum' => -10,
'maximum' => 10,
'discreteValues' => 11,
'intermediateChanges' => true,
'showButtons' => true,
'topDecorationDijit' => 'HorizontalRuleLabels',
'topDecorationContainer' => 'topContainer',
'topDecorationLabels' => array(
' ',
'20%',
'40%',
'60%',
'80%',
' ',
),
'topDecorationParams' => array(
'container' => array(
'style' => 'height:1.2em; font-size=75%;color:gray;',
),
'list' => array(
'style' => 'height:1em; font-size=75%;color:gray;',
),
),
'bottomDecorationDijit' => 'HorizontalRule',
'bottomDecorationContainer' => 'bottomContainer',
'bottomDecorationLabels' => array(
'0%',
'50%',
'100%',
),
'bottomDecorationParams' => array(
'list' => array(
'style' => 'height:1em; font-size=75%;color:gray;',
),
),
)
);
数字微调控制器(NumberSpinner)数字微调控制器是一个文本元素用来输入数字值;它也包括用于增加和减少一定值的元素。 可用方法如下:
Example #11 数字微调控制器 dijit 元素用法范例
$form->addElement(
'NumberSpinner',
'foo',
array(
'value' => '7',
'label' => 'NumberSpinner',
'smallDelta' => 5,
'largeDelta' => 25,
'defaultTimeout' => 500,
'timeoutChangeRate' => 100,
'min' => 9,
'max' => 1550,
'places' => 0,
'maxlength' => 20,
)
);
数字框数字框是一个文本元素用来输入数字值,不象数字增变器,数字框用手工输入。 校验和限制可以使数值在一定的范围并符合一定的格式。 在内部,数字框从 ValidationTextBox 和 TextBox派生; 所有对这些类可用的方法都可用,另外,下列方法可以独立使用:
Example #12 数字框 dijit 元素用法范例
$form->addElement(
'NumberTextBox',
'elevation',
array(
'label' => 'NumberTextBox',
'required' => true,
'invalidMessage' => 'Invalid elevation.',
'places' => 0,
'constraints' => array(
'min' => -20000,
'max' => 20000,
),
)
);
密码框密码框是个 ValidationTextBox,它和密码输入有联系;它唯一的意图是允许 dijit-themed 文本输入密码并提供客户端校验。 在内部,密码框从 ValidationTextBox 和 TextBox 派生; 所有对这些类可用的方法都可用。 Example #13 密码框 dijit 元素用法范例
$form->addElement(
'PasswordTextBox',
'password',
array(
'label' => 'Password',
'required' => true,
'trim' => true,
'lowercase' => true,
'regExp' => '^[a-z0-9]{6,}$',
'invalidMessage' => 'Invalid password; must be at least 6 alphanumeric characters',
)
);
单选按钮单选按钮封装了标准单选输入元素来提供一个和其它 dojo dijits 一致的外观。 (因为英文为 radio button,单选按钮在过去也被翻译为无线电按钮,但从功能上讲,单选按钮更准确 Jason Qi注)
单选按钮从 DijitMulti 继承,它让你通过
缺省地,该元素注册一个 Example #14 单选按钮 dijit 元素用法范例
$form->addElement(
'RadioButton',
'foo',
array(
'label' => 'RadioButton',
'multiOptions' => array(
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
),
'value' => 'bar',
)
);
滑尺摘要元素水平滑尺 和 垂直滑尺 都是从滑尺摘要元素派生而来的。它包含了许多通用的方法来配置你的滑尺,包括:
范例在每个具体的继承类里提供。 提交按钮虽然没有叫提交按钮的 Dijit,但我们还是在此提供一个按钮 dijit 用来提交 表单而不需请求任何另外的 javascript 绑定。它的工作方式和 按钮 dijit 一模一样。 Example #15 Example SubmitButton dijit element usage
$form->addElement(
'SubmitButton',
'foo',
array(
'required' => false,
'ignore' => true,
'label' => 'Submit Button!',
)
);
文字框文字框用来输入文字,在外观上和其它 dijit 保持一致。然而,它有一些过滤和校验的功能,详见下列方法:
Example #16 文字框 dijit 元素用法范例
$form->addElement(
'TextBox',
'foo',
array(
'value' => 'some text',
'label' => 'TextBox',
'trim' => true,
'propercase' => true,
)
);
TextareaTextarea 就像标准的 HTML textarea一样。然而,它不支持行列的设置。 作为替代,textarea 宽度可以用标准的 CSS 来设置,行就完全忽略了。当输入字符增加时,textarea 就垂直增长。 Example #17 Textarea dijit 元素用法范例
$form->addElement(
'Textarea',
'textarea',
array(
'label' => 'Textarea',
'required' => true,
'style' => 'width: 200px;',
)
);
时间文字框时间文字框是提供下拉式选择时间的文字输入框。下拉框可配置为显示一特定的时间窗口,带有特定的增量。 在内部,时间文字框从 日期文字框、 校验文字框 和 文字框 派生。 所有对这些类可用的方法都可用。另外,下列方法可以独立使用:
Example #18 时间文字框 dijit 元素用法范例 下面讲生成一个时间文字框,每次显示 2 小时,增量是 10 分钟。
$form->addElement(
'TimeTextBox',
'foo',
array(
'label' => 'TimeTextBox',
'required' => true,
'visibleRange' => 'T04:00:00',
'visibleIncrement' => 'T00:10:00',
'clickableIncrement' => 'T00:10:00',
)
);
校验文字框校验文字框添加校验和限制给文字输入。 在内部,它从 文字框 派生, 并增加了下列访问器和增变器来处理 dijit 参数:
Example #19 校验文字框 dijit 元素用法范例 下面生成一个校验文字框,要求只包含字符的单个字符串(例如,没有空格,大部分标点符号无效)。
$form->addElement(
'ValidationTextBox',
'foo',
array(
'label' => 'ValidationTextBox',
'required' => true,
'regExp' => '[\w]+',
'invalidMessage' => 'Invalid non-space text.',
)
);
垂直滑尺(VerticalSlider)垂直滑尺是水平滑尺的兄弟, 操作基本相同。唯一的区别是,对垂直滑尺来说使用了 'left*' 和 'right*' 替换了 在水平滑尺中的 'top*' 和 'bottom*',并且使用 VerticalRule 和 VerticalRuleLabels 分别替代了 HorizontalRule 和 HorizontalRuleLabels。 Example #20 垂直 dijit 元素使用范例 下面创建一个整数范围为 -10到10的垂直滑尺。左边有 20%、 40%、 60% 和 80%标记。 右边有 0 、50% 和 100%。每次改变,隐藏元素的值就更新一次。
$form->addElement(
'VerticalSlider',
'foo',
array(
'label' => 'VerticalSlider',
'value' => 5,
'style' => 'height: 200px; width: 3em;',
'minimum' => -10,
'maximum' => 10,
'discreteValues' => 11,
'intermediateChanges' => true,
'showButtons' => true,
'leftDecorationDijit' => 'VerticalRuleLabels',
'leftDecorationContainer' => 'leftContainer',
'leftDecorationLabels' => array(
' ',
'20%',
'40%',
'60%',
'80%',
' ',
),
'rightDecorationDijit' => 'VerticalRule',
'rightDecorationContainer' => 'rightContainer',
'rightDecorationLabels' => array(
'0%',
'50%',
'100%',
),
)
);
Dojo 表单范例Example #21 使用 Zend_Dojo_Form
和
class My_Form_Test extends Zend_Dojo_Form
{
/**
* Options to use with select elements
*/
protected $_selectOptions = array(
'red' => 'Rouge',
'blue' => 'Bleu',
'white' => 'Blanc',
'orange' => 'Orange',
'black' => 'Noir',
'green' => 'Vert',
);
/**
* Form initialization
*
* @return void
*/
public function init()
{
$this->setMethod('post');
$this->setAttribs(array(
'name' => 'masterForm',
));
$this->setDecorators(array(
'FormElements',
array('TabContainer', array(
'id' => 'tabContainer',
'style' => 'width: 600px; height: 500px;',
'dijitParams' => array(
'tabPosition' => 'top'
),
)),
'DijitForm',
));
$textForm = new Zend_Dojo_Form_SubForm();
$textForm->setAttribs(array(
'name' => 'textboxtab',
'legend' => 'Text Elements',
'dijitParams' => array(
'title' => 'Text Elements',
),
));
$textForm->addElement(
'TextBox',
'textbox',
array(
'value' => 'some text',
'label' => 'TextBox',
'trim' => true,
'propercase' => true,
)
)
->addElement(
'DateTextBox',
'datebox',
array(
'value' => '2008-07-05',
'label' => 'DateTextBox',
'required' => true,
)
)
->addElement(
'TimeTextBox',
'timebox',
array(
'label' => 'TimeTextBox',
'required' => true,
)
)
->addElement(
'CurrencyTextBox',
'currencybox',
array(
'label' => 'CurrencyTextBox',
'required' => true,
// 'currency' => 'USD',
'invalidMessage' => 'Invalid amount. Include dollar sign, commas, and cents.',
// 'fractional' => true,
// 'symbol' => 'USD',
// 'type' => 'currency',
)
)
->addElement(
'NumberTextBox',
'numberbox',
array(
'label' => 'NumberTextBox',
'required' => true,
'invalidMessage' => 'Invalid elevation.',
'constraints' => array(
'min' => -20000,
'max' => 20000,
'places' => 0,
)
)
)
->addElement(
'ValidationTextBox',
'validationbox',
array(
'label' => 'ValidationTextBox',
'required' => true,
'regExp' => '[\w]+',
'invalidMessage' => 'Invalid non-space text.',
)
)
->addElement(
'Textarea',
'textarea',
array(
'label' => 'Textarea',
'required' => true,
'style' => 'width: 200px;',
)
);
$toggleForm = new Zend_Dojo_Form_SubForm();
$toggleForm->setAttribs(array(
'name' => 'toggletab',
'legend' => 'Toggle Elements',
));
$toggleForm->addElement(
'NumberSpinner',
'ns',
array(
'value' => '7',
'label' => 'NumberSpinner',
'smallDelta' => 5,
'largeDelta' => 25,
'defaultTimeout' => 1000,
'timeoutChangeRate' => 100,
'min' => 9,
'max' => 1550,
'places' => 0,
'maxlength' => 20,
)
)
->addElement(
'Button',
'dijitButton',
array(
'label' => 'Button',
)
)
->addElement(
'CheckBox',
'checkbox',
array(
'label' => 'CheckBox',
'checkedValue' => 'foo',
'uncheckedValue' => 'bar',
'checked' => true,
)
)
->addElement(
'RadioButton',
'radiobutton',
array(
'label' => 'RadioButton',
'multiOptions' => array(
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
),
'value' => 'bar',
)
);
$selectForm = new Zend_Dojo_Form_SubForm();
$selectForm->setAttribs(array(
'name' => 'selecttab',
'legend' => 'Select Elements',
));
$selectForm->addElement(
'ComboBox',
'comboboxselect',
array(
'label' => 'ComboBox (select)',
'value' => 'blue',
'autocomplete' => false,
'multiOptions' => $this->_selectOptions,
)
)
->addElement(
'ComboBox',
'comboboxremote',
array(
'label' => 'ComboBox (remoter)',
'storeId' => 'stateStore',
'storeType' => 'dojo.data.ItemFileReadStore',
'storeParams' => array(
'url' => '/js/states.txt',
),
'dijitParams' => array(
'searchAttr' => 'name',
),
)
)
->addElement(
'FilteringSelect',
'filterselect',
array(
'label' => 'FilteringSelect (select)',
'value' => 'blue',
'autocomplete' => false,
'multiOptions' => $this->_selectOptions,
)
)
->addElement(
'FilteringSelect',
'filterselectremote',
array(
'label' => 'FilteringSelect (remoter)',
'storeId' => 'stateStore',
'storeType' => 'dojo.data.ItemFileReadStore',
'storeParams' => array(
'url' => '/js/states.txt',
),
'dijitParams' => array(
'searchAttr' => 'name',
),
)
);
$sliderForm = new Zend_Dojo_Form_SubForm();
$sliderForm->setAttribs(array(
'name' => 'slidertab',
'legend' => 'Slider Elements',
));
$sliderForm->addElement(
'HorizontalSlider',
'horizontal',
array(
'label' => 'HorizontalSlider',
'value' => 5,
'minimum' => -10,
'maximum' => 10,
'discreteValues' => 11,
'intermediateChanges' => true,
'showButtons' => true,
'topDecorationDijit' => 'HorizontalRuleLabels',
'topDecorationContainer' => 'topContainer',
'topDecorationLabels' => array(
' ',
'20%',
'40%',
'60%',
'80%',
' ',
),
'topDecorationParams' => array(
'container' => array(
'style' => 'height:1.2em; font-size=75%;color:gray;',
),
'list' => array(
'style' => 'height:1em; font-size=75%;color:gray;',
),
),
'bottomDecorationDijit' => 'HorizontalRule',
'bottomDecorationContainer' => 'bottomContainer',
'bottomDecorationLabels' => array(
'0%',
'50%',
'100%',
),
'bottomDecorationParams' => array(
'list' => array(
'style' => 'height:1em; font-size=75%;color:gray;',
),
),
)
)
->addElement(
'VerticalSlider',
'vertical',
array(
'label' => 'VerticalSlider',
'value' => 5,
'style' => 'height: 200px; width: 3em;',
'minimum' => -10,
'maximum' => 10,
'discreteValues' => 11,
'intermediateChanges' => true,
'showButtons' => true,
'leftDecorationDijit' => 'VerticalRuleLabels',
'leftDecorationContainer' => 'leftContainer',
'leftDecorationLabels' => array(
' ',
'20%',
'40%',
'60%',
'80%',
' ',
),
'rightDecorationDijit' => 'VerticalRule',
'rightDecorationContainer' => 'rightContainer',
'rightDecorationLabels' => array(
'0%',
'50%',
'100%',
),
)
);
$this->addSubForm($textForm, 'textboxtab')
->addSubForm($toggleForm, 'toggletab')
->addSubForm($selectForm, 'selecttab')
->addSubForm($sliderForm, 'slidertab');
}
}
Example #22 修改已存在的表单来使用 Dojo
通过使用 第一个范例示范装饰已存在的表单实例:
$form = new My_Custom_Form();
Zend_Dojo::enableForm($form);
$form->addElement(
'ComboBox',
'query',
array(
'label' => 'Color:',
'value' => 'blue',
'autocomplete' => false,
'multiOptions' => array(
'red' => 'Rouge',
'blue' => 'Bleu',
'white' => 'Blanc',
'orange' => 'Orange',
'black' => 'Noir',
'green' => 'Vert',
),
)
);
另外,你可以在表单初始化上做点小文章:
class My_Custom_Form extends Zend_Form
{
public function init()
{
Zend_Dojo::enableForm($this);
// ...
}
}
当然,如果你可以那样做 ... 你可以并应该简单地修改类来继承 Zend_Dojo_Form, 它可以随时替换已经 Dojo-enabled 的 Zend_Form ...
|