ZendX_JQuery-Form.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zendx.jquery.form">
  4. <title>ZendX_JQuery Form Elements and Decorators</title>
  5. <para>All View Helpers are pressed into Zend_Form elements or decorators also. They can even be
  6. easily integrated into your already existing forms. To enable a Form for Zend_JQuery support
  7. you can use two ways: Init your form as <code>$form = new ZendX_JQuery_Form();</code> or
  8. use the static method <methodname>ZendX_JQuery::enableForm($form)</methodname> to enable jQuery element support.
  9. </para>
  10. <sect2 id="zendx.jquery.form.usage">
  11. <title>General Elements and Decorator Usage</title>
  12. <para>Both elements and decorators of the Zend jQuery Form set can be initialized with
  13. the option key <code>jQueryParams</code> to set certain jQuery object related parameters.
  14. This jQueryParams array of options matches to the <varname>$params</varname> variable
  15. of the corresponding view helpers. For example:</para>
  16. <programlisting language="php"><![CDATA[
  17. $element = new ZendX_JQuery_Form_Element_DatePicker(
  18. 'dp1',
  19. array('jQueryParams' => array('defaultDate' => '2007/10/10'))
  20. );
  21. // would internally call to:
  22. $view->datePicker("dp1", "", array('defaultDate' => '2007/10/10'), array());
  23. ]]></programlisting>
  24. <para>Additionally elements jQuery options can be customized by the following methods:</para>
  25. <itemizedlist>
  26. <listitem><para><methodname>setJQueryParam($name, $value)</methodname>: Set the jQuery option <varname>$name</varname> to
  27. the given value.</para></listitem>
  28. <listitem><para><methodname>setJQueryParams($params)</methodname>: Set key value pairs of jQuery options and merge
  29. them with the already set options.</para></listitem>
  30. <listitem><para><methodname>getJQueryParam($name)</methodname>: Return the jQuery option with the given name.</para></listitem>
  31. <listitem><para><methodname>getJQueryParams()</methodname>: Return an array of all currently set jQuery options.</para></listitem>
  32. </itemizedlist>
  33. <para>Each jQuery related Decorator also owns a <methodname>getJQueryParams()</methodname> method, to set options you have to
  34. use the <methodname>setDecorators()</methodname>, <methodname>addDecorator()</methodname> or <methodname>addDecorators()</methodname> functionality
  35. of a form element and set the jQueryParams key as option:</para>
  36. <programlisting language="php"><![CDATA[
  37. $form->setDecorators(array(
  38. 'FormElements',
  39. array('AccordionContainer', array(
  40. 'id' => 'tabContainer',
  41. 'style' => 'width: 600px;',
  42. 'jQueryParams' => array(
  43. 'alwaysOpen' => false,
  44. 'animated' => "easeslide"
  45. ),
  46. )),
  47. 'Form'
  48. ));
  49. ]]></programlisting>
  50. </sect2>
  51. <sect2 id="zendx.jquery.form.elements">
  52. <title>Form Elements</title>
  53. <para>The Zend Framework jQuery Extras Extension comes with the following Form Elements:</para>
  54. <itemizedlist>
  55. <listitem><para><code>ZendX_JQuery_Form_Element_AutoComplete</code>: Proxy to AutoComplete View Helper</para></listitem>
  56. <listitem><para><code>ZendX_JQuery_Form_Element_ColorPicker</code>: Proxy to ColorPicker View Helper</para></listitem>
  57. <listitem><para><code>ZendX_JQuery_Form_Element_DatePicker</code>: Proxy to DatePicker View Helper</para></listitem>
  58. <listitem><para><code>ZendX_JQuery_Form_Element_Slider</code>: Proxy to Slider View Helper</para></listitem>
  59. <listitem><para><code>ZendX_JQuery_Form_Element_Spinner</code>: Proxy to Spinner View Helper</para></listitem>
  60. </itemizedlist>
  61. <note id="zendx.jquery.form.elements.markerinterface">
  62. <title>jQuery Decorators: Beware the Marker Interface for UiWidgetElements</title>
  63. <para>By default all the jQuery Form elements use the <code>ZendX_JQuery_Form_Decorator_UiWidgetElement</code> decorator
  64. for rendering the jQuery element with its specific view helper. This decorator is inheritly different
  65. from the ViewHelper decorator that is used for most of the default form elements in Zend_Form.
  66. To ensure that rendering works correctly for jQuery form elements at least one decorator has to
  67. implement the <code>ZendX_JQuery_Form_Decorator_UiWidgetElementMarker</code> interface, which
  68. the default decorator does. If no marker interface is found an exception is thrown. Use the marker
  69. interface if you want to implement your own decorator for the jQuery form element specific rendering.
  70. </para>
  71. </note>
  72. </sect2>
  73. <sect2 id="zendx.jquery.form.decorators">
  74. <title>Form Decorators</title>
  75. <para>The following Decorators come with the Zend Framework jQuery Extension:</para>
  76. <itemizedlist>
  77. <listitem><para><code>ZendX_JQuery_Form_Decorator_AccordionContainer</code>: Proxy to AccordionContainer View Helper</para></listitem>
  78. <listitem><para><code>ZendX_JQuery_Form_Decorator_AccordionPane</code>: Proxy to AccordionPane View Helper</para></listitem>
  79. <listitem><para><code>ZendX_JQuery_Form_Decorator_DialogContainer</code>: Proxy to DialogContainer View Helper</para></listitem>
  80. <listitem><para><code>ZendX_JQuery_Form_Decorator_TabContainer</code>: Proxy to TabContainer View Helper</para></listitem>
  81. <listitem><para><code>ZendX_JQuery_Form_Decorator_TabPane</code>: Proxy to TabPane View Helper</para></listitem>
  82. <listitem><para><code>ZendX_JQuery_Form_Decorator_UiWidgetElement</code>: Decorator to Display jQuery Form Elements</para></listitem>
  83. </itemizedlist>
  84. <para>Utilizing the Container elements is a bit more complicated, the following example builds a Form with 2 SubForms in a TabContainer:</para>
  85. <example id="zendx.jquery.form.decorators.tabexample">
  86. <title>SubForms with TabContainer Decorator</title>
  87. <para>The following example makes use of all Form elements and wraps them into 2 subforms that are decorated with a tab container. First
  88. we build the basic <code>ZendX_JQuery_Form</code>:</para>
  89. <programlisting language="php"><![CDATA[
  90. $form = new ZendX_JQuery_Form();
  91. $form->setAction('formdemo.php');
  92. $form->setAttrib('id', 'mainForm');
  93. $form->setAttrib('class', 'flora');
  94. $form->setDecorators(array(
  95. 'FormElements',
  96. array('TabContainer', array(
  97. 'id' => 'tabContainer',
  98. 'style' => 'width: 600px;',
  99. )),
  100. 'Form',
  101. ));
  102. ]]></programlisting>
  103. <para>Setting the Form Id (in this case to 'mainForm') is an important step for the TabContainer. It is needed that the subforms can relate
  104. to the Container Id in a later form building stage. We now initialize two SubForms that will be decorated with the <code>TabPane</code>
  105. decorators:</para>
  106. <programlisting language="php"><![CDATA[
  107. $subForm1 = new ZendX_JQuery_Form();
  108. $subForm1->setDecorators(array(
  109. 'FormElements',
  110. array('HtmlTag',
  111. array('tag' => 'dl')),
  112. array('TabPane',
  113. array('jQueryParams' => array('containerId' => 'mainForm',
  114. 'title' => 'DatePicker and Slider')))
  115. ));
  116. $subForm2 = new ZendX_JQuery_Form();
  117. $subForm2->setDecorators(array(
  118. 'FormElements',
  119. array('HtmlTag',
  120. array('tag' => 'dl')),
  121. array('TabPane',
  122. array('jQueryParams' => array('containerId' => 'mainForm',
  123. 'title' => 'AutoComplete and Spinner')))
  124. ));
  125. ]]></programlisting>
  126. <para>In this stage it is important that the <code>'containerId'</code> option is set in each SubForm TabPane, or the subforms
  127. cannot relate back to the tab Container and would not be displayed. To enforce this setting, an exception of the type <code>ZendX_JQuery_Exception</code>
  128. is thrown if the option is not set. We can now add all the desired elements to the subforms:</para>
  129. <programlisting language="php"><![CDATA[
  130. // Add Element Date Picker
  131. $elem = new ZendX_JQuery_Form_Element_DatePicker(
  132. "datePicker1", array("label" => "Date Picker:")
  133. );
  134. $elem->setJQueryParam('dateFormat', 'dd.mm.yy');
  135. $subForm1->addElement($elem);
  136. // Add Element Spinner
  137. $elem = new ZendX_JQuery_Form_Element_Spinner(
  138. "spinner1", array('label' => 'Spinner:')
  139. );
  140. $elem->setJQueryParams(array('min' => 0, 'max' => 1000, 'start' => 100));
  141. $subForm1->addElement($elem);
  142. // Add Slider Element
  143. $elem = new ZendX_JQuery_Form_Element_Slider(
  144. "slider1", array('label' => 'Slider:')
  145. );
  146. $elem->setJQueryParams(array('defaultValue' => '75'));
  147. $subForm2->addElement($elem);
  148. // Add Autocomplete Element
  149. $elem = new ZendX_JQuery_Form_Element_AutoComplete(
  150. "ac1", array('label' => 'Autocomplete:')
  151. );
  152. $elem->setJQueryParams(array('source' => array('New York',
  153. 'Berlin',
  154. 'Bern',
  155. 'Boston')));
  156. $subForm2->addElement($elem);
  157. // Submit Button
  158. $elem = new Zend_Form_Element_Submit("btn1", array('value' => 'Submit'));
  159. $subForm1->addElement($elem);
  160. ]]></programlisting>
  161. <para>Three additional lines are missing to put it all together and we have a jQuery animated form:</para>
  162. <programlisting language="php"><![CDATA[
  163. $form->addSubForm($subForm1, 'subform1');
  164. $form->addSubForm($subForm2, 'subform2');
  165. $formString = $form->render($view);
  166. ]]></programlisting>
  167. </example>
  168. <example id="zendx.jquery.form.decorators.dialogexample">
  169. <title>Wrapping a Form into the Dialog Container</title>
  170. <para>The only use for the Dialog Container in Zend Form context is to wrap itself around a form and
  171. display it in a dialog. Its important to remember that the order of the decorators has to be different than in the Accordion and
  172. Tab Container examples.</para>
  173. <programlisting language="php"><![CDATA[
  174. // Create new jQuery Form
  175. $form = new ZendX_JQuery_Form();
  176. $form->setAction('formdemo.php');
  177. // Wrap the complete form inside a Dialog box
  178. $form->setDecorators(array(
  179. 'FormElements',
  180. 'Form',
  181. array('DialogContainer', array(
  182. 'id' => 'tabContainer',
  183. 'style' => 'width: 600px;',
  184. 'jQueryParams' => array(
  185. 'tabPosition' => 'top'
  186. ),
  187. )),
  188. ));
  189. // Add Element Spinner
  190. $elem = new ZendX_JQuery_Form_Element_Spinner("spinner1", array('label' => 'Spinner:', 'attribs' => array('class' => 'flora')));
  191. $elem->setJQueryParams(array('min' => 0, 'max' => 1000, 'start' => 100));
  192. $form->addElement($elem);
  193. ]]></programlisting>
  194. </example>
  195. </sect2>
  196. </sect1>