Zend_Form-Forms.xml 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.form.forms">
  4. <title>Creating Forms Using Zend_Form</title>
  5. <para>
  6. The <classname>Zend_Form</classname> class is used to aggregate form elements,
  7. display groups, and subforms. It can then perform the following actions
  8. on those items:
  9. </para>
  10. <itemizedlist>
  11. <listitem>
  12. <para>
  13. Validation, including retrieving error codes and messages
  14. </para>
  15. </listitem>
  16. <listitem>
  17. <para>
  18. Value aggregation, including populating items and retrieving both
  19. filtered and unfiltered values from all items
  20. </para>
  21. </listitem>
  22. <listitem>
  23. <para>
  24. Iteration over all items, in the order in which they are entered or
  25. based on the order hints retrieved from each item
  26. </para>
  27. </listitem>
  28. <listitem>
  29. <para>
  30. Rendering of the entire form, either via a single decorator that
  31. performs custom rendering or by iterating over each item in the form
  32. </para>
  33. </listitem>
  34. </itemizedlist>
  35. <para>
  36. While forms created with <classname>Zend_Form</classname> may be complex, probably
  37. the best use case is for simple forms; its best use is for Rapid
  38. Application Development (RAD) and prototyping.
  39. </para>
  40. <para>
  41. At its most basic, you simply instantiate a form object:
  42. </para>
  43. <programlisting language="php"><![CDATA[
  44. // Generic form object:
  45. $form = new Zend_Form();
  46. // Custom form object:
  47. $form = new My_Form()
  48. ]]></programlisting>
  49. <para>
  50. You can optionally pass in a instance of <classname>Zend_Config</classname> or an array,
  51. which will be used to set object state and potentially create new elements:
  52. </para>
  53. <programlisting language="php"><![CDATA[
  54. // Passing in configuration options:
  55. $form = new Zend_Form($config);
  56. ]]></programlisting>
  57. <para>
  58. <classname>Zend_Form</classname> is iterable, and will iterate over elements,
  59. display groups, and subforms, using the order they were registered and
  60. any order index each may have. This is useful in cases where you wish to
  61. render the elements manually in the appropriate order.
  62. </para>
  63. <para>
  64. <classname>Zend_Form</classname>'s magic lies in its ability to serve as a factory
  65. for elements and display groups, as well as the ability to render itself
  66. through decorators.
  67. </para>
  68. <sect2 id="zend.form.forms.plugins">
  69. <title>Plugin Loaders</title>
  70. <para>
  71. <classname>Zend_Form</classname> makes use of
  72. <classname>Zend_Loader_PluginLoader</classname> to allow developers to specify
  73. the locations of alternate elements and decorators. Each has its own
  74. plugin loader associated with it, and general accessors are used to
  75. retrieve and modify each.
  76. </para>
  77. <para>
  78. The following loader types are used with the various plugin loader
  79. methods: 'element' and 'decorator'. The type names are case
  80. insensitive.
  81. </para>
  82. <para>
  83. The methods used to interact with plugin loaders are as follows:
  84. </para>
  85. <itemizedlist>
  86. <listitem>
  87. <para>
  88. <methodname>setPluginLoader($loader, $type)</methodname>: $loader is the
  89. plugin loader object itself, while type is one of the types
  90. specified above. This sets the plugin loader for the given type
  91. to the newly specified loader object.
  92. </para>
  93. </listitem>
  94. <listitem>
  95. <para>
  96. <methodname>getPluginLoader($type)</methodname>: retrieves the plugin loader
  97. associated with $type.
  98. </para>
  99. </listitem>
  100. <listitem>
  101. <para>
  102. <methodname>addPrefixPath($prefix, $path, $type = null)</methodname>: adds a
  103. prefix/path association to the loader specified by $type. If
  104. $type is <constant>NULL</constant>, it will attempt to add the path to all
  105. loaders, by appending the prefix with each of "_Element" and
  106. "_Decorator"; and appending the path with "Element/" and
  107. "Decorator/". If you have all your extra form element classes
  108. under a common hierarchy, this is a convenience method for
  109. setting the base prefix for them.
  110. </para>
  111. </listitem>
  112. <listitem>
  113. <para>
  114. <methodname>addPrefixPaths(array $spec)</methodname>: allows you to add many
  115. paths at once to one or more plugin loaders. It expects each
  116. array item to be an array with the keys 'path', 'prefix', and
  117. 'type'.
  118. </para>
  119. </listitem>
  120. </itemizedlist>
  121. <para>
  122. Additionally, you can specify prefix paths for all elements and
  123. display groups created through a <classname>Zend_Form</classname> instance
  124. using the following methods:
  125. </para>
  126. <itemizedlist>
  127. <listitem>
  128. <para>
  129. <methodname>addElementPrefixPath($prefix, $path, $type = null)</methodname>:
  130. Just like <methodname>addPrefixPath()</methodname>, you must specify a class
  131. prefix and a path. <varname>$type</varname>, when specified, must be
  132. one of the plugin loader types specified by
  133. <classname>Zend_Form_Element</classname>; see the <link
  134. linkend="zend.form.elements.loaders">element plugins
  135. section</link> for more information on valid
  136. <varname>$type</varname> values. If no <varname>$type</varname> is
  137. specified, the method will assume it is a general prefix for all
  138. types.
  139. </para>
  140. </listitem>
  141. <listitem>
  142. <para>
  143. <methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname>:
  144. Just like <methodname>addPrefixPath()</methodname>, you must specify a class
  145. prefix and a path; however, since display groups only support
  146. decorators as plugins, no <varname>$type</varname> is necessary.
  147. </para>
  148. </listitem>
  149. </itemizedlist>
  150. <para>
  151. Custom elements and decorators are an easy way to share
  152. functionality between forms and encapsulate custom functionality.
  153. See the <link
  154. linkend="zend.form.elements.loaders.customLabel">Custom Label
  155. example</link> in the elements documentation for an example of
  156. how custom elements can be used as replacements for standard
  157. classes.
  158. </para>
  159. </sect2>
  160. <sect2 id="zend.form.forms.elements">
  161. <title>Elements</title>
  162. <para>
  163. <classname>Zend_Form</classname> provides several accessors for adding and
  164. removing form elements from a form. These can take element object
  165. instances or serve as factories for instantiating the element
  166. objects themselves.
  167. </para>
  168. <para>
  169. The most basic method for adding an element is
  170. <methodname>addElement()</methodname>. This method can take either an object of
  171. type <classname>Zend_Form_Element</classname> (or of a class extending
  172. <classname>Zend_Form_Element</classname>), or arguments for building a new
  173. element -- including the element type, name, and any configuration
  174. options.
  175. </para>
  176. <para>
  177. Some examples:
  178. </para>
  179. <programlisting language="php"><![CDATA[
  180. // Using an element instance:
  181. $element = new Zend_Form_Element_Text('foo');
  182. $form->addElement($element);
  183. // Using a factory
  184. //
  185. // Creates an element of type Zend_Form_Element_Text with the
  186. // name of 'foo':
  187. $form->addElement('text', 'foo');
  188. // Pass label option to the element:
  189. $form->addElement('text', 'foo', array('label' => 'Foo:'));
  190. ]]></programlisting>
  191. <note>
  192. <title>addElement() Implements Fluent Interface</title>
  193. <para>
  194. <methodname>addElement()</methodname> implements a fluent interface; that is
  195. to say, it returns the <classname>Zend_Form</classname> object, and not
  196. the element. This is done to allow you to chain together
  197. multiple addElement() methods or other form methods that
  198. implement the fluent interface (all setters in <classname>Zend_Form</classname>
  199. implement the pattern).
  200. </para>
  201. <para>
  202. If you wish to return the element instead, use
  203. <methodname>createElement()</methodname>, which is outlined below. Be aware,
  204. however, that <methodname>createElement()</methodname> does not attach the
  205. element to the form.
  206. </para>
  207. <para>
  208. Internally, <methodname>addElement()</methodname> actually uses
  209. <methodname>createElement()</methodname> to create the element before
  210. attaching it to the form.
  211. </para>
  212. </note>
  213. <para>
  214. Once an element has been added to the form, you can retrieve it by
  215. name. This can be done either by using the <methodname>getElement()</methodname>
  216. method or by using overloading to access the element as an object
  217. property:
  218. </para>
  219. <programlisting language="php"><![CDATA[
  220. // getElement():
  221. $foo = $form->getElement('foo');
  222. // As object property:
  223. $foo = $form->foo;
  224. ]]></programlisting>
  225. <para>
  226. Occasionally, you may want to create an element without attaching it
  227. to the form (for instance, if you wish to make use of the various
  228. plugin paths registered with the form, but wish to later attach the
  229. object to a sub form). The <methodname>createElement()</methodname> method
  230. allows you to do so:
  231. </para>
  232. <programlisting language="php"><![CDATA[
  233. // $username becomes a Zend_Form_Element_Text object:
  234. $username = $form->createElement('text', 'username');
  235. ]]></programlisting>
  236. <sect3 id="zend.form.forms.elements.values">
  237. <title>Populating and Retrieving Values</title>
  238. <para>
  239. After validating a form, you will typically need to retrieve the
  240. values so you can perform other operations, such as updating a
  241. database or notifying a web service. You can retrieve all values
  242. for all elements using <methodname>getValues()</methodname>;
  243. <methodname>getValue($name)</methodname> allows you to retrieve a single
  244. element's value by element name:
  245. </para>
  246. <programlisting language="php"><![CDATA[
  247. // Get all values:
  248. $values = $form->getValues();
  249. // Get only 'foo' element's value:
  250. $value = $form->getValue('foo');
  251. ]]></programlisting>
  252. <para>
  253. Sometimes you'll want to populate the form with specified values
  254. prior to rendering. This can be done with either the
  255. <methodname>setDefaults()</methodname> or <methodname>populate()</methodname>
  256. methods:
  257. </para>
  258. <programlisting language="php"><![CDATA[
  259. $form->setDefaults($data);
  260. $form->populate($data);
  261. ]]></programlisting>
  262. <para>
  263. On the flip side, you may want to clear a form after populating
  264. or validating it; this can be done using the
  265. <methodname>reset()</methodname> method:
  266. </para>
  267. <programlisting language="php"><![CDATA[
  268. $form->reset();
  269. ]]></programlisting>
  270. </sect3>
  271. <sect3 id="zend.form.forms.elements.global">
  272. <title>Global Operations</title>
  273. <para>
  274. Occasionally you will want certain operations to affect all
  275. elements. Common scenarios include needing to set plugin prefix
  276. paths for all elements, setting decorators for all elements, and
  277. setting filters for all elements. As examples:
  278. </para>
  279. <example id="zend.form.forms.elements.global.allpaths">
  280. <title>Setting Prefix Paths for All Elements</title>
  281. <para>
  282. You can set prefix paths for all elements by type, or using
  283. a global prefix. Some examples:
  284. </para>
  285. <programlisting language="php"><![CDATA[
  286. // Set global prefix path:
  287. // Creates paths for prefixes My_Foo_Filter, My_Foo_Validate,
  288. // and My_Foo_Decorator
  289. $form->addElementPrefixPath('My_Foo', 'My/Foo/');
  290. // Just filter paths:
  291. $form->addElementPrefixPath('My_Foo_Filter',
  292. 'My/Foo/Filter',
  293. 'filter');
  294. // Just validator paths:
  295. $form->addElementPrefixPath('My_Foo_Validate',
  296. 'My/Foo/Validate',
  297. 'validate');
  298. // Just decorator paths:
  299. $form->addElementPrefixPath('My_Foo_Decorator',
  300. 'My/Foo/Decorator',
  301. 'decorator');
  302. ]]></programlisting>
  303. </example>
  304. <example id="zend.form.forms.elements.global.decorators">
  305. <title>Setting Decorators for All Elements</title>
  306. <para>
  307. You can set decorators for all elements.
  308. <methodname>setElementDecorators()</methodname> accepts an array of
  309. decorators, just like <methodname>setDecorators()</methodname>, and will
  310. overwrite any previously set decorators in each element. In
  311. this example, we set the decorators to simply a ViewHelper
  312. and a Label:
  313. </para>
  314. <programlisting language="php"><![CDATA[
  315. $form->setElementDecorators(array(
  316. 'ViewHelper',
  317. 'Label'
  318. ));
  319. ]]></programlisting>
  320. </example>
  321. <example id="zend.form.forms.elements.global.decoratorsFilter">
  322. <title>Setting Decorators for Some Elements</title>
  323. <para>
  324. You can also set decorators for a subset of elements,
  325. either by inclusion or exclusion. The second argument to
  326. <methodname>setElementDecorators()</methodname> may be an array of
  327. element names; by default, specifying such an array will
  328. set the specified decorators on those elements only. You
  329. may also pass a third argument, a flag indicating whether
  330. this list of elements is for inclusion or exclusion
  331. purposes. If the flag is <constant>FALSE</constant>, it will decorate all
  332. elements <emphasis>except</emphasis> those in the passed list.
  333. As with standard usage of the method, any decorators passed
  334. will overwrite any previously set decorators in each
  335. element.
  336. </para>
  337. <para>
  338. In the following snippet, we indicate that we want only the
  339. ViewHelper and Label decorators for the 'foo' and 'bar'
  340. elements:
  341. </para>
  342. <programlisting language="php"><![CDATA[
  343. $form->setElementDecorators(
  344. array(
  345. 'ViewHelper',
  346. 'Label'
  347. ),
  348. array(
  349. 'foo',
  350. 'bar'
  351. )
  352. );
  353. ]]></programlisting>
  354. <para>
  355. On the flip side, with this snippet, we'll now indicate
  356. that we want to use only the ViewHelper and Label
  357. decorators for every element <emphasis>except</emphasis>
  358. the 'foo' and 'bar' elements:
  359. </para>
  360. <programlisting language="php"><![CDATA[
  361. $form->setElementDecorators(
  362. array(
  363. 'ViewHelper',
  364. 'Label'
  365. ),
  366. array(
  367. 'foo',
  368. 'bar'
  369. ),
  370. false
  371. );
  372. ]]></programlisting>
  373. </example>
  374. <note>
  375. <title>Some Decorators are Inappropriate for Some Elements</title>
  376. <para>
  377. While <methodname>setElementDecorators()</methodname> may seem like
  378. a good solution, there are some cases where it may
  379. actually end up with unexpected results. For example,
  380. the various button elements (Submit, Button, Reset)
  381. currently use the label as the value of the button,
  382. and only use ViewHelper and DtDdWrapper decorators --
  383. preventing an additional labels, errors, and hints from
  384. being rendered. The example above would duplicate some
  385. content (the label) for button elements.
  386. </para>
  387. <para>
  388. You can use the inclusion/exclusion array to overcome
  389. this issue as noted in the previous example.
  390. </para>
  391. <para>
  392. So, use this method wisely, and realize that you may
  393. need to exclude some elements or manually change some elements' decorators
  394. to prevent unwanted output.
  395. </para>
  396. </note>
  397. <example id="zend.form.forms.elements.global.filters">
  398. <title>Setting Filters for All Elements</title>
  399. <para>
  400. In some cases, you may want to apply the same filter to all
  401. elements; a common case is to <methodname>trim()</methodname> all values:
  402. </para>
  403. <programlisting language="php"><![CDATA[
  404. $form->setElementFilters(array('StringTrim'));
  405. ]]></programlisting>
  406. </example>
  407. </sect3>
  408. <sect3 id="zend.form.forms.elements.methods">
  409. <title>Methods For Interacting With Elements</title>
  410. <para>
  411. The following methods may be used to interact with elements:
  412. </para>
  413. <itemizedlist>
  414. <listitem>
  415. <para>
  416. <methodname>createElement($element, $name = null, $options =
  417. null)</methodname>
  418. </para>
  419. </listitem>
  420. <listitem>
  421. <para>
  422. <methodname>addElement($element, $name = null, $options = null)</methodname>
  423. </para>
  424. </listitem>
  425. <listitem>
  426. <para>
  427. <methodname>addElements(array $elements)</methodname>
  428. </para>
  429. </listitem>
  430. <listitem>
  431. <para>
  432. <methodname>setElements(array $elements)</methodname>
  433. </para>
  434. </listitem>
  435. <listitem>
  436. <para>
  437. <methodname>getElement($name)</methodname>
  438. </para>
  439. </listitem>
  440. <listitem>
  441. <para>
  442. <methodname>getElements()</methodname>
  443. </para>
  444. </listitem>
  445. <listitem>
  446. <para>
  447. <methodname>removeElement($name)</methodname>
  448. </para>
  449. </listitem>
  450. <listitem>
  451. <para>
  452. <methodname>clearElements()</methodname>
  453. </para>
  454. </listitem>
  455. <listitem>
  456. <para>
  457. <methodname>setDefaults(array $defaults)</methodname>
  458. </para>
  459. </listitem>
  460. <listitem>
  461. <para>
  462. <methodname>setDefault($name, $value)</methodname>
  463. </para>
  464. </listitem>
  465. <listitem>
  466. <para>
  467. <methodname>getValue($name)</methodname>
  468. </para>
  469. </listitem>
  470. <listitem>
  471. <para>
  472. <methodname>getValues()</methodname>
  473. </para>
  474. </listitem>
  475. <listitem>
  476. <para>
  477. <methodname>getUnfilteredValue($name)</methodname>
  478. </para>
  479. </listitem>
  480. <listitem>
  481. <para>
  482. <methodname>getUnfilteredValues()</methodname>
  483. </para>
  484. </listitem>
  485. <listitem>
  486. <para>
  487. <methodname>setElementFilters(array $filters)</methodname>
  488. </para>
  489. </listitem>
  490. <listitem>
  491. <para>
  492. <methodname>setElementDecorators(array $decorators)</methodname>
  493. </para>
  494. </listitem>
  495. <listitem>
  496. <para>
  497. <methodname>addElementPrefixPath($prefix, $path, $type = null)</methodname>
  498. </para>
  499. </listitem>
  500. <listitem>
  501. <para>
  502. <methodname>addElementPrefixPaths(array $spec)</methodname>
  503. </para>
  504. </listitem>
  505. </itemizedlist>
  506. </sect3>
  507. </sect2>
  508. <sect2 id="zend.form.forms.displaygroups">
  509. <title>Display Groups</title>
  510. <para>
  511. Display groups are a way to create virtual groupings of elements for
  512. display purposes. All elements remain accessible by name in the
  513. form, but when iterating over the form or rendering, any elements in
  514. a display group are rendered together. The most common use case for
  515. this is for grouping elements in fieldsets.
  516. </para>
  517. <para>
  518. The base class for display groups is
  519. <classname>Zend_Form_DisplayGroup</classname>. While it can be instantiated
  520. directly, it is usually best to use <classname>Zend_Form</classname>'s
  521. <methodname>addDisplayGroup()</methodname> method to do so. This method takes an
  522. array of element names as its first argument, and a name for the display
  523. group as its second argument. You may optionally pass in an array of
  524. options or a <classname>Zend_Config</classname> object as the third argument.
  525. </para>
  526. <para>
  527. Assuming that the elements 'username' and 'password' are already set
  528. in the form, the following code would group these elements in a
  529. 'login' display group:
  530. </para>
  531. <programlisting language="php"><![CDATA[
  532. $form->addDisplayGroup(array('username', 'password'), 'login');
  533. ]]></programlisting>
  534. <para>
  535. You can access display groups using the
  536. <methodname>getDisplayGroup()</methodname> method, or via overloading using the
  537. display group's name:
  538. </para>
  539. <programlisting language="php"><![CDATA[
  540. // Using getDisplayGroup():
  541. $login = $form->getDisplayGroup('login');
  542. // Using overloading:
  543. $login = $form->login;
  544. ]]></programlisting>
  545. <note>
  546. <title>Default Decorators Do Not Need to Be Loaded</title>
  547. <para>
  548. By default, the default decorators are loaded during object
  549. initialization. You can disable this by passing the
  550. 'disableLoadDefaultDecorators' option when creating a display
  551. group:
  552. </para>
  553. <programlisting language="php"><![CDATA[
  554. $form->addDisplayGroup(
  555. array('foo', 'bar'),
  556. 'foobar',
  557. array('disableLoadDefaultDecorators' => true)
  558. );
  559. ]]></programlisting>
  560. <para>
  561. This option may be mixed with any other options you pass,
  562. both as array options or in a <classname>Zend_Config</classname> object.
  563. </para>
  564. </note>
  565. <sect3 id="zend.form.forms.displaygroups.global">
  566. <title>Global Operations</title>
  567. <para>
  568. Just as with elements, there are some operations which might
  569. affect all display groups; these include setting decorators and
  570. setting the plugin path in which to look for decorators.
  571. </para>
  572. <example id="zend.form.forms.displaygroups.global.paths">
  573. <title>Setting Decorator Prefix Path for All Display Groups</title>
  574. <para>
  575. By default, display groups inherit whichever decorator paths
  576. the form uses; however, if they should look in alternate
  577. locations, you can use the
  578. <methodname>addDisplayGroupPrefixPath()</methodname> method.
  579. </para>
  580. <programlisting language="php"><![CDATA[
  581. $form->addDisplayGroupPrefixPath('My_Foo_Decorator', 'My/Foo/Decorator');
  582. ]]></programlisting>
  583. </example>
  584. <example id="zend.form.forms.displaygroups.global.decorators">
  585. <title>Setting Decorators for All Display Groups</title>
  586. <para>
  587. You can set decorators for all display groups.
  588. <methodname>setDisplayGroupDecorators()</methodname> accepts an array of
  589. decorators, just like <methodname>setDecorators()</methodname>, and will
  590. overwrite any previously set decorators in each display
  591. group. In this example, we set the decorators to simply a
  592. fieldset (the FormElements decorator is necessary to ensure
  593. that the elements are iterated):
  594. </para>
  595. <programlisting language="php"><![CDATA[
  596. $form->setDisplayGroupDecorators(array(
  597. 'FormElements',
  598. 'Fieldset'
  599. ));
  600. ]]></programlisting>
  601. </example>
  602. </sect3>
  603. <sect3 id="zend.form.forms.displaygroups.customClasses">
  604. <title>Using Custom Display Group Classes</title>
  605. <para>
  606. By default, <classname>Zend_Form</classname> uses the
  607. <classname>Zend_Form_DisplayGroup</classname> class for display groups.
  608. You may find you need to extend this class in order to provided
  609. custom functionality. <methodname>addDisplayGroup()</methodname> does not
  610. allow passing in a concrete instance, but does allow specifying
  611. the class to use as one of its options, using the
  612. 'displayGroupClass' key:
  613. </para>
  614. <programlisting language="php"><![CDATA[
  615. // Use the 'My_DisplayGroup' class
  616. $form->addDisplayGroup(
  617. array('username', 'password'),
  618. 'user',
  619. array('displayGroupClass' => 'My_DisplayGroup')
  620. );
  621. ]]></programlisting>
  622. <para>
  623. If the class has not yet been loaded, <classname>Zend_Form</classname>
  624. will attempt to do so using <classname>Zend_Loader</classname>.
  625. </para>
  626. <para>
  627. You can also specify a default display group class to use with
  628. the form such that all display groups created with the form
  629. object will use that class:
  630. </para>
  631. <programlisting language="php"><![CDATA[
  632. // Use the 'My_DisplayGroup' class for all display groups:
  633. $form->setDefaultDisplayGroupClass('My_DisplayGroup');
  634. ]]></programlisting>
  635. <para>
  636. This setting may be specified in configurations as
  637. 'defaultDisplayGroupClass', and will be loaded early to ensure
  638. all display groups use that class.
  639. </para>
  640. </sect3>
  641. <sect3 id="zend.form.forms.displaygroups.interactionmethods">
  642. <title>Methods for Interacting With Display Groups</title>
  643. <para>
  644. The following methods may be used to interact with display
  645. groups:
  646. </para>
  647. <itemizedlist>
  648. <listitem>
  649. <para>
  650. <methodname>addDisplayGroup(array $elements, $name, $options
  651. = null)</methodname>
  652. </para>
  653. </listitem>
  654. <listitem>
  655. <para>
  656. <methodname>addDisplayGroups(array $groups)</methodname>
  657. </para>
  658. </listitem>
  659. <listitem>
  660. <para>
  661. <methodname>setDisplayGroups(array $groups)</methodname>
  662. </para>
  663. </listitem>
  664. <listitem>
  665. <para>
  666. <methodname>getDisplayGroup($name)</methodname>
  667. </para>
  668. </listitem>
  669. <listitem>
  670. <para>
  671. <methodname>getDisplayGroups()</methodname>
  672. </para>
  673. </listitem>
  674. <listitem>
  675. <para>
  676. <methodname>removeDisplayGroup($name)</methodname>
  677. </para>
  678. </listitem>
  679. <listitem>
  680. <para>
  681. <methodname>clearDisplayGroups()</methodname>
  682. </para>
  683. </listitem>
  684. <listitem>
  685. <para>
  686. <methodname>setDisplayGroupDecorators(array $decorators)</methodname>
  687. </para>
  688. </listitem>
  689. <listitem>
  690. <para>
  691. <methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname>
  692. </para>
  693. </listitem>
  694. <listitem>
  695. <para>
  696. <methodname>setDefaultDisplayGroupClass($class)</methodname>
  697. </para>
  698. </listitem>
  699. <listitem>
  700. <para>
  701. <methodname>getDefaultDisplayGroupClass($class)</methodname>
  702. </para>
  703. </listitem>
  704. </itemizedlist>
  705. </sect3>
  706. <sect3 id="zend.form.forms.displaygroups.methods">
  707. <title>Zend_Form_DisplayGroup Methods</title>
  708. <para>
  709. <classname>Zend_Form_DisplayGroup</classname> has the following methods,
  710. grouped by type:
  711. </para>
  712. <itemizedlist>
  713. <listitem>
  714. <para>Configuration:</para>
  715. <itemizedlist>
  716. <listitem>
  717. <para>
  718. <methodname>setOptions(array $options)</methodname>
  719. </para>
  720. </listitem>
  721. <listitem>
  722. <para><methodname>setConfig(Zend_Config $config)</methodname></para>
  723. </listitem>
  724. </itemizedlist>
  725. </listitem>
  726. <listitem>
  727. <para>Metadata:</para>
  728. <itemizedlist>
  729. <listitem>
  730. <para><methodname>setAttrib($key, $value)</methodname></para>
  731. </listitem>
  732. <listitem>
  733. <para><methodname>addAttribs(array $attribs)</methodname></para>
  734. </listitem>
  735. <listitem>
  736. <para><methodname>setAttribs(array $attribs)</methodname></para>
  737. </listitem>
  738. <listitem><para><methodname>getAttrib($key)</methodname></para></listitem>
  739. <listitem><para><methodname>getAttribs()</methodname></para></listitem>
  740. <listitem>
  741. <para><methodname>removeAttrib($key)</methodname></para>
  742. </listitem>
  743. <listitem><para><methodname>clearAttribs()</methodname></para></listitem>
  744. <listitem><para><methodname>setName($name)</methodname></para></listitem>
  745. <listitem><para><methodname>getName()</methodname></para></listitem>
  746. <listitem>
  747. <para><methodname>setDescription($value)</methodname></para>
  748. </listitem>
  749. <listitem><para><methodname>getDescription()</methodname></para></listitem>
  750. <listitem>
  751. <para><methodname>setLegend($legend)</methodname></para>
  752. </listitem>
  753. <listitem><para><methodname>getLegend()</methodname></para></listitem>
  754. <listitem><para><methodname>setOrder($order)</methodname></para></listitem>
  755. <listitem><para><methodname>getOrder()</methodname></para></listitem>
  756. </itemizedlist>
  757. </listitem>
  758. <listitem>
  759. <para>Elements:</para>
  760. <itemizedlist>
  761. <listitem>
  762. <para>
  763. <methodname>createElement($type, $name, array $options
  764. = array())</methodname>
  765. </para>
  766. </listitem>
  767. <listitem>
  768. <para>
  769. <methodname>addElement($typeOrElement, $name, array $options =
  770. array())</methodname>
  771. </para>
  772. </listitem>
  773. <listitem>
  774. <para><methodname>addElements(array $elements)</methodname></para>
  775. </listitem>
  776. <listitem>
  777. <para><methodname>setElements(array $elements)</methodname></para>
  778. </listitem>
  779. <listitem><para><methodname>getElement($name)</methodname></para></listitem>
  780. <listitem><para><methodname>getElements()</methodname></para></listitem>
  781. <listitem>
  782. <para><methodname>removeElement($name)</methodname></para>
  783. </listitem>
  784. <listitem><para><methodname>clearElements()</methodname></para></listitem>
  785. </itemizedlist>
  786. </listitem>
  787. <listitem>
  788. <para>Plugin loaders:</para>
  789. <itemizedlist>
  790. <listitem>
  791. <para>
  792. <methodname>setPluginLoader(Zend_Loader_PluginLoader
  793. $loader)</methodname>
  794. </para>
  795. </listitem>
  796. <listitem><para><methodname>getPluginLoader()</methodname></para></listitem>
  797. <listitem>
  798. <para><methodname>addPrefixPath($prefix, $path)</methodname></para>
  799. </listitem>
  800. <listitem>
  801. <para><methodname>addPrefixPaths(array $spec)</methodname></para>
  802. </listitem>
  803. </itemizedlist>
  804. </listitem>
  805. <listitem>
  806. <para>Decorators:</para>
  807. <itemizedlist>
  808. <listitem>
  809. <para>
  810. <methodname>addDecorator($decorator, $options = null)</methodname>
  811. </para>
  812. </listitem>
  813. <listitem>
  814. <para><methodname>addDecorators(array $decorators)</methodname></para>
  815. </listitem>
  816. <listitem>
  817. <para><methodname>setDecorators(array $decorators)</methodname></para>
  818. </listitem>
  819. <listitem>
  820. <para><methodname>getDecorator($name)</methodname></para>
  821. </listitem>
  822. <listitem><para><methodname>getDecorators()</methodname></para></listitem>
  823. <listitem>
  824. <para><methodname>removeDecorator($name)</methodname></para>
  825. </listitem>
  826. <listitem><para><methodname>clearDecorators()</methodname></para></listitem>
  827. </itemizedlist>
  828. </listitem>
  829. <listitem>
  830. <para>Rendering:</para>
  831. <itemizedlist>
  832. <listitem>
  833. <para>
  834. <methodname>setView(Zend_View_Interface $view = null)</methodname>
  835. </para>
  836. </listitem>
  837. <listitem><para><methodname>getView()</methodname></para></listitem>
  838. <listitem>
  839. <para>
  840. <methodname>render(Zend_View_Interface $view = null)</methodname>
  841. </para>
  842. </listitem>
  843. </itemizedlist>
  844. </listitem>
  845. <listitem>
  846. <para>I18n:</para>
  847. <itemizedlist>
  848. <listitem>
  849. <para>
  850. <methodname>setTranslator(Zend_Translate_Adapter $translator =
  851. null)</methodname>
  852. </para>
  853. </listitem>
  854. <listitem><para><methodname>getTranslator()</methodname></para></listitem>
  855. <listitem>
  856. <para><methodname>setDisableTranslator($flag)</methodname></para>
  857. </listitem>
  858. <listitem>
  859. <para><methodname>translatorIsDisabled()</methodname></para>
  860. </listitem>
  861. </itemizedlist>
  862. </listitem>
  863. </itemizedlist>
  864. </sect3>
  865. </sect2>
  866. <sect2 id="zend.form.forms.subforms">
  867. <title>Sub Forms</title>
  868. <para>
  869. Sub forms serve several purposes:
  870. </para>
  871. <itemizedlist>
  872. <listitem>
  873. <para>
  874. Creating logical element groups. Since sub forms are simply
  875. forms, you can validate subforms as individual entities.
  876. </para>
  877. </listitem>
  878. <listitem>
  879. <para>
  880. Creating multi-page forms. Since sub forms are simply forms, you
  881. can display a separate sub form per page, building up multi-page
  882. forms where each form has its own validation logic. Only once
  883. all sub forms validate would the form be considered complete.
  884. </para>
  885. </listitem>
  886. <listitem>
  887. <para>
  888. Display groupings. Like display groups, sub forms, when rendered
  889. as part of a larger form, can be used to group elements. Be
  890. aware, however, that the master form object will have no
  891. awareness of the elements in sub forms.
  892. </para>
  893. </listitem>
  894. </itemizedlist>
  895. <para>
  896. A sub form may be a <classname>Zend_Form</classname> object, or, more
  897. typically, a <classname>Zend_Form_SubForm</classname> object. The latter
  898. contains decorators suitable for inclusion in a larger form (i.e.,
  899. it does not render additional <acronym>HTML</acronym> form tags, but does group
  900. elements). To attach a sub form, simply add it to the form and give
  901. it a name:
  902. </para>
  903. <programlisting language="php"><![CDATA[
  904. $form->addSubForm($subForm, 'subform');
  905. ]]></programlisting>
  906. <para>
  907. You can retrieve a sub form using either
  908. <methodname>getSubForm($name)</methodname> or overloading using the sub form
  909. name:
  910. </para>
  911. <programlisting language="php"><![CDATA[
  912. // Using getSubForm():
  913. $subForm = $form->getSubForm('subform');
  914. // Using overloading:
  915. $subForm = $form->subform;
  916. ]]></programlisting>
  917. <para>
  918. Sub forms are included in form iteration, although the elements they
  919. contain are not.
  920. </para>
  921. <sect3 id="zend.form.forms.subforms.global">
  922. <title>Global Operations</title>
  923. <para>
  924. Like elements and display groups, there are some operations that
  925. might need to affect all sub forms. Unlike display groups and
  926. elements, however, sub forms inherit most functionality from the
  927. master form object, and the only real operation that may need to
  928. be performed globally is setting decorators for sub forms. For
  929. this purpose, there is the <methodname>setSubFormDecorators()</methodname>
  930. method. In the next example, we'll set the decorator for all
  931. subforms to be simply a fieldset (the FormElements decorator is
  932. needed to ensure its elements are iterated):
  933. </para>
  934. <programlisting language="php"><![CDATA[
  935. $form->setSubFormDecorators(array(
  936. 'FormElements',
  937. 'Fieldset'
  938. ));
  939. ]]></programlisting>
  940. </sect3>
  941. <sect3 id="zend.form.forms.subforms.methods">
  942. <title>Methods for Interacting With Sub Forms</title>
  943. <para>
  944. The following methods may be used to interact with sub forms:
  945. </para>
  946. <itemizedlist>
  947. <listitem>
  948. <para>
  949. <methodname>addSubForm(Zend_Form $form, $name, $order = null)</methodname>
  950. </para>
  951. </listitem>
  952. <listitem>
  953. <para>
  954. <methodname>addSubForms(array $subForms)</methodname>
  955. </para>
  956. </listitem>
  957. <listitem>
  958. <para>
  959. <methodname>setSubForms(array $subForms)</methodname>
  960. </para>
  961. </listitem>
  962. <listitem>
  963. <para>
  964. <methodname>getSubForm($name)</methodname>
  965. </para>
  966. </listitem>
  967. <listitem>
  968. <para>
  969. <methodname>getSubForms()</methodname>
  970. </para>
  971. </listitem>
  972. <listitem>
  973. <para>
  974. <methodname>removeSubForm($name)</methodname>
  975. </para>
  976. </listitem>
  977. <listitem>
  978. <para>
  979. <methodname>clearSubForms()</methodname>
  980. </para>
  981. </listitem>
  982. <listitem>
  983. <para>
  984. <methodname>setSubFormDecorators(array $decorators)</methodname>
  985. </para>
  986. </listitem>
  987. </itemizedlist>
  988. </sect3>
  989. </sect2>
  990. <sect2 id="zend.form.forms.metadata">
  991. <title>Metadata and Attributes</title>
  992. <para>
  993. While a form's usefulness primarily derives from the elements it
  994. contains, it can also contain other metadata, such as a name (often
  995. used as a unique ID in the <acronym>HTML</acronym> markup); the form action and method;
  996. the number of elements, groups, and sub forms it contains; and
  997. arbitrary metadata (usually used to set <acronym>HTML</acronym> attributes for the form
  998. tag itself).
  999. </para>
  1000. <para>
  1001. You can set and retrieve a form's name using the name accessors:
  1002. </para>
  1003. <programlisting language="php"><![CDATA[
  1004. // Set the name:
  1005. $form->setName('registration');
  1006. // Retrieve the name:
  1007. $name = $form->getName();
  1008. ]]></programlisting>
  1009. <para>
  1010. To set the action (url to which the form submits) and method (method
  1011. by which it should submit, e.g., 'POST' or '<constant>GET</constant>'), use the action
  1012. and method accessors:
  1013. </para>
  1014. <programlisting language="php"><![CDATA[
  1015. // Set the action and method:
  1016. $form->setAction('/user/login')
  1017. ->setMethod('post');
  1018. ]]></programlisting>
  1019. <para>
  1020. You may also specify the form encoding type specifically using the
  1021. enctype accessors. <classname>Zend_Form</classname> defines two constants,
  1022. <constant>Zend_Form::ENCTYPE_URLENCODED</constant> and
  1023. <constant>Zend_Form::ENCTYPE_MULTIPART</constant>, corresponding to the
  1024. values 'application/x-www-form-urlencoded' and
  1025. 'multipart/form-data', respectively; however, you can set this to
  1026. any arbitrary encoding type.
  1027. </para>
  1028. <programlisting language="php"><![CDATA[
  1029. // Set the action, method, and enctype:
  1030. $form->setAction('/user/login')
  1031. ->setMethod('post')
  1032. ->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
  1033. ]]></programlisting>
  1034. <note>
  1035. <para>
  1036. The method, action, and enctype are only used internally for rendering,
  1037. and not for any sort of validation.
  1038. </para>
  1039. </note>
  1040. <para>
  1041. <classname>Zend_Form</classname> implements the <classname>Countable</classname>
  1042. interface, allowing you to pass it as an argument to count:
  1043. </para>
  1044. <programlisting language="php"><![CDATA[
  1045. $numItems = count($form);
  1046. ]]></programlisting>
  1047. <para>
  1048. Setting arbitrary metadata is done through the attribs accessors.
  1049. Since overloading in <classname>Zend_Form</classname> is used to access
  1050. elements, display groups, and sub forms, this is the only method for
  1051. accessing metadata.
  1052. </para>
  1053. <programlisting language="php"><![CDATA[
  1054. // Setting attributes:
  1055. $form->setAttrib('class', 'zend-form')
  1056. ->addAttribs(array(
  1057. 'id' => 'registration',
  1058. 'onSubmit' => 'validate(this)',
  1059. ));
  1060. // Retrieving attributes:
  1061. $class = $form->getAttrib('class');
  1062. $attribs = $form->getAttribs();
  1063. // Remove an attribute:
  1064. $form->removeAttrib('onSubmit');
  1065. // Clear all attributes:
  1066. $form->clearAttribs();
  1067. ]]></programlisting>
  1068. </sect2>
  1069. <sect2 id="zend.form.forms.decorators">
  1070. <title>Decorators</title>
  1071. <para>
  1072. Creating the markup for a form is often a time-consuming task,
  1073. particularly if you plan on re-using the same markup to show things
  1074. such as validation errors, submitted values, etc.
  1075. <classname>Zend_Form</classname>'s answer to this issue is
  1076. <emphasis>decorators</emphasis>.
  1077. </para>
  1078. <para>
  1079. Decorators for <classname>Zend_Form</classname> objects can be used to render
  1080. a form. The FormElements decorator will iterate through all items in
  1081. a form -- elements, display groups, and sub forms -- and render
  1082. them, returning the result. Additional decorators may then be used
  1083. to wrap this content, or append or prepend it.
  1084. </para>
  1085. <para>
  1086. The default decorators for <classname>Zend_Form</classname> are FormElements,
  1087. HtmlTag (wraps in a definition list), and Form; the equivalent code
  1088. for creating them is as follows:
  1089. </para>
  1090. <programlisting language="php"><![CDATA[
  1091. $form->setDecorators(array(
  1092. 'FormElements',
  1093. array('HtmlTag', array('tag' => 'dl')),
  1094. 'Form'
  1095. ));
  1096. ]]></programlisting>
  1097. <para>
  1098. This creates output like the following:
  1099. </para>
  1100. <programlisting language="html"><![CDATA[
  1101. <form action="/form/action" method="post">
  1102. <dl>
  1103. ...
  1104. </dl>
  1105. </form>
  1106. ]]></programlisting>
  1107. <para>
  1108. Any attributes set on the form object will be used as <acronym>HTML</acronym>
  1109. attributes of the <emphasis>&lt;form&gt;</emphasis> tag.
  1110. </para>
  1111. <note>
  1112. <title>Default Decorators Do Not Need to Be Loaded</title>
  1113. <para>
  1114. By default, the default decorators are loaded during object
  1115. initialization. You can disable this by passing the
  1116. 'disableLoadDefaultDecorators' option to the constructor:
  1117. </para>
  1118. <programlisting language="php"><![CDATA[
  1119. $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
  1120. ]]></programlisting>
  1121. <para>
  1122. This option may be mixed with any other options you pass,
  1123. both as array options or in a <classname>Zend_Config</classname> object.
  1124. </para>
  1125. </note>
  1126. <note>
  1127. <title>Using Multiple Decorators of the Same Type</title>
  1128. <para>
  1129. Internally, <classname>Zend_Form</classname> uses a decorator's
  1130. class as the lookup mechanism when retrieving decorators. As a
  1131. result, you cannot register multiple decorators of the same
  1132. type; subsequent decorators will simply overwrite those that
  1133. existed before.
  1134. </para>
  1135. <para>
  1136. To get around this, you can use aliases. Instead of passing a
  1137. decorator or decorator name as the first argument to
  1138. <methodname>addDecorator()</methodname>, pass an array with a single
  1139. element, with the alias pointing to the decorator object or
  1140. name:
  1141. </para>
  1142. <programlisting language="php"><![CDATA[
  1143. // Alias to 'FooBar':
  1144. $form->addDecorator(array('FooBar' => 'HtmlTag'), array('tag' => 'div'));
  1145. // And retrieve later:
  1146. $form = $element->getDecorator('FooBar');
  1147. ]]></programlisting>
  1148. <para>
  1149. In the <methodname>addDecorators()</methodname> and
  1150. <methodname>setDecorators()</methodname> methods, you will need to pass
  1151. the 'decorator' option in the array representing the decorator:
  1152. </para>
  1153. <programlisting language="php"><![CDATA[
  1154. // Add two 'HtmlTag' decorators, aliasing one to 'FooBar':
  1155. $form->addDecorators(
  1156. array('HtmlTag', array('tag' => 'div')),
  1157. array(
  1158. 'decorator' => array('FooBar' => 'HtmlTag'),
  1159. 'options' => array('tag' => 'dd')
  1160. ),
  1161. );
  1162. // And retrieve later:
  1163. $htmlTag = $form->getDecorator('HtmlTag');
  1164. $fooBar = $form->getDecorator('FooBar');
  1165. ]]></programlisting>
  1166. </note>
  1167. <para>
  1168. You may create your own decorators for generating the form. One
  1169. common use case is if you know the exact <acronym>HTML</acronym> you wish to use; your
  1170. decorator could create the exact <acronym>HTML</acronym> and simply return it,
  1171. potentially using the decorators from individual elements or display
  1172. groups.
  1173. </para>
  1174. <para>
  1175. The following methods may be used to interact with decorators:
  1176. </para>
  1177. <itemizedlist>
  1178. <listitem>
  1179. <para>
  1180. <methodname>addDecorator($decorator, $options = null)</methodname>
  1181. </para>
  1182. </listitem>
  1183. <listitem>
  1184. <para>
  1185. <methodname>addDecorators(array $decorators)</methodname>
  1186. </para>
  1187. </listitem>
  1188. <listitem>
  1189. <para>
  1190. <methodname>setDecorators(array $decorators)</methodname>
  1191. </para>
  1192. </listitem>
  1193. <listitem>
  1194. <para>
  1195. <methodname>getDecorator($name)</methodname>
  1196. </para>
  1197. </listitem>
  1198. <listitem>
  1199. <para>
  1200. <methodname>getDecorators()</methodname>
  1201. </para>
  1202. </listitem>
  1203. <listitem>
  1204. <para>
  1205. <methodname>removeDecorator($name)</methodname>
  1206. </para>
  1207. </listitem>
  1208. <listitem>
  1209. <para>
  1210. <methodname>clearDecorators()</methodname>
  1211. </para>
  1212. </listitem>
  1213. </itemizedlist>
  1214. <para>
  1215. <classname>Zend_Form</classname> also uses overloading to allow rendering
  1216. specific decorators. <methodname>__call()</methodname> will intercept methods
  1217. that lead with the text 'render' and use the remainder of the method
  1218. name to lookup a decorator; if found, it will then render that
  1219. <emphasis>single</emphasis> decorator. Any arguments passed to the
  1220. method call will be used as content to pass to the decorator's
  1221. <methodname>render()</methodname> method. As an example:
  1222. </para>
  1223. <programlisting language="php"><![CDATA[
  1224. // Render only the FormElements decorator:
  1225. echo $form->renderFormElements();
  1226. // Render only the fieldset decorator, passing in content:
  1227. echo $form->renderFieldset("<p>This is fieldset content</p>");
  1228. ]]></programlisting>
  1229. <para>
  1230. If the decorator does not exist, an exception is raised.
  1231. </para>
  1232. </sect2>
  1233. <sect2 id="zend.form.forms.validation">
  1234. <title>Validation</title>
  1235. <para>
  1236. A primary use case for forms is validating submitted data.
  1237. <classname>Zend_Form</classname> allows you to validate an entire form, a partial form,
  1238. or responses for XmlHttpRequests (AJAX). If the submitted data is not valid, it has
  1239. methods for retrieving the various error codes and messages for
  1240. elements and sub forms.
  1241. </para>
  1242. <para>
  1243. To validate a full form, use the <methodname>isValid()</methodname> method:
  1244. </para>
  1245. <programlisting language="php"><![CDATA[
  1246. if (!$form->isValid($_POST)) {
  1247. // failed validation
  1248. }
  1249. ]]></programlisting>
  1250. <para>
  1251. <methodname>isValid()</methodname> will validate every required element, and any
  1252. unrequired element contained in the submitted data.
  1253. </para>
  1254. <para>
  1255. Sometimes you may need to validate only a subset of the data; for
  1256. this, use <methodname>isValidPartial($data)</methodname>:
  1257. </para>
  1258. <programlisting language="php"><![CDATA[
  1259. if (!$form->isValidPartial($data)) {
  1260. // failed validation
  1261. }
  1262. ]]></programlisting>
  1263. <para>
  1264. <methodname>isValidPartial()</methodname> only attempts to validate those items
  1265. in the data for which there are matching elements; if an element is
  1266. not represented in the data, it is skipped.
  1267. </para>
  1268. <para>
  1269. When validating elements or groups of elements for an <acronym>AJAX</acronym> request,
  1270. you will typically be validating a subset of the form, and want the
  1271. response back in <acronym>JSON</acronym>. <methodname>processAjax()</methodname> does
  1272. precisely that:
  1273. </para>
  1274. <programlisting language="php"><![CDATA[
  1275. $json = $form->processAjax($data);
  1276. ]]></programlisting>
  1277. <para>
  1278. You can then simply send the <acronym>JSON</acronym> response to the client. If the
  1279. form is valid, this will be a boolean <constant>TRUE</constant> response. If not, it
  1280. will be a javascript object containing key/message pairs, where each
  1281. 'message' is an array of validation error messages.
  1282. </para>
  1283. <para>
  1284. For forms that fail validation, you can retrieve both error codes
  1285. and error messages, using <methodname>getErrors()</methodname> and
  1286. <methodname>getMessages()</methodname>, respectively:
  1287. </para>
  1288. <programlisting language="php"><![CDATA[
  1289. $codes = $form->getErrors();
  1290. $messages = $form->getMessages();
  1291. ]]></programlisting>
  1292. <note>
  1293. <para>
  1294. Since the messages returned by <methodname>getMessages()</methodname> are an
  1295. array of error code/message pairs, <methodname>getErrors()</methodname> is
  1296. typically not needed.
  1297. </para>
  1298. </note>
  1299. <para>
  1300. You can retrieve codes and error messages for individual elements by
  1301. simply passing the element name to each:
  1302. </para>
  1303. <programlisting language="php"><![CDATA[
  1304. $codes = $form->getErrors('username');
  1305. $messages = $form->getMessages('username');
  1306. ]]></programlisting>
  1307. <note>
  1308. <para>
  1309. Note: When validating elements, <classname>Zend_Form</classname> sends a
  1310. second argument to each element's <methodname>isValid()</methodname> method:
  1311. the array of data being validated. This can then be used by
  1312. individual validators to allow them to utilize other submitted
  1313. values when determining the validity of the data. An example
  1314. would be a registration form that requires both a password and
  1315. password confirmation; the password element could use the
  1316. password confirmation as part of its validation.
  1317. </para>
  1318. </note>
  1319. <sect3 id="zend.form.forms.validation.errors">
  1320. <title>Custom Error Messages</title>
  1321. <para>
  1322. At times, you may want to specify one or more specific error
  1323. messages to use instead of the error messages generated by the
  1324. validators attached to your elements. Additionally, at times you
  1325. may want to mark the form invalid yourself. This
  1326. functionality is possible via the following methods.
  1327. </para>
  1328. <itemizedlist>
  1329. <listitem>
  1330. <para>
  1331. <methodname>addErrorMessage($message)</methodname>: add an error message
  1332. to display on form validation errors. You may call this more
  1333. than once, and new messages are appended to the stack.
  1334. </para>
  1335. </listitem>
  1336. <listitem>
  1337. <para>
  1338. <methodname>addErrorMessages(array $messages)</methodname>: add multiple
  1339. error messages to display on form validation errors.
  1340. </para>
  1341. </listitem>
  1342. <listitem>
  1343. <para>
  1344. <methodname>setErrorMessages(array $messages)</methodname>: add multiple
  1345. error messages to display on form validation errors,
  1346. overwriting all previously set error messages.
  1347. </para>
  1348. </listitem>
  1349. <listitem>
  1350. <para>
  1351. <methodname>getErrorMessages()</methodname>: retrieve the list of
  1352. custom error messages that have been defined.
  1353. </para>
  1354. </listitem>
  1355. <listitem>
  1356. <para>
  1357. <methodname>clearErrorMessages()</methodname>: remove all custom error
  1358. messages that have been defined.
  1359. </para>
  1360. </listitem>
  1361. <listitem>
  1362. <para>
  1363. <methodname>markAsError()</methodname>: mark the form as having
  1364. failed validation.
  1365. </para>
  1366. </listitem>
  1367. <listitem>
  1368. <para>
  1369. <methodname>addError($message)</methodname>: add a message to the custom
  1370. error messages stack and flag the form as invalid.
  1371. </para>
  1372. </listitem>
  1373. <listitem>
  1374. <para>
  1375. <methodname>addErrors(array $messages)</methodname>: add several
  1376. messages to the custom error messages stack and flag the
  1377. form as invalid.
  1378. </para>
  1379. </listitem>
  1380. <listitem>
  1381. <para>
  1382. <methodname>setErrors(array $messages)</methodname>: overwrite the
  1383. custom error messages stack with the provided messages and
  1384. flag the form as invalid.
  1385. </para>
  1386. </listitem>
  1387. </itemizedlist>
  1388. <para>
  1389. All errors set in this fashion may be translated.
  1390. </para>
  1391. </sect3>
  1392. <sect3 id="zend.form.forms.validation.values">
  1393. <title>Retrieving Valid Values Only</title>
  1394. <para>
  1395. There are scenarios when you want to allow your user to work on a valid form
  1396. in several steps. Meanwhile you allow the user to save the form with any
  1397. set of values inbetween. Then if all the data is specified you can transfer
  1398. the model from the building or prototying stage to a valid stage.
  1399. </para>
  1400. <para>
  1401. You can retrieve all the valid values that match the submitted data by calling:
  1402. </para>
  1403. <programlisting language="php"><![CDATA[
  1404. $validValues = $form->getValidValues($_POST);
  1405. ]]></programlisting>
  1406. </sect3>
  1407. </sect2>
  1408. <sect2 id="zend.form.forms.methods">
  1409. <title>Methods</title>
  1410. <para>
  1411. The following is a full list of methods available to
  1412. <classname>Zend_Form</classname>, grouped by type:
  1413. </para>
  1414. <itemizedlist>
  1415. <listitem>
  1416. <para>Configuration and Options:</para>
  1417. <itemizedlist>
  1418. <listitem>
  1419. <para><methodname>setOptions(array $options)</methodname></para>
  1420. </listitem>
  1421. <listitem>
  1422. <para><methodname>setConfig(Zend_Config $config)</methodname></para>
  1423. </listitem>
  1424. </itemizedlist>
  1425. </listitem>
  1426. <listitem>
  1427. <para>Plugin Loaders and paths:</para>
  1428. <itemizedlist>
  1429. <listitem>
  1430. <para>
  1431. <methodname>setPluginLoader(Zend_Loader_PluginLoader_Interface $loader,
  1432. $type = null)</methodname>
  1433. </para>
  1434. </listitem>
  1435. <listitem>
  1436. <para><methodname>getPluginLoader($type = null)</methodname></para>
  1437. </listitem>
  1438. <listitem>
  1439. <para>
  1440. <methodname>addPrefixPath($prefix, $path, $type = null) </methodname>
  1441. </para>
  1442. </listitem>
  1443. <listitem>
  1444. <para><methodname>addPrefixPaths(array $spec)</methodname></para>
  1445. </listitem>
  1446. <listitem>
  1447. <para>
  1448. <methodname>addElementPrefixPath($prefix, $path, $type
  1449. = null)</methodname>
  1450. </para>
  1451. </listitem>
  1452. <listitem>
  1453. <para><methodname>addElementPrefixPaths(array $spec)</methodname></para>
  1454. </listitem>
  1455. <listitem>
  1456. <para>
  1457. <methodname>addDisplayGroupPrefixPath($prefix, $path)</methodname>
  1458. </para>
  1459. </listitem>
  1460. </itemizedlist>
  1461. </listitem>
  1462. <listitem>
  1463. <para>Metadata:</para>
  1464. <itemizedlist>
  1465. <listitem>
  1466. <para><methodname>setAttrib($key, $value)</methodname></para>
  1467. </listitem>
  1468. <listitem>
  1469. <para><methodname>addAttribs(array $attribs)</methodname></para>
  1470. </listitem>
  1471. <listitem>
  1472. <para><methodname>setAttribs(array $attribs)</methodname></para>
  1473. </listitem>
  1474. <listitem><para><methodname>getAttrib($key)</methodname></para></listitem>
  1475. <listitem><para><methodname>getAttribs()</methodname></para></listitem>
  1476. <listitem><para><methodname>removeAttrib($key)</methodname></para></listitem>
  1477. <listitem><para><methodname>clearAttribs()</methodname></para></listitem>
  1478. <listitem><para><methodname>setAction($action)</methodname></para></listitem>
  1479. <listitem><para><methodname>getAction()</methodname></para></listitem>
  1480. <listitem><para><methodname>setMethod($method)</methodname></para></listitem>
  1481. <listitem><para><methodname>getMethod()</methodname></para></listitem>
  1482. <listitem><para><methodname>setName($name)</methodname></para></listitem>
  1483. <listitem><para><methodname>getName()</methodname></para></listitem>
  1484. </itemizedlist>
  1485. </listitem>
  1486. <listitem>
  1487. <para>Elements:</para>
  1488. <itemizedlist>
  1489. <listitem>
  1490. <para>
  1491. <methodname>addElement($element, $name = null, $options
  1492. = null)</methodname>
  1493. </para>
  1494. </listitem>
  1495. <listitem>
  1496. <para><methodname>addElements(array $elements)</methodname></para>
  1497. </listitem>
  1498. <listitem>
  1499. <para><methodname>setElements(array $elements)</methodname></para>
  1500. </listitem>
  1501. <listitem><para><methodname>getElement($name)</methodname></para></listitem>
  1502. <listitem><para><methodname>getElements()</methodname></para></listitem>
  1503. <listitem><para><methodname>removeElement($name)</methodname></para></listitem>
  1504. <listitem><para><methodname>clearElements()</methodname></para></listitem>
  1505. <listitem>
  1506. <para><methodname>setDefaults(array $defaults)</methodname></para>
  1507. </listitem>
  1508. <listitem>
  1509. <para><methodname>setDefault($name, $value)</methodname></para>
  1510. </listitem>
  1511. <listitem><para><methodname>getValue($name)</methodname></para></listitem>
  1512. <listitem><para><methodname>getValues()</methodname></para></listitem>
  1513. <listitem>
  1514. <para><methodname>getUnfilteredValue($name)</methodname></para>
  1515. </listitem>
  1516. <listitem><para><methodname>getUnfilteredValues()</methodname></para></listitem>
  1517. <listitem>
  1518. <para><methodname>setElementFilters(array $filters)</methodname></para>
  1519. </listitem>
  1520. <listitem>
  1521. <para>
  1522. <methodname>setElementDecorators(array $decorators)</methodname>
  1523. </para>
  1524. </listitem>
  1525. </itemizedlist>
  1526. </listitem>
  1527. <listitem>
  1528. <para>Sub forms:</para>
  1529. <itemizedlist>
  1530. <listitem>
  1531. <para>
  1532. <methodname>addSubForm(Zend_Form $form, $name, $order
  1533. = null)</methodname>
  1534. </para>
  1535. </listitem>
  1536. <listitem>
  1537. <para><methodname>addSubForms(array $subForms)</methodname></para>
  1538. </listitem>
  1539. <listitem>
  1540. <para><methodname>setSubForms(array $subForms)</methodname></para>
  1541. </listitem>
  1542. <listitem><para><methodname>getSubForm($name)</methodname></para></listitem>
  1543. <listitem><para><methodname>getSubForms()</methodname></para></listitem>
  1544. <listitem><para><methodname>removeSubForm($name)</methodname></para></listitem>
  1545. <listitem><para><methodname>clearSubForms()</methodname></para></listitem>
  1546. <listitem>
  1547. <para>
  1548. <methodname>setSubFormDecorators(array $decorators)</methodname>
  1549. </para>
  1550. </listitem>
  1551. </itemizedlist>
  1552. </listitem>
  1553. <listitem>
  1554. <para>Display groups:</para>
  1555. <itemizedlist>
  1556. <listitem>
  1557. <para>
  1558. <methodname>addDisplayGroup(array $elements, $name, $options
  1559. = null)</methodname>
  1560. </para>
  1561. </listitem>
  1562. <listitem>
  1563. <para><methodname>addDisplayGroups(array $groups)</methodname></para>
  1564. </listitem>
  1565. <listitem>
  1566. <para><methodname>setDisplayGroups(array $groups)</methodname></para>
  1567. </listitem>
  1568. <listitem>
  1569. <para><methodname>getDisplayGroup($name)</methodname></para>
  1570. </listitem>
  1571. <listitem><para><methodname>getDisplayGroups()</methodname></para></listitem>
  1572. <listitem>
  1573. <para><methodname>removeDisplayGroup($name)</methodname></para>
  1574. </listitem>
  1575. <listitem><para><methodname>clearDisplayGroups()</methodname></para></listitem>
  1576. <listitem>
  1577. <para>
  1578. <methodname>setDisplayGroupDecorators(array $decorators)</methodname>
  1579. </para>
  1580. </listitem>
  1581. </itemizedlist>
  1582. </listitem>
  1583. <listitem>
  1584. <para>Validation</para>
  1585. <itemizedlist>
  1586. <listitem>
  1587. <para><methodname>populate(array $values)</methodname></para>
  1588. </listitem>
  1589. <listitem><para><methodname>isValid(array $data)</methodname></para></listitem>
  1590. <listitem>
  1591. <para><methodname>isValidPartial(array $data)</methodname></para>
  1592. </listitem>
  1593. <listitem>
  1594. <para><methodname>processAjax(array $data)</methodname></para>
  1595. </listitem>
  1596. <listitem><para><methodname>persistData()</methodname></para></listitem>
  1597. <listitem>
  1598. <para><methodname>getErrors($name = null)</methodname></para>
  1599. </listitem>
  1600. <listitem>
  1601. <para><methodname>getMessages($name = null)</methodname></para>
  1602. </listitem>
  1603. </itemizedlist>
  1604. </listitem>
  1605. <listitem>
  1606. <para>Rendering:</para>
  1607. <itemizedlist>
  1608. <listitem>
  1609. <para>
  1610. <methodname>setView(Zend_View_Interface $view = null)</methodname>
  1611. </para>
  1612. </listitem>
  1613. <listitem><para><methodname>getView()</methodname></para></listitem>
  1614. <listitem>
  1615. <para>
  1616. <methodname>addDecorator($decorator, $options = null)</methodname>
  1617. </para>
  1618. </listitem>
  1619. <listitem>
  1620. <para><methodname>addDecorators(array $decorators)</methodname></para>
  1621. </listitem>
  1622. <listitem>
  1623. <para><methodname>setDecorators(array $decorators)</methodname></para>
  1624. </listitem>
  1625. <listitem><para><methodname>getDecorator($name)</methodname></para></listitem>
  1626. <listitem><para><methodname>getDecorators()</methodname></para></listitem>
  1627. <listitem>
  1628. <para><methodname>removeDecorator($name)</methodname></para>
  1629. </listitem>
  1630. <listitem><para><methodname>clearDecorators()</methodname></para></listitem>
  1631. <listitem>
  1632. <para>
  1633. <methodname>render(Zend_View_Interface $view = null)</methodname>
  1634. </para>
  1635. </listitem>
  1636. </itemizedlist>
  1637. </listitem>
  1638. <listitem>
  1639. <para>I18n:</para>
  1640. <itemizedlist>
  1641. <listitem>
  1642. <para>
  1643. <methodname>setTranslator(Zend_Translate_Adapter $translator
  1644. = null)</methodname>
  1645. </para>
  1646. </listitem>
  1647. <listitem><para><methodname>getTranslator()</methodname></para></listitem>
  1648. <listitem>
  1649. <para><methodname>setDisableTranslator($flag)</methodname></para>
  1650. </listitem>
  1651. <listitem>
  1652. <para><methodname>translatorIsDisabled()</methodname></para>
  1653. </listitem>
  1654. </itemizedlist>
  1655. </listitem>
  1656. </itemizedlist>
  1657. </sect2>
  1658. <sect2 id="zend.form.forms.config">
  1659. <title>Configuration</title>
  1660. <para>
  1661. <classname>Zend_Form</classname> is fully configurable via
  1662. <methodname>setOptions()</methodname> and <methodname>setConfig()</methodname> (or by
  1663. passing options or a <classname>Zend_Config</classname> object to the
  1664. constructor). Using these methods, you can specify form elements,
  1665. display groups, decorators, and metadata.
  1666. </para>
  1667. <para>
  1668. As a general rule, if 'set' + the option key refers to a
  1669. <classname>Zend_Form</classname> method, then the value provided will be
  1670. passed to that method. If the accessor does not exist, the key is
  1671. assumed to reference an attribute, and will be passed to
  1672. <methodname>setAttrib()</methodname>.
  1673. </para>
  1674. <para>
  1675. Exceptions to the rule include the following:
  1676. </para>
  1677. <itemizedlist>
  1678. <listitem>
  1679. <para>
  1680. <property>prefixPath</property> will be passed to
  1681. <methodname>addPrefixPaths()</methodname>
  1682. </para>
  1683. </listitem>
  1684. <listitem>
  1685. <para>
  1686. <property>elementPrefixPath</property> will be passed to
  1687. <methodname>addElementPrefixPaths()</methodname>
  1688. </para>
  1689. </listitem>
  1690. <listitem>
  1691. <para>
  1692. <property>displayGroupPrefixPath</property> will be passed to
  1693. <methodname>addDisplayGroupPrefixPaths()</methodname>
  1694. </para>
  1695. </listitem>
  1696. <listitem>
  1697. <para>the following setters cannot be set in this way:</para>
  1698. <itemizedlist>
  1699. <listitem>
  1700. <para><property>setAttrib</property> (though setAttribs *will* work)</para>
  1701. </listitem>
  1702. <listitem><para><property>setConfig</property></para></listitem>
  1703. <listitem><para><property>setDefault</property></para></listitem>
  1704. <listitem><para><property>setOptions</property></para></listitem>
  1705. <listitem><para><property>setPluginLoader</property></para></listitem>
  1706. <listitem><para><property>setSubForms</property></para></listitem>
  1707. <listitem><para><property>setTranslator</property></para></listitem>
  1708. <listitem><para><property>setView</property></para></listitem>
  1709. </itemizedlist>
  1710. </listitem>
  1711. </itemizedlist>
  1712. <para>
  1713. As an example, here is a config file that passes configuration for
  1714. every type of configurable data:
  1715. </para>
  1716. <programlisting language="ini"><![CDATA[
  1717. [element]
  1718. name = "registration"
  1719. action = "/user/register"
  1720. method = "post"
  1721. attribs.class = "zend_form"
  1722. attribs.onclick = "validate(this)"
  1723. disableTranslator = 0
  1724. prefixPath.element.prefix = "My_Element"
  1725. prefixPath.element.path = "My/Element/"
  1726. elementPrefixPath.validate.prefix = "My_Validate"
  1727. elementPrefixPath.validate.path = "My/Validate/"
  1728. displayGroupPrefixPath.prefix = "My_Group"
  1729. displayGroupPrefixPath.path = "My/Group/"
  1730. elements.username.type = "text"
  1731. elements.username.options.label = "Username"
  1732. elements.username.options.validators.alpha.validator = "Alpha"
  1733. elements.username.options.filters.lcase = "StringToLower"
  1734. ; more elements, of course...
  1735. elementFilters.trim = "StringTrim"
  1736. ;elementDecorators.trim = "StringTrim"
  1737. displayGroups.login.elements.username = "username"
  1738. displayGroups.login.elements.password = "password"
  1739. displayGroupDecorators.elements.decorator = "FormElements"
  1740. displayGroupDecorators.fieldset.decorator = "Fieldset"
  1741. decorators.elements.decorator = "FormElements"
  1742. decorators.fieldset.decorator = "FieldSet"
  1743. decorators.fieldset.decorator.options.class = "zend_form"
  1744. decorators.form.decorator = "Form"
  1745. ]]></programlisting>
  1746. <para>
  1747. The above could easily be abstracted to an <acronym>XML</acronym> or
  1748. <acronym>PHP</acronym> array-based configuration file.
  1749. </para>
  1750. </sect2>
  1751. <sect2 id="zend.form.forms.custom">
  1752. <title>Custom forms</title>
  1753. <para>
  1754. An alternative to using configuration-based forms is to subclass
  1755. <classname>Zend_Form</classname>. This has several benefits:
  1756. </para>
  1757. <itemizedlist>
  1758. <listitem>
  1759. <para>
  1760. You can unit test your form easily to ensure validations and
  1761. rendering perform as expected.
  1762. </para>
  1763. </listitem>
  1764. <listitem>
  1765. <para>
  1766. Fine-grained control over individual elements.
  1767. </para>
  1768. </listitem>
  1769. <listitem>
  1770. <para>
  1771. Re-use of form objects, and greater portability (no need to
  1772. track config files).
  1773. </para>
  1774. </listitem>
  1775. <listitem>
  1776. <para>
  1777. Implementing custom functionality.
  1778. </para>
  1779. </listitem>
  1780. </itemizedlist>
  1781. <para>
  1782. The most typical use case would be to use the
  1783. <methodname>init()</methodname> method to setup specific form elements and
  1784. configuration:
  1785. </para>
  1786. <programlisting language="php"><![CDATA[
  1787. class My_Form_Login extends Zend_Form
  1788. {
  1789. public function init()
  1790. {
  1791. $username = new Zend_Form_Element_Text('username');
  1792. $username->class = 'formtext';
  1793. $username->setLabel('Username:')
  1794. ->setDecorators(array(
  1795. array('ViewHelper',
  1796. array('helper' => 'formText')),
  1797. array('Label',
  1798. array('class' => 'label'))
  1799. ));
  1800. $password = new Zend_Form_Element_Password('password');
  1801. $password->class = 'formtext';
  1802. $password->setLabel('Password:')
  1803. ->setDecorators(array(
  1804. array('ViewHelper',
  1805. array('helper' => 'formPassword')),
  1806. array('Label',
  1807. array('class' => 'label'))
  1808. ));
  1809. $submit = new Zend_Form_Element_Submit('login');
  1810. $submit->class = 'formsubmit';
  1811. $submit->setValue('Login')
  1812. ->setDecorators(array(
  1813. array('ViewHelper',
  1814. array('helper' => 'formSubmit'))
  1815. ));
  1816. $this->addElements(array(
  1817. $username,
  1818. $password,
  1819. $submit
  1820. ));
  1821. $this->setDecorators(array(
  1822. 'FormElements',
  1823. 'Fieldset',
  1824. 'Form'
  1825. ));
  1826. }
  1827. }
  1828. ]]></programlisting>
  1829. <para>
  1830. This form can then be instantiated with simply:
  1831. </para>
  1832. <programlisting language="php"><![CDATA[
  1833. $form = new My_Form_Login();
  1834. ]]></programlisting>
  1835. <para>
  1836. and all functionality is already setup and ready; no config files
  1837. needed. (Note that this example is greatly simplified, as it
  1838. contains no validators or filters for the elements.)
  1839. </para>
  1840. <para>
  1841. Another common reason for extension is to define a set of
  1842. default decorators. You can do this by overriding the
  1843. <methodname>loadDefaultDecorators()</methodname> method:
  1844. </para>
  1845. <programlisting language="php"><![CDATA[
  1846. class My_Form_Login extends Zend_Form
  1847. {
  1848. public function loadDefaultDecorators()
  1849. {
  1850. $this->setDecorators(array(
  1851. 'FormElements',
  1852. 'Fieldset',
  1853. 'Form'
  1854. ));
  1855. }
  1856. }
  1857. ]]></programlisting>
  1858. </sect2>
  1859. </sect1>
  1860. <!--
  1861. vim:se ts=4 sw=4 et:
  1862. -->