Zend_Form-Forms.xml 78 KB

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