Zend_Dojo-Form-Examples.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <sect2 id="zend.dojo.form.examples">
  2. <title>Dojo 表单范例</title>
  3. <example id="zend.dojo.form.examples.dojoform">
  4. <title>使用 Zend_Dojo_Form</title>
  5. <para>
  6. 和 <code>Zend_Form</code> 一起使用 Dojo的最容易的办法是利用 <code>Zend_Dojo_Form</code>,
  7. 直接使用或继承都可以。这个范例继承 Zend_Dojo_Form,并示范了所有 dijit 元素的用法。
  8. 它生成了四个子表单,并装饰了表单来使用 TabContainer,把每个子表单显示在它自己的 tab 上。
  9. </para>
  10. <programlisting role="php"><![CDATA[
  11. class My_Form_Test extends Zend_Dojo_Form
  12. {
  13. /**
  14. * Options to use with select elements
  15. */
  16. protected $_selectOptions = array(
  17. 'red' => 'Rouge',
  18. 'blue' => 'Bleu',
  19. 'white' => 'Blanc',
  20. 'orange' => 'Orange',
  21. 'black' => 'Noir',
  22. 'green' => 'Vert',
  23. );
  24. /**
  25. * Form initialization
  26. *
  27. * @return void
  28. */
  29. public function init()
  30. {
  31. $this->setMethod('post');
  32. $this->setAttribs(array(
  33. 'name' => 'masterForm',
  34. ));
  35. $this->setDecorators(array(
  36. 'FormElements',
  37. array('TabContainer', array(
  38. 'id' => 'tabContainer',
  39. 'style' => 'width: 600px; height: 500px;',
  40. 'dijitParams' => array(
  41. 'tabPosition' => 'top'
  42. ),
  43. )),
  44. 'DijitForm',
  45. ));
  46. $textForm = new Zend_Dojo_Form_SubForm();
  47. $textForm->setAttribs(array(
  48. 'name' => 'textboxtab',
  49. 'legend' => 'Text Elements',
  50. 'dijitParams' => array(
  51. 'title' => 'Text Elements',
  52. ),
  53. ));
  54. $textForm->addElement(
  55. 'TextBox',
  56. 'textbox',
  57. array(
  58. 'value' => 'some text',
  59. 'label' => 'TextBox',
  60. 'trim' => true,
  61. 'propercase' => true,
  62. )
  63. )
  64. ->addElement(
  65. 'DateTextBox',
  66. 'datebox',
  67. array(
  68. 'value' => '2008-07-05',
  69. 'label' => 'DateTextBox',
  70. 'required' => true,
  71. )
  72. )
  73. ->addElement(
  74. 'TimeTextBox',
  75. 'timebox',
  76. array(
  77. 'label' => 'TimeTextBox',
  78. 'required' => true,
  79. )
  80. )
  81. ->addElement(
  82. 'CurrencyTextBox',
  83. 'currencybox',
  84. array(
  85. 'label' => 'CurrencyTextBox',
  86. 'required' => true,
  87. // 'currency' => 'USD',
  88. 'invalidMessage' => 'Invalid amount. Include dollar sign, commas, and cents.',
  89. // 'fractional' => true,
  90. // 'symbol' => 'USD',
  91. // 'type' => 'currency',
  92. )
  93. )
  94. ->addElement(
  95. 'NumberTextBox',
  96. 'numberbox',
  97. array(
  98. 'label' => 'NumberTextBox',
  99. 'required' => true,
  100. 'invalidMessage' => 'Invalid elevation.',
  101. 'constraints' => array(
  102. 'min' => -20000,
  103. 'max' => 20000,
  104. 'places' => 0,
  105. )
  106. )
  107. )
  108. ->addElement(
  109. 'ValidationTextBox',
  110. 'validationbox',
  111. array(
  112. 'label' => 'ValidationTextBox',
  113. 'required' => true,
  114. 'regExp' => '[\w]+',
  115. 'invalidMessage' => 'Invalid non-space text.',
  116. )
  117. )
  118. ->addElement(
  119. 'Textarea',
  120. 'textarea',
  121. array(
  122. 'label' => 'Textarea',
  123. 'required' => true,
  124. 'style' => 'width: 200px;',
  125. )
  126. );
  127. $toggleForm = new Zend_Dojo_Form_SubForm();
  128. $toggleForm->setAttribs(array(
  129. 'name' => 'toggletab',
  130. 'legend' => 'Toggle Elements',
  131. ));
  132. $toggleForm->addElement(
  133. 'NumberSpinner',
  134. 'ns',
  135. array(
  136. 'value' => '7',
  137. 'label' => 'NumberSpinner',
  138. 'smallDelta' => 5,
  139. 'largeDelta' => 25,
  140. 'defaultTimeout' => 1000,
  141. 'timeoutChangeRate' => 100,
  142. 'min' => 9,
  143. 'max' => 1550,
  144. 'places' => 0,
  145. 'maxlength' => 20,
  146. )
  147. )
  148. ->addElement(
  149. 'Button',
  150. 'dijitButton',
  151. array(
  152. 'label' => 'Button',
  153. )
  154. )
  155. ->addElement(
  156. 'CheckBox',
  157. 'checkbox',
  158. array(
  159. 'label' => 'CheckBox',
  160. 'checkedValue' => 'foo',
  161. 'uncheckedValue' => 'bar',
  162. 'checked' => true,
  163. )
  164. )
  165. ->addElement(
  166. 'RadioButton',
  167. 'radiobutton',
  168. array(
  169. 'label' => 'RadioButton',
  170. 'multiOptions' => array(
  171. 'foo' => 'Foo',
  172. 'bar' => 'Bar',
  173. 'baz' => 'Baz',
  174. ),
  175. 'value' => 'bar',
  176. )
  177. );
  178. $selectForm = new Zend_Dojo_Form_SubForm();
  179. $selectForm->setAttribs(array(
  180. 'name' => 'selecttab',
  181. 'legend' => 'Select Elements',
  182. ));
  183. $selectForm->addElement(
  184. 'ComboBox',
  185. 'comboboxselect',
  186. array(
  187. 'label' => 'ComboBox (select)',
  188. 'value' => 'blue',
  189. 'autocomplete' => false,
  190. 'multiOptions' => $this->_selectOptions,
  191. )
  192. )
  193. ->addElement(
  194. 'ComboBox',
  195. 'comboboxremote',
  196. array(
  197. 'label' => 'ComboBox (remoter)',
  198. 'storeId' => 'stateStore',
  199. 'storeType' => 'dojo.data.ItemFileReadStore',
  200. 'storeParams' => array(
  201. 'url' => '/js/states.txt',
  202. ),
  203. 'dijitParams' => array(
  204. 'searchAttr' => 'name',
  205. ),
  206. )
  207. )
  208. ->addElement(
  209. 'FilteringSelect',
  210. 'filterselect',
  211. array(
  212. 'label' => 'FilteringSelect (select)',
  213. 'value' => 'blue',
  214. 'autocomplete' => false,
  215. 'multiOptions' => $this->_selectOptions,
  216. )
  217. )
  218. ->addElement(
  219. 'FilteringSelect',
  220. 'filterselectremote',
  221. array(
  222. 'label' => 'FilteringSelect (remoter)',
  223. 'storeId' => 'stateStore',
  224. 'storeType' => 'dojo.data.ItemFileReadStore',
  225. 'storeParams' => array(
  226. 'url' => '/js/states.txt',
  227. ),
  228. 'dijitParams' => array(
  229. 'searchAttr' => 'name',
  230. ),
  231. )
  232. );
  233. $sliderForm = new Zend_Dojo_Form_SubForm();
  234. $sliderForm->setAttribs(array(
  235. 'name' => 'slidertab',
  236. 'legend' => 'Slider Elements',
  237. ));
  238. $sliderForm->addElement(
  239. 'HorizontalSlider',
  240. 'horizontal',
  241. array(
  242. 'label' => 'HorizontalSlider',
  243. 'value' => 5,
  244. 'minimum' => -10,
  245. 'maximum' => 10,
  246. 'discreteValues' => 11,
  247. 'intermediateChanges' => true,
  248. 'showButtons' => true,
  249. 'topDecorationDijit' => 'HorizontalRuleLabels',
  250. 'topDecorationContainer' => 'topContainer',
  251. 'topDecorationLabels' => array(
  252. ' ',
  253. '20%',
  254. '40%',
  255. '60%',
  256. '80%',
  257. ' ',
  258. ),
  259. 'topDecorationParams' => array(
  260. 'container' => array(
  261. 'style' => 'height:1.2em; font-size=75%;color:gray;',
  262. ),
  263. 'list' => array(
  264. 'style' => 'height:1em; font-size=75%;color:gray;',
  265. ),
  266. ),
  267. 'bottomDecorationDijit' => 'HorizontalRule',
  268. 'bottomDecorationContainer' => 'bottomContainer',
  269. 'bottomDecorationLabels' => array(
  270. '0%',
  271. '50%',
  272. '100%',
  273. ),
  274. 'bottomDecorationParams' => array(
  275. 'list' => array(
  276. 'style' => 'height:1em; font-size=75%;color:gray;',
  277. ),
  278. ),
  279. )
  280. )
  281. ->addElement(
  282. 'VerticalSlider',
  283. 'vertical',
  284. array(
  285. 'label' => 'VerticalSlider',
  286. 'value' => 5,
  287. 'style' => 'height: 200px; width: 3em;',
  288. 'minimum' => -10,
  289. 'maximum' => 10,
  290. 'discreteValues' => 11,
  291. 'intermediateChanges' => true,
  292. 'showButtons' => true,
  293. 'leftDecorationDijit' => 'VerticalRuleLabels',
  294. 'leftDecorationContainer' => 'leftContainer',
  295. 'leftDecorationLabels' => array(
  296. ' ',
  297. '20%',
  298. '40%',
  299. '60%',
  300. '80%',
  301. ' ',
  302. ),
  303. 'rightDecorationDijit' => 'VerticalRule',
  304. 'rightDecorationContainer' => 'rightContainer',
  305. 'rightDecorationLabels' => array(
  306. '0%',
  307. '50%',
  308. '100%',
  309. ),
  310. )
  311. );
  312. $this->addSubForm($textForm, 'textboxtab')
  313. ->addSubForm($toggleForm, 'toggletab')
  314. ->addSubForm($selectForm, 'selecttab')
  315. ->addSubForm($sliderForm, 'slidertab');
  316. }
  317. }
  318. ]]></programlisting>
  319. </example>
  320. <example id="zend.dojo.form.examples.decorating">
  321. <title> 修改已存在的表单来使用 Dojo </title>
  322. <para>
  323. 通过使用 <code>Zend_Dojo::enableForm()</code> 静态方法,已存在的表单也可以使用 Dojo。
  324. </para>
  325. <para>
  326. 第一个范例示范装饰已存在的表单实例:
  327. </para>
  328. <programlisting role="php"><![CDATA[
  329. $form = new My_Custom_Form();
  330. Zend_Dojo::enableForm($form);
  331. $form->addElement(
  332. 'ComboBox',
  333. 'query',
  334. array(
  335. 'label' => 'Color:',
  336. 'value' => 'blue',
  337. 'autocomplete' => false,
  338. 'multiOptions' => array(
  339. 'red' => 'Rouge',
  340. 'blue' => 'Bleu',
  341. 'white' => 'Blanc',
  342. 'orange' => 'Orange',
  343. 'black' => 'Noir',
  344. 'green' => 'Vert',
  345. ),
  346. )
  347. );
  348. ]]></programlisting>
  349. <para>
  350. 另外,你可以在表单初始化上做点小文章:
  351. </para>
  352. <programlisting role="php"><![CDATA[
  353. class My_Custom_Form extends Zend_Form
  354. {
  355. public function init()
  356. {
  357. Zend_Dojo::enableForm($this);
  358. // ...
  359. }
  360. }
  361. ]]></programlisting>
  362. <para>
  363. 当然,如果你可以那样做 ... 你可以并应该简单地修改类来继承 Zend_Dojo_Form,
  364. 它可以随时替换已经 Dojo-enabled 的 Zend_Form ...
  365. </para>
  366. </example>
  367. </sect2>
  368. <!--
  369. vim:se ts=4 sw=4 et:
  370. -->