Zend_Dojo-Form-Examples.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.dojo.form.examples">
  4. <title>Dojo Form Examples</title>
  5. <example id="zend.dojo.form.examples.dojoform">
  6. <title>Using Zend_Dojo_Form</title>
  7. <para>
  8. The easiest way to utilize Dojo with <classname>Zend_Form</classname> is to
  9. utilize <classname>Zend_Dojo_Form</classname>, either through direct usage or
  10. by extending it. This example shows extending <classname>Zend_Dojo_Form</classname>, and
  11. shows usage of all dijit elements. It creates four sub forms, and
  12. decorates the form to utilize a TabContainer, showing each sub form
  13. in its own tab.
  14. </para>
  15. <programlisting language="php"><![CDATA[
  16. class My_Form_Test extends Zend_Dojo_Form
  17. {
  18. /**
  19. * Options to use with select elements
  20. */
  21. protected $_selectOptions = array(
  22. 'red' => 'Rouge',
  23. 'blue' => 'Bleu',
  24. 'white' => 'Blanc',
  25. 'orange' => 'Orange',
  26. 'black' => 'Noir',
  27. 'green' => 'Vert',
  28. );
  29. /**
  30. * Form initialization
  31. *
  32. * @return void
  33. */
  34. public function init()
  35. {
  36. $this->setMethod('post');
  37. $this->setAttribs(array(
  38. 'name' => 'masterForm',
  39. ));
  40. $this->setDecorators(array(
  41. 'FormElements',
  42. array('TabContainer', array(
  43. 'id' => 'tabContainer',
  44. 'style' => 'width: 600px; height: 500px;',
  45. 'dijitParams' => array(
  46. 'tabPosition' => 'top'
  47. ),
  48. )),
  49. 'DijitForm',
  50. ));
  51. $textForm = new Zend_Dojo_Form_SubForm();
  52. $textForm->setAttribs(array(
  53. 'name' => 'textboxtab',
  54. 'legend' => 'Text Elements',
  55. 'dijitParams' => array(
  56. 'title' => 'Text Elements',
  57. ),
  58. ));
  59. $textForm->addElement(
  60. 'TextBox',
  61. 'textbox',
  62. array(
  63. 'value' => 'some text',
  64. 'label' => 'TextBox',
  65. 'trim' => true,
  66. 'propercase' => true,
  67. )
  68. )
  69. ->addElement(
  70. 'DateTextBox',
  71. 'datebox',
  72. array(
  73. 'value' => '2008-07-05',
  74. 'label' => 'DateTextBox',
  75. 'required' => true,
  76. )
  77. )
  78. ->addElement(
  79. 'TimeTextBox',
  80. 'timebox',
  81. array(
  82. 'label' => 'TimeTextBox',
  83. 'required' => true,
  84. )
  85. )
  86. ->addElement(
  87. 'CurrencyTextBox',
  88. 'currencybox',
  89. array(
  90. 'label' => 'CurrencyTextBox',
  91. 'required' => true,
  92. // 'currency' => 'USD',
  93. 'invalidMessage' => 'Invalid amount. ' .
  94. 'Include dollar sign, commas, ' .
  95. 'and cents.',
  96. // 'fractional' => true,
  97. // 'symbol' => 'USD',
  98. // 'type' => 'currency',
  99. )
  100. )
  101. ->addElement(
  102. 'NumberTextBox',
  103. 'numberbox',
  104. array(
  105. 'label' => 'NumberTextBox',
  106. 'required' => true,
  107. 'invalidMessage' => 'Invalid elevation.',
  108. 'constraints' => array(
  109. 'min' => -20000,
  110. 'max' => 20000,
  111. 'places' => 0,
  112. )
  113. )
  114. )
  115. ->addElement(
  116. 'ValidationTextBox',
  117. 'validationbox',
  118. array(
  119. 'label' => 'ValidationTextBox',
  120. 'required' => true,
  121. 'regExp' => '[\w]+',
  122. 'invalidMessage' => 'Invalid non-space text.',
  123. )
  124. )
  125. ->addElement(
  126. 'Textarea',
  127. 'textarea',
  128. array(
  129. 'label' => 'Textarea',
  130. 'required' => true,
  131. 'style' => 'width: 200px;',
  132. )
  133. );
  134. $editorForm = new Zend_Dojo_Form_SubForm();
  135. $editorForm->setAttribs(array(
  136. 'name' => 'editortab',
  137. 'legend' => 'Editor',
  138. 'dijitParams' => array(
  139. 'title' => 'Editor'
  140. ),
  141. ))
  142. $editorForm->addElement(
  143. 'Editor',
  144. 'wysiwyg',
  145. array(
  146. 'label' => 'Editor',
  147. 'inheritWidth' => 'true',
  148. )
  149. );
  150. $toggleForm = new Zend_Dojo_Form_SubForm();
  151. $toggleForm->setAttribs(array(
  152. 'name' => 'toggletab',
  153. 'legend' => 'Toggle Elements',
  154. ));
  155. $toggleForm->addElement(
  156. 'NumberSpinner',
  157. 'ns',
  158. array(
  159. 'value' => '7',
  160. 'label' => 'NumberSpinner',
  161. 'smallDelta' => 5,
  162. 'largeDelta' => 25,
  163. 'defaultTimeout' => 1000,
  164. 'timeoutChangeRate' => 100,
  165. 'min' => 9,
  166. 'max' => 1550,
  167. 'places' => 0,
  168. 'maxlength' => 20,
  169. )
  170. )
  171. ->addElement(
  172. 'Button',
  173. 'dijitButton',
  174. array(
  175. 'label' => 'Button',
  176. )
  177. )
  178. ->addElement(
  179. 'CheckBox',
  180. 'checkbox',
  181. array(
  182. 'label' => 'CheckBox',
  183. 'checkedValue' => 'foo',
  184. 'uncheckedValue' => 'bar',
  185. 'checked' => true,
  186. )
  187. )
  188. ->addElement(
  189. 'RadioButton',
  190. 'radiobutton',
  191. array(
  192. 'label' => 'RadioButton',
  193. 'multiOptions' => array(
  194. 'foo' => 'Foo',
  195. 'bar' => 'Bar',
  196. 'baz' => 'Baz',
  197. ),
  198. 'value' => 'bar',
  199. )
  200. );
  201. $selectForm = new Zend_Dojo_Form_SubForm();
  202. $selectForm->setAttribs(array(
  203. 'name' => 'selecttab',
  204. 'legend' => 'Select Elements',
  205. ));
  206. $selectForm->addElement(
  207. 'ComboBox',
  208. 'comboboxselect',
  209. array(
  210. 'label' => 'ComboBox (select)',
  211. 'value' => 'blue',
  212. 'autocomplete' => false,
  213. 'multiOptions' => $this->_selectOptions,
  214. )
  215. )
  216. ->addElement(
  217. 'ComboBox',
  218. 'comboboxremote',
  219. array(
  220. 'label' => 'ComboBox (remoter)',
  221. 'storeId' => 'stateStore',
  222. 'storeType' => 'dojo.data.ItemFileReadStore',
  223. 'storeParams' => array(
  224. 'url' => '/js/states.txt',
  225. ),
  226. 'dijitParams' => array(
  227. 'searchAttr' => 'name',
  228. ),
  229. )
  230. )
  231. ->addElement(
  232. 'FilteringSelect',
  233. 'filterselect',
  234. array(
  235. 'label' => 'FilteringSelect (select)',
  236. 'value' => 'blue',
  237. 'autocomplete' => false,
  238. 'multiOptions' => $this->_selectOptions,
  239. )
  240. )
  241. ->addElement(
  242. 'FilteringSelect',
  243. 'filterselectremote',
  244. array(
  245. 'label' => 'FilteringSelect (remoter)',
  246. 'storeId' => 'stateStore',
  247. 'storeType' => 'dojo.data.ItemFileReadStore',
  248. 'storeParams' => array(
  249. 'url' => '/js/states.txt',
  250. ),
  251. 'dijitParams' => array(
  252. 'searchAttr' => 'name',
  253. ),
  254. )
  255. );
  256. $sliderForm = new Zend_Dojo_Form_SubForm();
  257. $sliderForm->setAttribs(array(
  258. 'name' => 'slidertab',
  259. 'legend' => 'Slider Elements',
  260. ));
  261. $sliderForm->addElement(
  262. 'HorizontalSlider',
  263. 'horizontal',
  264. array(
  265. 'label' => 'HorizontalSlider',
  266. 'value' => 5,
  267. 'minimum' => -10,
  268. 'maximum' => 10,
  269. 'discreteValues' => 11,
  270. 'intermediateChanges' => true,
  271. 'showButtons' => true,
  272. 'topDecorationDijit' => 'HorizontalRuleLabels',
  273. 'topDecorationContainer' => 'topContainer',
  274. 'topDecorationLabels' => array(
  275. ' ',
  276. '20%',
  277. '40%',
  278. '60%',
  279. '80%',
  280. ' ',
  281. ),
  282. 'topDecorationParams' => array(
  283. 'container' => array(
  284. 'style' => 'height:1.2em; ' .
  285. 'font-size=75%;color:gray;',
  286. ),
  287. 'list' => array(
  288. 'style' => 'height:1em; ' .
  289. 'font-size=75%;color:gray;',
  290. ),
  291. ),
  292. 'bottomDecorationDijit' => 'HorizontalRule',
  293. 'bottomDecorationContainer' => 'bottomContainer',
  294. 'bottomDecorationLabels' => array(
  295. '0%',
  296. '50%',
  297. '100%',
  298. ),
  299. 'bottomDecorationParams' => array(
  300. 'list' => array(
  301. 'style' => 'height:1em; ' .
  302. 'font-size=75%;color:gray;',
  303. ),
  304. ),
  305. )
  306. )
  307. ->addElement(
  308. 'VerticalSlider',
  309. 'vertical',
  310. array(
  311. 'label' => 'VerticalSlider',
  312. 'value' => 5,
  313. 'style' => 'height: 200px; width: 3em;',
  314. 'minimum' => -10,
  315. 'maximum' => 10,
  316. 'discreteValues' => 11,
  317. 'intermediateChanges' => true,
  318. 'showButtons' => true,
  319. 'leftDecorationDijit' => 'VerticalRuleLabels',
  320. 'leftDecorationContainer' => 'leftContainer',
  321. 'leftDecorationLabels' => array(
  322. ' ',
  323. '20%',
  324. '40%',
  325. '60%',
  326. '80%',
  327. ' ',
  328. ),
  329. 'rightDecorationDijit' => 'VerticalRule',
  330. 'rightDecorationContainer' => 'rightContainer',
  331. 'rightDecorationLabels' => array(
  332. '0%',
  333. '50%',
  334. '100%',
  335. ),
  336. )
  337. );
  338. $this->addSubForm($textForm, 'textboxtab')
  339. ->addSubForm($editorForm, 'editortab')
  340. ->addSubForm($toggleForm, 'toggletab')
  341. ->addSubForm($selectForm, 'selecttab')
  342. ->addSubForm($sliderForm, 'slidertab');
  343. }
  344. }
  345. ]]></programlisting>
  346. </example>
  347. <example id="zend.dojo.form.examples.decorating">
  348. <title>Modifying an existing form to utilize Dojo</title>
  349. <para>
  350. Existing forms can be modified to utilize Dojo as well, by use of
  351. the <methodname>Zend_Dojo::enableForm()</methodname> static method.
  352. </para>
  353. <para>
  354. This first example shows decorating an existing form instance:
  355. </para>
  356. <programlisting language="php"><![CDATA[
  357. $form = new My_Custom_Form();
  358. Zend_Dojo::enableForm($form);
  359. $form->addElement(
  360. 'ComboBox',
  361. 'query',
  362. array(
  363. 'label' => 'Color:',
  364. 'value' => 'blue',
  365. 'autocomplete' => false,
  366. 'multiOptions' => array(
  367. 'red' => 'Rouge',
  368. 'blue' => 'Bleu',
  369. 'white' => 'Blanc',
  370. 'orange' => 'Orange',
  371. 'black' => 'Noir',
  372. 'green' => 'Vert',
  373. ),
  374. )
  375. );
  376. ]]></programlisting>
  377. <para>
  378. Alternately, you can make a slight tweak to your form
  379. initialization:
  380. </para>
  381. <programlisting language="php"><![CDATA[
  382. class My_Custom_Form extends Zend_Form
  383. {
  384. public function init()
  385. {
  386. Zend_Dojo::enableForm($this);
  387. // ...
  388. }
  389. }
  390. ]]></programlisting>
  391. <para>
  392. Of course, if you can do that... you could and should simply alter
  393. the class to inherit from <classname>Zend_Dojo_Form</classname>, which is a drop-in
  394. replacement of <classname>Zend_Form</classname> that's already Dojo-enabled...
  395. </para>
  396. </example>
  397. </sect2>
  398. <!--
  399. vim:se ts=4 sw=4 et:
  400. -->