Zend_Form-Forms.xml 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. <sect1 id="zend.form.forms">
  2. <title> 使用 Zend_Form 生成表单 </title>
  3. <para>
  4. <code>Zend_Form</code> 类用来聚合表单元素,显示组和子表单。它可以在这些条目中执行下列动作:
  5. </para>
  6. <itemizedlist>
  7. <listitem><para>
  8. 校验,包括读取错误代码和消息
  9. </para></listitem>
  10. <listitem><para>
  11. 值聚合,包括组装(populating)条目和从所有条目读取过滤的和未过滤的值
  12. </para></listitem>
  13. <listitem><para>
  14. 迭代所有条目,按它们键入的顺序或者从每个条目中读取的顺序提示来进行
  15. </para></listitem>
  16. <listitem><para>
  17. 解析(rendering)整个表单,通过执行定制解析的单个装饰器(decorator)或者迭代(iterating)表单中的所有条目
  18. </para></listitem>
  19. </itemizedlist>
  20. <para>
  21. 虽然由 <code>Zend_Form</code> 生成的表单可能复杂,可能最好的用法是做简单的表单,但是对于快速程序开发和生成原型(prototyping),它是最好的。
  22. </para>
  23. <para>
  24. 最基本的事情是实例化一个表单对象:
  25. </para>
  26. <programlisting role="php"><![CDATA[<?php
  27. // Generic form object:
  28. $form = new Zend_Form();
  29. // Custom form object:
  30. $form = new My_Form()
  31. ?>]]></programlisting>
  32. <para>
  33. 你可以可选地传递配置参数,它用来设置对象状态以及可能生成新元素:
  34. </para>
  35. <programlisting role="php"><![CDATA[<?php
  36. // Passing in configuration options:
  37. $form = new Zend_Form($config);
  38. ?>]]></programlisting>
  39. <para>
  40. <code>Zend_Form</code> 是可迭代的(iterable),它将按照它们注册的顺序和任何拥有的索引迭代所有元素、显示组和子表单。当你想手动地按适当的顺序解析(render)元素的时候,这个很有用。
  41. </para>
  42. <para>
  43. <code>Zend_Form</code> 的魔法在于它能够为元素和显示组提供工厂服务(serve as a factory),并且通过装饰器来解析(render)自己。
  44. </para>
  45. <sect2 id="zend.form.forms.plugins">
  46. <title> 插件加载器 </title>
  47. <para>
  48. <code>Zend_Form</code> 利用 <code>Zend_Loader_PluginLoader</code> 使开发者指定替代元素和装饰器的位置。每个表单有和它一起伴随的插件加载器,总访问器用来读取和修改每个表单。
  49. </para>
  50. <para>
  51. 下列加载器类型和不同的插件加载器方法 'element' 和 'decorator' 一起来使用。类型名大小写敏感。
  52. </para>
  53. <para>
  54. 用来和插件加载器结合使用的方法如下:
  55. </para>
  56. <itemizedlist>
  57. <listitem><para>
  58. <code>setPluginLoader($loader, $type)</code>: $loader 是插件加载器对象自己,type 是上述的类型之一。该方法为给定的类型设置插件加载器为新指定的加载器对象。
  59. </para></listitem>
  60. <listitem><para>
  61. <code>getPluginLoader($type)</code>: 按类型($type)来获取插件加载器。
  62. </para></listitem>
  63. <listitem><para>
  64. <code>addPrefixPath($prefix, $path, $type = null)</code>: 添加一个 prefix/path 结合对给由 $type 指定的加载器。如果 $type 是 null,通过追加前缀给 "_Element" 和 "_Decorator",它将尝试添加路径给所有的加载器,并用 "Element/" 和 "Decorator/" 追加路径。如果在通用的等级下有所有额外的表单元素类,这是个很方便的给它们设置基础前缀的方法。
  65. </para></listitem>
  66. <listitem><para>
  67. <code>addPrefixPaths(array $spec)</code>: 允许一次性添加多个路径给一个或多个插件加载器。它需要每个数组元素(item)是一个带有'path'、'prefix'和'type'键的数组。
  68. </para></listitem>
  69. </itemizedlist>
  70. <para>
  71. 另外,也可以用下列方法为所有通过<code>Zend_Form</code>实例生成元素和显示组指定前缀路径:
  72. </para>
  73. <itemizedlist>
  74. <listitem><para>
  75. <code>addElementPrefixPath($prefix, $path, $type = null)</code>: 就象 <code>addPrefixPath()</code> 一样,必需指定一个类的前缀和路径。当指定 <code>$type</code>,必需是由 <code>Zend_Form_Element</code> 指定的插件加载器类型之一,更多关于有效 <code>$type</code>值的信息参见 <link linkend="zend.form.elements.loaders">element plugins section</link>。如果没有指定 <code>$type</code>,该方法假定它是一个用于所有类型的总的前缀。
  76. </para></listitem>
  77. <listitem><para>
  78. <code>addDisplayGroupPrefixPath($prefix, $path)</code>: 就象 <code>addPrefixPath()</code> 一样,必需指定一个类的前缀和路径,然而,显示组只支持装饰器作为插件,<code>$type</code> 不是必需的。
  79. </para></listitem>
  80. </itemizedlist>
  81. <para>
  82. 定制元素和装饰器是在表单和封装定制函数之间共享功能的简便方法。在元素文档(elements documentation)中参见 <link linkend="zend.form.elements.loaders.customLabel">Custom Label example</link> ,看定制元素如何用来替换标准类。
  83. </para>
  84. </sect2>
  85. <sect2 id="zend.form.forms.elements">
  86. <title> 元素(Elements)</title>
  87. <para>
  88. <code>Zend_Form</code> 提供了若干访问器用来从表单中添加和删除元素。这些可以获得元素对象实例或以工厂模式来为实例化元素对象自己服务。
  89. </para>
  90. <para>
  91. 添加元素的最基本的方法是 <code>addElement()</code>。该方法可以或者带有 <code>Zend_Form_Element</code> (或者继承 <code>Zend_Form_Element</code> 的类 )类型对象的参数,或者用于建立新元素的参数 - 包括元素类型、名称和任何配置选项。
  92. </para>
  93. <para>
  94. 如一些例子:
  95. </para>
  96. <programlisting role="php"><![CDATA[<?php
  97. // Using an element instance:
  98. $element = new Zend_Form_Element_Text('foo');
  99. $form->addElement($element);
  100. // Using a factory
  101. //
  102. // Creates an element of type Zend_Form_Element_Text with the
  103. // name of 'foo':
  104. $form->addElement('text', 'foo');
  105. // Pass label option to the element:
  106. $form->addElement('text', 'foo', array('label' => 'Foo:'));
  107. ?>]]></programlisting>
  108. <note>
  109. <title>addElement() 实现 Fluent Interface</title>
  110. <para>
  111. <code>addElement()</code> 实现一个 fluent interface,就是说,它返回 <code>Zend_Form</code> 对象,而不是元素。这样做来允许你把多个 addElement()方法或者其它实现 fluent interface 的表单方法链接起来(所有 Zend_Form 中的设置器( setters) 都实现这个模式)。
  112. </para>
  113. <para>
  114. 如果你想返回元素,使用 <code>createElement()</code>,下面是要点。然而请注意,<code>createElement()</code> 不把元素加到表单上。
  115. </para>
  116. <para>
  117. 在内部,<code>addElement()</code> 实际上用 <code>createElement()</code> 来生成元素之后把它加到表单上。
  118. </para>
  119. </note>
  120. <para>
  121. 一旦元素被添加到表单,可以用名字来读取。通过使用 <code>getElement()</code> 方法或者通过重载(overloading)使元素作为对象属性来访问:
  122. </para>
  123. <programlisting role="php"><![CDATA[<?php
  124. // getElement():
  125. $foo = $form->getElement('foo');
  126. // As object property:
  127. $foo = $form->foo;
  128. ?>]]></programlisting>
  129. <para>
  130. 偶尔地,你只想生成一个元素并不想把它加到表单上(例如,你想利用众多的用表单注册的插件路径,但稍后把这些对象加到子表单上)。<code>createElement()</code> 方法可以完成这些:
  131. </para>
  132. <programlisting role="php"><![CDATA[<?php
  133. // $username becomes a Zend_Form_Element_Text object:
  134. $username = $form->createElement('text', 'username');
  135. ?>]]></programlisting>
  136. <sect3 id="zend.form.forms.elements.values">
  137. <title> 组装和读取数值 </title>
  138. <para>
  139. 校验表单之后,你一般需要读取它的数值以便执行其它操作,如更新数据库或通知一个 web 服务。你可以通过 <code>getValues()</code> 来读取所有元素的值,<code>getValue($name)</code> 可以通过名字来读取一个单个的值:
  140. </para>
  141. <programlisting role="php"><![CDATA[<?php
  142. // Get all values:
  143. $values = $form->getValues();
  144. // Get only 'foo' element's value:
  145. $value = $form->getValue('foo');
  146. ?>]]></programlisting>
  147. <para>
  148. 有时候,在解析之前,你想用特定的值来组装表单,可以通过 <code>setDefaults()</code> 或 <code>populate()</code> 方法来完成:
  149. </para>
  150. <programlisting role="php"><![CDATA[<?php
  151. $form->setDefaults($data);
  152. $form->populate($data);
  153. ?>]]></programlisting>
  154. <para>
  155. 在另一面,你可能想在组装和校验之前清除一个表单,可使用 <code>reset()</code> 方法来完成:
  156. </para>
  157. <programlisting role="php"><![CDATA[
  158. $form->reset();
  159. ]]></programlisting>
  160. </sect3>
  161. <sect3 id="zend.form.forms.elements.global">
  162. <title> 全局操作 </title>
  163. <para>
  164. 偶尔,你想用对所有元素进行特定的操作,一般的情形包括需要为所有元素设置插件前缀路径、装饰器和过滤器,如下例:
  165. </para>
  166. <example id="zend.form.forms.elements.global.allpaths">
  167. <title> 为所有元素设置前缀路径 </title>
  168. <para>
  169. 可以通过类型来为所有的元素设置前缀路径,或者使用全局前缀,如这些例子:
  170. </para>
  171. <programlisting role="php"><![CDATA[<?php
  172. // Set global prefix path:
  173. // Creates paths for prefixes My_Foo_Filter, My_Foo_Validate,
  174. // and My_Foo_Decorator
  175. $form->addElementPrefixPath('My_Foo', 'My/Foo/');
  176. // Just filter paths:
  177. $form->addElementPrefixPath('My_Foo_Filter', 'My/Foo/Filter', 'filter');
  178. // Just validator paths:
  179. $form->addElementPrefixPath('My_Foo_Validate', 'My/Foo/Validate', 'validate');
  180. // Just decorator paths:
  181. $form->addElementPrefixPath('My_Foo_Decorator', 'My/Foo/Decorator', 'decorator');
  182. ?>]]></programlisting>
  183. </example>
  184. <example id="zend.form.forms.elements.global.decorators">
  185. <title> 为所有元素设置装饰器(Decorators)</title>
  186. <para>
  187. 你可以为所有元素设置装饰器。<code>setElementDecorators()</code> 接受一个装饰器数组, 就象 <code>setDecorators()</code> 一样,并对每个元素重写(overwrite)先前设置的装饰器。在本例中,我们为 ViewHelper 和一个 Label 设置装饰器:
  188. </para>
  189. <programlisting role="php"><![CDATA[<?php
  190. $form->setElementDecorators(array(
  191. 'ViewHelper',
  192. 'Label'
  193. ));
  194. ?>]]></programlisting>
  195. </example>
  196. <example id="zend.form.forms.elements.global.decoratorsFilter">
  197. <title> 为某些元素设置装饰器 </title>
  198. <para>
  199. 你也可以对一个元素的子集设置装饰器,包含和排除都可以。
  200. <code>setElementDecorators()</code> 的第二个参数可以是元素名数组;
  201. 缺省地,指定这样一个数组将只对这些元素设置指定的装饰器。
  202. 你也可以传递第三个参数,用来指示是包含还是排除这个元素列表的标志。
  203. 如果是 false,它将装饰 <emphasis>除了</emphasis> 这些在列表中的元素 <emphasis>之外</emphasis>
  204. 的所有元素。使用方法的标准用法,在每个元素中,任何传递的装饰器将覆盖任何先前设置的装饰器。
  205. </para>
  206. <para>
  207. 在下面的片段中,我们只对 'foo' 和 'bar' 元素使用视图助手和标签装饰器:
  208. </para>
  209. <programlisting role="php"><![CDATA[
  210. $form->setElementDecorators(
  211. array(
  212. 'ViewHelper',
  213. 'Label'
  214. ),
  215. array(
  216. 'foo',
  217. 'bar'
  218. )
  219. );
  220. ]]></programlisting>
  221. <para>
  222. 另一方面,在这个片段中,我们对<emphasis>除了</emphasis>'foo' 元素 'bar'<emphasis>之外</emphasis>的所以元素使用视图助手和标签装饰器:
  223. </para>
  224. <programlisting role="php"><![CDATA[
  225. $form->setElementDecorators(
  226. array(
  227. 'ViewHelper',
  228. 'Label'
  229. ),
  230. array(
  231. 'foo',
  232. 'bar'
  233. ),
  234. false
  235. );
  236. ]]></programlisting>
  237. </example>
  238. <note>
  239. <title> 有些装饰器不适合某些元素 </title>
  240. <para>
  241. 虽然 <code>setElementDecorators()</code> 可能是个好方案,但有时候不会得到所期望的结果。例如,各种各样的按钮元素(Submit, Button, Reset)。当前使用标签(label)作为按钮(button)的值(value),并只使用 ViewHelper 和 DtDdWrapper 装饰器 -- 防止解析另外的标签、错误、和提示,上述例子可能重复(duplicate)一些内容(标签)。
  242. </para>
  243. <para>
  244. 你可以使用 包含/排除 数组来克服在前面例子中所述的问题。
  245. </para>
  246. <para>
  247. 所以,要灵活使用这个方法,你需要排除或手工修改一些元素的装饰器来防止不希望的输出。
  248. </para>
  249. </note>
  250. <example id="zend.form.forms.elements.global.filters">
  251. <title> 为所有元素设置过滤器 </title>
  252. <para>
  253. 大多数情况下,你想对所有元素应用同一个过滤器,一个通常的用法是 <code>修整(trim())</code> 所有的值:
  254. </para>
  255. <programlisting role="php"><![CDATA[<?php
  256. $form->setElementFilters(array('StringTrim'));
  257. ?>]]></programlisting>
  258. </example>
  259. </sect3>
  260. <sect3 id="zend.form.forms.elements.methods">
  261. <title> 和元素交互使用的方法 </title>
  262. <para>
  263. 下面的方法用来和元素交互使用:
  264. </para>
  265. <itemizedlist>
  266. <listitem><para>
  267. <code>createElement($element, $name = null, $options = null)</code>
  268. </para></listitem>
  269. <listitem><para>
  270. <code>addElement($element, $name = null, $options = null)</code>
  271. </para></listitem>
  272. <listitem><para>
  273. <code>addElements(array $elements)</code>
  274. </para></listitem>
  275. <listitem><para>
  276. <code>setElements(array $elements)</code>
  277. </para></listitem>
  278. <listitem><para>
  279. <code>getElement($name)</code>
  280. </para></listitem>
  281. <listitem><para>
  282. <code>getElements()</code>
  283. </para></listitem>
  284. <listitem><para>
  285. <code>removeElement($name)</code>
  286. </para></listitem>
  287. <listitem><para>
  288. <code>clearElements()</code>
  289. </para></listitem>
  290. <listitem><para>
  291. <code>setDefaults(array $defaults)</code>
  292. </para></listitem>
  293. <listitem><para>
  294. <code>setDefault($name, $value)</code>
  295. </para></listitem>
  296. <listitem><para>
  297. <code>getValue($name)</code>
  298. </para></listitem>
  299. <listitem><para>
  300. <code>getValues()</code>
  301. </para></listitem>
  302. <listitem><para>
  303. <code>getUnfilteredValue($name)</code>
  304. </para></listitem>
  305. <listitem><para>
  306. <code>getUnfilteredValues()</code>
  307. </para></listitem>
  308. <listitem><para>
  309. <code>setElementFilters(array $filters)</code>
  310. </para></listitem>
  311. <listitem><para>
  312. <code>setElementDecorators(array $decorators)</code>
  313. </para></listitem>
  314. <listitem><para>
  315. <code>addElementPrefixPath($prefix, $path, $type = null)</code>
  316. </para></listitem>
  317. <listitem><para>
  318. <code>addElementPrefixPaths(array $spec)</code>
  319. </para></listitem>
  320. </itemizedlist>
  321. </sect3>
  322. </sect2>
  323. <sect2 id="zend.form.forms.displaygroups">
  324. <title> 显示组(Display Groups)</title>
  325. <para>
  326. 显示组是生成要显示的虚拟元素组的一种方法。所有元素在表单中保持用名字可访问,但是当迭代或解析(rendering)所有表单的时候,任何在显示组中的元素将被一起解析(rendered)。最普通的用例是把(数据库表中的)字段组成一组元素。
  327. </para>
  328. <para>
  329. 显示组的基本类是 <code>Zend_Form_DisplayGroup</code>。虽然它可以直接实例化,但是一般最好是用 <code>Zend_Form</code> 的 <code>addDisplayGroup()</code> 方法来做。该方法带有一个元素数组作为第一个参数,和一个显示组的名称作为第二个参数。也可以传递一个选项数组或 <code>Zend_Config</code> 对象作为第三个参数。
  330. </para>
  331. <para>
  332. 假定元素 'username' 和 'password'已经在表单中,下面的代码将把它们组成一个 'login'显示组:
  333. </para>
  334. <programlisting role="php"><![CDATA[<?php
  335. $form->addDisplayGroup(array('username', 'password'), 'login');
  336. ?>]]></programlisting>
  337. <para>
  338. 你可以用 <code>getDisplayGroup()</code> 方法来访问显示组,或用显示组的名称来重载(overloading):
  339. </para>
  340. <programlisting role="php"><![CDATA[<?php
  341. // Using getDisplayGroup():
  342. $login = $form->getDisplayGroup('login');
  343. // Using overloading:
  344. $login = $form->login;
  345. ?>]]></programlisting>
  346. <note>
  347. <title> 不需要加载缺省的装饰器 </title>
  348. <para>
  349. 缺省地,缺省的装饰器在对象初始化过程中加载。你可以在生成显示组的时候通过传递 'disableLoadDefaultDecorators' 选项来关闭(disable)它。
  350. </para>
  351. <programlisting role="php"><![CDATA[<?php
  352. $form->addDisplayGroup(
  353. array('foo', 'bar'),
  354. 'foobar',
  355. array('disableLoadDefaultDecorators' => true)
  356. );
  357. ]]></programlisting>
  358. <para>
  359. 该选项可以和其它选项混合,它们都是数组或者 <code>Zend_Config</code> 对象。
  360. </para>
  361. </note>
  362. <sect3 id="zend.form.forms.displaygroups.global">
  363. <title> 全局操作 </title>
  364. <para>
  365. 就像对元素一样,一些操作可能影响所有的显示组,包括设置装饰器和寻找装饰器的插件路径。
  366. </para>
  367. <example id="zend.form.forms.displaygroups.global.paths">
  368. <title> 为所有的显示组设置装饰器前缀路径 </title>
  369. <para>
  370. 缺省地,显示组继承表单所使用的任何一个装饰器的路径,然而,如果它们在另外的位置,可以使用这个方法:<code>addDisplayGroupPrefixPath()</code>。
  371. </para>
  372. <programlisting role="php"><![CDATA[<?php
  373. $form->addDisplayGroupPrefixPath('My_Foo_Decorator', 'My/Foo/Decorator');
  374. ?>]]></programlisting>
  375. </example>
  376. <example id="zend.form.forms.displaygroups.global.decorators">
  377. <title> 为所有显示组设置装饰器 </title>
  378. <para>
  379. 你可以为所有的显示组设置装饰器。<code>setDisplayGroupDecorators()</code> 接受一个装饰器数组,就像 <code>setDecorators()</code> 一样,并将在每个显示组重写(overwrite)先前设置的装饰器。在这个例子中,我们给字段(为确保元素被迭代,FormElements 装饰器是必需的)设置装饰器:
  380. </para>
  381. <programlisting role="php"><![CDATA[<?php
  382. $form->setDisplayGroupDecorators(array(
  383. 'FormElements',
  384. 'Fieldset'
  385. ));
  386. ?>]]></programlisting>
  387. </example>
  388. </sect3>
  389. <sect3 id="zend.form.forms.displaygroups.customClasses">
  390. <title> 使用定制的显示组类 </title>
  391. <para>
  392. 缺省地,<code>Zend_Form</code> 为显示组使用 <code>Zend_Form_DisplayGroup</code> 类,你可能需要继承这个类来提供定制的功能。<code>addDisplayGroup()</code> 不允许传递一个具体的实例,但确实允许用 'displayGroupClass' 键来指定一个类来作为它的一个选项:
  393. </para>
  394. <programlisting role="php"><![CDATA[<?php
  395. // Use the 'My_DisplayGroup' class
  396. $form->addDisplayGroup(
  397. array('username', 'password'),
  398. 'user',
  399. array('displayGroupClass' => 'My_DisplayGroup')
  400. );
  401. ?>]]></programlisting>
  402. <para>
  403. 如果类没有加载,<code>Zend_Form</code> 将用 <code>Zend_Loader</code> 来加载。
  404. </para>
  405. <para>
  406. 你也可以指定一个缺省的显示组类来和表单一起使用,这样所有用这个表单对象生成的显示组将使用那个类:
  407. </para>
  408. <programlisting role="php"><![CDATA[<?php
  409. // Use the 'My_DisplayGroup' class for all display groups:
  410. $form->setDefaultDisplayGroupClass('My_DisplayGroup');
  411. ?>]]></programlisting>
  412. <para>
  413. 这个设置可能在配置中指定为 'defaultDisplayGroupClass',并在早期加载以确保所有的显示组使用那个类。
  414. </para>
  415. </sect3>
  416. <sect3 id="zend.form.forms.displaygroups.interactionmethods">
  417. <title> 和显示组交互使用的方法 </title>
  418. <para>
  419. 下列方法用来和显示组一起交互使用:
  420. </para>
  421. <itemizedlist>
  422. <listitem><para>
  423. <code>addDisplayGroup(array $elements, $name, $options = null)</code>
  424. </para></listitem>
  425. <listitem><para>
  426. <code>addDisplayGroups(array $groups)</code>
  427. </para></listitem>
  428. <listitem><para>
  429. <code>setDisplayGroups(array $groups)</code>
  430. </para></listitem>
  431. <listitem><para>
  432. <code>getDisplayGroup($name)</code>
  433. </para></listitem>
  434. <listitem><para>
  435. <code>getDisplayGroups()</code>
  436. </para></listitem>
  437. <listitem><para>
  438. <code>removeDisplayGroup($name)</code>
  439. </para></listitem>
  440. <listitem><para>
  441. <code>clearDisplayGroups()</code>
  442. </para></listitem>
  443. <listitem><para>
  444. <code>setDisplayGroupDecorators(array $decorators)</code>
  445. </para></listitem>
  446. <listitem><para>
  447. <code>addDisplayGroupPrefixPath($prefix, $path)</code>
  448. </para></listitem>
  449. <listitem><para>
  450. <code>setDefaultDisplayGroupClass($class)</code>
  451. </para></listitem>
  452. <listitem><para>
  453. <code>getDefaultDisplayGroupClass($class)</code>
  454. </para></listitem>
  455. </itemizedlist>
  456. </sect3>
  457. <sect3 id="zend.form.forms.displaygroups.methods">
  458. <title>Zend_Form_DisplayGroup 方法 </title>
  459. <para>
  460. <code>Zend_Form_DisplayGroup</code> 有下列方法,以类来分组:
  461. </para>
  462. <itemizedlist>
  463. <listitem><para>Configuration:</para>
  464. <itemizedlist>
  465. <listitem><para><code>setOptions(array $options)</code></para></listitem>
  466. <listitem><para><code>setConfig(Zend_Config $config)</code></para></listitem>
  467. </itemizedlist>
  468. </listitem>
  469. <listitem><para>Metadata:</para>
  470. <itemizedlist>
  471. <listitem><para><code>setAttrib($key, $value)</code></para></listitem>
  472. <listitem><para><code>addAttribs(array $attribs)</code></para></listitem>
  473. <listitem><para><code>setAttribs(array $attribs)</code></para></listitem>
  474. <listitem><para><code>getAttrib($key)</code></para></listitem>
  475. <listitem><para><code>getAttribs()</code></para></listitem>
  476. <listitem><para><code>removeAttrib($key)</code></para></listitem>
  477. <listitem><para><code>clearAttribs()</code></para></listitem>
  478. <listitem><para><code>setName($name)</code></para></listitem>
  479. <listitem><para><code>getName()</code></para></listitem>
  480. <listitem><para><code>setDescription($value)</code></para></listitem>
  481. <listitem><para><code>getDescription()</code></para></listitem>
  482. <listitem><para><code>setLegend($legend)</code></para></listitem>
  483. <listitem><para><code>getLegend()</code></para></listitem>
  484. <listitem><para><code>setOrder($order)</code></para></listitem>
  485. <listitem><para><code>getOrder()</code></para></listitem>
  486. </itemizedlist>
  487. </listitem>
  488. <listitem><para>Elements:</para>
  489. <itemizedlist>
  490. <listitem><para><code>createElement($type, $name, array $options = array())</code></para></listitem>
  491. <listitem><para><code>addElement($typeOrElement, $name, array $options = array())</code></para></listitem>
  492. <listitem><para><code>addElements(array $elements)</code></para></listitem>
  493. <listitem><para><code>setElements(array $elements)</code></para></listitem>
  494. <listitem><para><code>getElement($name)</code></para></listitem>
  495. <listitem><para><code>getElements()</code></para></listitem>
  496. <listitem><para><code>removeElement($name)</code></para></listitem>
  497. <listitem><para><code>clearElements()</code></para></listitem>
  498. </itemizedlist>
  499. </listitem>
  500. <listitem><para>Plugin loaders:</para>
  501. <itemizedlist>
  502. <listitem><para><code>setPluginLoader(Zend_Loader_PluginLoader $loader)</code></para></listitem>
  503. <listitem><para><code>getPluginLoader()</code></para></listitem>
  504. <listitem><para><code>addPrefixPath($prefix, $path)</code></para></listitem>
  505. <listitem><para><code>addPrefixPaths(array $spec)</code></para></listitem>
  506. </itemizedlist>
  507. </listitem>
  508. <listitem><para>Decorators:</para>
  509. <itemizedlist>
  510. <listitem><para><code>addDecorator($decorator, $options = null)</code></para></listitem>
  511. <listitem><para><code>addDecorators(array $decorators)</code></para></listitem>
  512. <listitem><para><code>setDecorators(array $decorators)</code></para></listitem>
  513. <listitem><para><code>getDecorator($name)</code></para></listitem>
  514. <listitem><para><code>getDecorators()</code></para></listitem>
  515. <listitem><para><code>removeDecorator($name)</code></para></listitem>
  516. <listitem><para><code>clearDecorators()</code></para></listitem>
  517. </itemizedlist>
  518. </listitem>
  519. <listitem><para>Rendering:</para>
  520. <itemizedlist>
  521. <listitem><para><code>setView(Zend_View_Interface $view = null)</code></para></listitem>
  522. <listitem><para><code>getView()</code></para></listitem>
  523. <listitem><para><code>render(Zend_View_Interface $view = null)</code></para></listitem>
  524. </itemizedlist>
  525. </listitem>
  526. <listitem><para>I18n:</para>
  527. <itemizedlist>
  528. <listitem><para><code>setTranslator(Zend_Translate_Adapter $translator = null)</code></para></listitem>
  529. <listitem><para><code>getTranslator()</code></para></listitem>
  530. <listitem><para><code>setDisableTranslator($flag)</code></para></listitem>
  531. <listitem><para><code>translatorIsDisabled()</code></para></listitem>
  532. </itemizedlist>
  533. </listitem>
  534. </itemizedlist>
  535. </sect3>
  536. </sect2>
  537. <sect2 id="zend.form.forms.subforms">
  538. <title> 子表单 </title>
  539. <para>
  540. 子表单服务于如干目的:
  541. </para>
  542. <itemizedlist>
  543. <listitem><para>
  544. 生成逻辑元素组。因为子表单只是个表单,你可以把它当作独立的实体来校验。
  545. </para></listitem>
  546. <listitem><para>
  547. 生成多页表单。因为子表单只是个表单,你可以在每页上显示一个子表单,建立每个子表单都有自己的校验逻辑的多页表单。只有一旦所有的子表单校验了,整个表单才算完整。
  548. </para></listitem>
  549. <listitem><para>
  550. 成组地显示。像显示组一样,当子表单作为大表单的一部分解析,可以被用做组元素。然而要小心,主表单对象不知道子表单中的元素。
  551. </para></listitem>
  552. </itemizedlist>
  553. <para>
  554. 子表单可以是 <code>Zend_Form</code> 对象,或者更一般地是 <code>Zend_Form_SubForm</code> 对象。后者包含适合包含在大表单(例如,它不解析另外的 HTML 表单标签,但解析组元素)里的装饰器。为了加上子表单,简单地把它添加到一个表单并给出名字:
  555. </para>
  556. <programlisting role="php"><![CDATA[<?php
  557. $form->addSubForm($subForm, 'subform');
  558. ?>]]></programlisting>
  559. <para>
  560. 可以用 <code>getSubForm($name)</code> 或用子表单名重载(overloading)它来读取子表单:
  561. </para>
  562. <programlisting role="php"><![CDATA[<?php
  563. // Using getSubForm():
  564. $subForm = $form->getSubForm('subform');
  565. // Using overloading:
  566. $subForm = $form->subform;
  567. ?>]]></programlisting>
  568. <para>
  569. 虽然子表单包含的元素不包含在表单迭代中,但子表单包含在其中。
  570. </para>
  571. <sect3 id="zend.form.forms.subforms.global">
  572. <title> 全局操作 </title>
  573. <para>
  574. 像元素和显示组一样,有些操作可以影响所有的子表单。然而不像显示组和元素,子表单从主表单对象继承了最多的功能,并且唯一的需要全局执行的操作是为子表单设置装饰器,<code>setSubFormDecorators()</code> 方法来完成此任务。在下一个例子中,我们将为所有作为字段(为确保迭代它的元素,FormElements 装饰器是必需的)的子表单设置装饰器:
  575. </para>
  576. <programlisting role="php"><![CDATA[<?php
  577. $form->setSubFormDecorators(array(
  578. 'FormElements',
  579. 'Fieldset'
  580. ));
  581. ?>]]></programlisting>
  582. </sect3>
  583. <sect3 id="zend.form.forms.subforms.methods">
  584. <title> 和子表单交互使用的方法 </title>
  585. <para>
  586. 下列方法用来和子表单交互使用:
  587. </para>
  588. <itemizedlist>
  589. <listitem><para>
  590. <code>addSubForm(Zend_Form $form, $name, $order = null)</code>
  591. </para></listitem>
  592. <listitem><para>
  593. <code>addSubForms(array $subForms)</code>
  594. </para></listitem>
  595. <listitem><para>
  596. <code>setSubForms(array $subForms)</code>
  597. </para></listitem>
  598. <listitem><para>
  599. <code>getSubForm($name)</code>
  600. </para></listitem>
  601. <listitem><para>
  602. <code>getSubForms()</code>
  603. </para></listitem>
  604. <listitem><para>
  605. <code>removeSubForm($name)</code>
  606. </para></listitem>
  607. <listitem><para>
  608. <code>clearSubForms()</code>
  609. </para></listitem>
  610. <listitem><para>
  611. <code>setSubFormDecorators(array $decorators)</code>
  612. </para></listitem>
  613. </itemizedlist>
  614. </sect3>
  615. </sect2>
  616. <sect2 id="zend.form.forms.metadata">
  617. <title> 元数据(Metadata)和属性(Attributes)</title>
  618. <para>
  619. 虽然表单的有效性基本源于它所包含的元素,但它也可以包含其它元数据,如名称(在 HTML 标记语言中经常用作唯一的 ID)、表单动作和方法、许多元素、组、它所包含的子表单和属性元数据(通常用做为表单标签自己设置 HTML 属性)。
  620. </para>
  621. <para>
  622. 可以使用名字访问器来设置和读取表单的名字:
  623. </para>
  624. <programlisting role="php"><![CDATA[<?php
  625. // Set the name:
  626. $form->setName('registration');
  627. // Retrieve the name:
  628. $name = $form->getName();
  629. ?>]]></programlisting>
  630. <para>
  631. 为了设置动作(到表单提交的 url )和方法(提交的方法如 'POST' 或 'GET'),使用动作和方法访问器:
  632. </para>
  633. <programlisting role="php"><![CDATA[<?php
  634. // Set the action and method:
  635. $form->setAction('/user/login')
  636. ->setMethod('post');
  637. ?>]]></programlisting>
  638. <para>
  639. 你也可以指定特别使用 enctype 访问器的表单编码类型。
  640. Zend_Form 定义了两个常量,
  641. <code>Zend_Form::ENCTYPE_URLENCODED</code> 和
  642. <code>Zend_Form::ENCTYPE_MULTIPART</code>,分别对应值为
  643. 'application/x-www-form-urlencoded' 和
  644. 'multipart/form-data';
  645. 然而,你可以把它设置为任意的编码类型。
  646. </para>
  647. <programlisting role="php"><![CDATA[<?php
  648. // Set the action, method, and enctype:
  649. $form->setAction('/user/login')
  650. ->setMethod('post')
  651. ->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
  652. ?>]]></programlisting>
  653. <note>
  654. <para>
  655. 方法、动作和 enctype 只用来内部解析,不用于任何校验。
  656. </para>
  657. </note>
  658. <para>
  659. <code>Zend_Form</code> 实现 <code>Countable</code> 接口,允许把它当作参数传递给计数器(count):
  660. </para>
  661. <programlisting role="php"><![CDATA[<?php
  662. $numItems = count($form);
  663. ?>]]></programlisting>
  664. <para>
  665. 设置任意元数据可通过属性访问器来完成。因为在 <code>Zend_Form</code> 中重载用于访问元素、显示组和子表单,这是唯一的访问元数据的方法。
  666. </para>
  667. <programlisting role="php"><![CDATA[<?php
  668. // Setting attributes:
  669. $form->setAttrib('class', 'zend-form')
  670. ->addAttribs(array(
  671. 'id' => 'registration',
  672. 'onSubmit' => 'validate(this)',
  673. ));
  674. // Retrieving attributes:
  675. $class = $form->getAttrib('class');
  676. $attribs = $form->getAttribs();
  677. // Remove an attribute:
  678. $form->removeAttrib('onSubmit');
  679. // Clear all attributes:
  680. $form->clearAttribs();
  681. ?>]]></programlisting>
  682. </sect2>
  683. <sect2 id="zend.form.forms.decorators">
  684. <title> 装饰器 </title>
  685. <para>
  686. 为表单生成 markup 通常是一件耗时的任务,特别是如果打算重复使用同一个 markup 来显示校验错误、提交的值等。<code>Zend_Form</code> 的解决办法是<emphasis> 装饰器(decorators)</emphasis>。
  687. </para>
  688. <para>
  689. <code>Zend_Form</code> 对象的装饰器可用来解析表单。FormElements 装饰器将迭代所有在表单中的条目 --元素、显示组、子表单 --并解析它们和返回结果。另外,装饰器可以用来封装内容、或追加、或预先准备它。
  690. </para>
  691. <para>
  692. <code>Zend_Form</code> 的缺省装饰器是 FormElements,HtmlTag( 封装在定义列表 )和 Form,生成它们的等同的代码如下:
  693. </para>
  694. <programlisting role="php"><![CDATA[<?php
  695. $form->setDecorators(array(
  696. 'FormElements',
  697. array('HtmlTag', array('tag' => 'dl')),
  698. 'Form'
  699. ));
  700. ?>]]></programlisting>
  701. <para>
  702. 生成输出如下:
  703. </para>
  704. <programlisting role="html"><![CDATA[
  705. <form action="/form/action" method="post">
  706. <dl>
  707. ...
  708. </dl>
  709. </form>
  710. ]]></programlisting>
  711. <para>
  712. 在表单对象中的任何属性设置将用做 <code>&lt;form&gt;</code> 标签的 HTML 属性。
  713. </para>
  714. <note>
  715. <title> 不需要加载缺省的装饰器 </title>
  716. <para>
  717. 缺省地,缺省的装饰器在对象初始化过程中加载。可通过传递 'disableLoadDefaultDecorators' 选项给构造器来关闭(disable)它:
  718. </para>
  719. <programlisting role="php"><![CDATA[<?php
  720. $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
  721. ]]></programlisting>
  722. <para>
  723. 该选项可以和其它任何选项混合,它们都是数组或在一个 <code>Zend_Config</code> 对象里。
  724. </para>
  725. </note>
  726. <note>
  727. <title> 使用同类型的多个装饰器 </title>
  728. <para>
  729. 在内部,当读取装饰器时,<code>Zend_Form</code> 使用装饰器的类做为查找机制。结果,可以注册同类型的多个装饰器,后来的装饰器将重些以前的(装饰器)。
  730. </para>
  731. <para>
  732. To get around this,可以使用别名。不用传递装饰器或装饰器名称为第一个参数给 <code>addDecorator()</code>,而是传递一个带有单个元素的、带有指向装饰器对象或名字的别名的数组:
  733. </para>
  734. <programlisting role="php"><![CDATA[<?php
  735. // Alias to 'FooBar':
  736. $form->addDecorator(array('FooBar' => 'HtmlTag'), array('tag' => 'div'));
  737. // And retrieve later:
  738. $form = $element->getDecorator('FooBar');
  739. ?>]]></programlisting>
  740. <para>
  741. 在 <code>addDecorators()</code> 和 <code>setDecorators()</code> 方法中,需要传递在表示装饰器的数组中的 'decorator' 选项:
  742. </para>
  743. <programlisting role="php"><![CDATA[<?php
  744. // Add two 'HtmlTag' decorators, aliasing one to 'FooBar':
  745. $form->addDecorators(
  746. array('HtmlTag', array('tag' => 'div')),
  747. array(
  748. 'decorator' => array('FooBar' => 'HtmlTag'),
  749. 'options' => array('tag' => 'dd')
  750. ),
  751. );
  752. // And retrieve later:
  753. $htmlTag = $form->getDecorator('HtmlTag');
  754. $fooBar = $form->getDecorator('FooBar');
  755. ?>]]></programlisting>
  756. </note>
  757. <para>
  758. 为生成表单,你可以生成自己的装饰器。一个通常的用例是如果你知道额外的你想用的 HTML,你的装饰器可以潜在地使用从独立的元素或显示组来的装饰器生成额外的 HTML 并返回它。
  759. </para>
  760. <para>
  761. 下列方法可以用来和装饰器交互使用:
  762. </para>
  763. <itemizedlist>
  764. <listitem><para>
  765. <code>addDecorator($decorator, $options = null)</code>
  766. </para></listitem>
  767. <listitem><para>
  768. <code>addDecorators(array $decorators)</code>
  769. </para></listitem>
  770. <listitem><para>
  771. <code>setDecorators(array $decorators)</code>
  772. </para></listitem>
  773. <listitem><para>
  774. <code>getDecorator($name)</code>
  775. </para></listitem>
  776. <listitem><para>
  777. <code>getDecorators()</code>
  778. </para></listitem>
  779. <listitem><para>
  780. <code>removeDecorator($name)</code>
  781. </para></listitem>
  782. <listitem><para>
  783. <code>clearDecorators()</code>
  784. </para></listitem>
  785. </itemizedlist>
  786. </sect2>
  787. <sect2 id="zend.form.forms.validation">
  788. <title> 校验 </title>
  789. <para>
  790. 表单的一个基本用例是校验提交的数据。<code>Zend_Form</code> 让你一次性校验整个表单或部分表单,还有对 XmlHttpRequests (AJAX)的自动校验响应。如果提交的数据无效,它有一些方法用来为元素和子表单校验失败而读取各种各样的错误代码和消息。
  791. </para>
  792. <para>
  793. 为了校验整个表单,使用 <code>isValid()</code> 方法:
  794. </para>
  795. <programlisting role="php"><![CDATA[<?php
  796. if (!$form->isValid($_POST)) {
  797. // failed validation
  798. }
  799. ?>]]></programlisting>
  800. <para>
  801. <code>isValid()</code> 校验每个必需的元素和任何包括在提交的数据中的非必需的元素。
  802. </para>
  803. <para>
  804. 有时候你可能只需要校验数据的一个子集,可以使用 <code>isValidPartial($data)</code>:
  805. </para>
  806. <programlisting role="php"><![CDATA[<?php
  807. if (!$form->isValidPartial($data)) {
  808. // failed validation
  809. }
  810. ?>]]></programlisting>
  811. <para>
  812. <code>isValidPartial()</code> 只尝试校验那些数据中匹配的元素,如果某个元素不在数据中,那就跳过。
  813. </para>
  814. <para>
  815. 当为 AJAX 请求校验元素或元素组,你一般要校验表单的一个子集,并想要响应返回到 JSON。用 <code>processAjax()</code> 正好:
  816. </para>
  817. <programlisting role="php"><![CDATA[<?php
  818. $json = $form->processAjax($data);
  819. ?>]]></programlisting>
  820. <para>
  821. 你可以发送 JSON 响应到客户端。如果表单有效,这将是个布尔 true 响应,如果表单无效,则是个包含 key/message 对的 javascript 对象,这里,每个 'message' 是校验错误消息的数组。
  822. </para>
  823. <para>
  824. 对于校验失败的表单,你可以分别使用 <code>getErrors()</code> 和 <code>getMessages()</code> 获取错误代码和错误消息:
  825. </para>
  826. <programlisting role="php"><![CDATA[<?php
  827. $codes = $form->getErrors();
  828. $messages = $form->getMessage();
  829. ?>]]></programlisting>
  830. <note>
  831. <para>
  832. 因为由 <code>getMessages()</code> 返回的消息是 error code/message 对的数组,一般不需要 <code>getErrors()</code>。
  833. </para>
  834. </note>
  835. <para>
  836. 你可以通过传递元素名为单个元素来获取代码和错误消息;
  837. </para>
  838. <programlisting role="php"><![CDATA[<?php
  839. $codes = $form->getErrors('username');
  840. $messages = $form->getMessages('username');
  841. ?>]]></programlisting>
  842. <note>
  843. <para>
  844. 注意:当校验元素时,<code>Zend_Form</code> 发送第二个参数给每个元素的 <code>isValid()</code> 方法:被校验的数据的数组。当确定数据和合法性时,单个的校验器可以用这个来让它们来利用其它提交的值。一个典型的例子是注册表单,密码和密码确认都是必需的;密码元素可以使用密码确认做为它的校验的一部分。
  845. </para>
  846. </note>
  847. <sect3 id="zend.form.forms.validation.errors">
  848. <title> 定制错误消息 </title>
  849. <para>
  850. 有时,你想定制一条或多条特定的错误消息来替代由附加到元素上的校验器所带的错误消息。
  851. 另外,有时候你想自己标记表单无效,从 1.6.0 版开始,通过下列方法来实现这个功能。
  852. </para>
  853. <itemizedlist>
  854. <listitem><para>
  855. <code>addErrorMessage($message)</code>: 添加一条来显示当校验失败时的错误消息。
  856. 可以多次调用,新消息就追加到堆栈。
  857. </para></listitem>
  858. <listitem><para>
  859. <code>addErrorMessages(array $messages)</code>: 添加多条错误消息来显示校验错误。
  860. </para></listitem>
  861. <listitem><para>
  862. <code>setErrorMessages(array $messages)</code>: 添加多条错误消息来显示校验错误,并覆盖先前的错误消息。
  863. </para></listitem>
  864. <listitem><para>
  865. <code>getErrorMessages()</code>: 读取已定义的定制的错误消息列表。
  866. </para></listitem>
  867. <listitem><para>
  868. <code>clearErrorMessages()</code>: 删除已定义的定制的错误消息。
  869. </para></listitem>
  870. <listitem><para>
  871. <code>markAsError()</code>: 标记表单已经有失败的校验。
  872. </para></listitem>
  873. <listitem><para>
  874. <code>addError($message)</code>: 添加一条消息给定制错误消息栈并标记表单无效。
  875. </para></listitem>
  876. <listitem><para>
  877. <code>addErrors(array $messages)</code>: 添加数条消息给定制错误消息栈并标记表单无效。
  878. </para></listitem>
  879. <listitem><para>
  880. <code>setErrors(array $messages)</code>: 覆盖定制的错误消息堆栈并标记表单无效。
  881. </para></listitem>
  882. </itemizedlist>
  883. <para>
  884. 所有用这个方式设置的错误可以被翻译。
  885. 另外,你可以插入占位符 "%value%" 来表示元素的值;当读取到错误消息时,这个当前元素值将被替换。
  886. </para>
  887. </sect3>
  888. </sect2>
  889. <sect2 id="zend.form.forms.methods">
  890. <title> 方法 </title>
  891. <para>
  892. 下面是 <code>Zend_Form</code> 的方法大全,按类分组:
  893. </para>
  894. <itemizedlist>
  895. <listitem><para> 配置和选项:</para>
  896. <itemizedlist>
  897. <listitem><para><code>setOptions(array $options)</code></para></listitem>
  898. <listitem><para><code>setConfig(Zend_Config $config)</code></para></listitem>
  899. </itemizedlist>
  900. </listitem>
  901. <listitem><para> 插件加载器和路径:</para>
  902. <itemizedlist>
  903. <listitem><para><code>setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)</code></para></listitem>
  904. <listitem><para><code>getPluginLoader($type = null)</code></para></listitem>
  905. <listitem><para><code>addPrefixPath($prefix, $path, $type = null) </code></para></listitem>
  906. <listitem><para><code>addPrefixPaths(array $spec)</code></para></listitem>
  907. <listitem><para><code>addElementPrefixPath($prefix, $path, $type = null)</code></para></listitem>
  908. <listitem><para><code>addElementPrefixPaths(array $spec)</code></para></listitem>
  909. <listitem><para><code>addDisplayGroupPrefixPath($prefix, $path)</code></para></listitem>
  910. </itemizedlist>
  911. </listitem>
  912. <listitem><para> 元数据: </para>
  913. <itemizedlist>
  914. <listitem><para><code>setAttrib($key, $value)</code></para></listitem>
  915. <listitem><para><code>addAttribs(array $attribs)</code></para></listitem>
  916. <listitem><para><code>setAttribs(array $attribs)</code></para></listitem>
  917. <listitem><para><code>getAttrib($key)</code></para></listitem>
  918. <listitem><para><code>getAttribs()</code></para></listitem>
  919. <listitem><para><code>removeAttrib($key)</code></para></listitem>
  920. <listitem><para><code>clearAttribs()</code></para></listitem>
  921. <listitem><para><code>setAction($action)</code></para></listitem>
  922. <listitem><para><code>getAction()</code></para></listitem>
  923. <listitem><para><code>setMethod($method)</code></para></listitem>
  924. <listitem><para><code>getMethod()</code></para></listitem>
  925. <listitem><para><code>setName($name)</code></para></listitem>
  926. <listitem><para><code>getName()</code></para></listitem>
  927. </itemizedlist>
  928. </listitem>
  929. <listitem><para> 元素:</para>
  930. <itemizedlist>
  931. <listitem><para><code>addElement($element, $name = null, $options = null)</code></para></listitem>
  932. <listitem><para><code>addElements(array $elements)</code></para></listitem>
  933. <listitem><para><code>setElements(array $elements)</code></para></listitem>
  934. <listitem><para><code>getElement($name)</code></para></listitem>
  935. <listitem><para><code>getElements()</code></para></listitem>
  936. <listitem><para><code>removeElement($name)</code></para></listitem>
  937. <listitem><para><code>clearElements()</code></para></listitem>
  938. <listitem><para><code>setDefaults(array $defaults)</code></para></listitem>
  939. <listitem><para><code>setDefault($name, $value)</code></para></listitem>
  940. <listitem><para><code>getValue($name)</code></para></listitem>
  941. <listitem><para><code>getValues()</code></para></listitem>
  942. <listitem><para><code>getUnfilteredValue($name)</code></para></listitem>
  943. <listitem><para><code>getUnfilteredValues()</code></para></listitem>
  944. <listitem><para><code>setElementFilters(array $filters)</code></para></listitem>
  945. <listitem><para><code>setElementDecorators(array $decorators)</code></para></listitem>
  946. </itemizedlist>
  947. </listitem>
  948. <listitem><para> 子表单:</para>
  949. <itemizedlist>
  950. <listitem><para><code>addSubForm(Zend_Form $form, $name, $order = null)</code></para></listitem>
  951. <listitem><para><code>addSubForms(array $subForms)</code></para></listitem>
  952. <listitem><para><code>setSubForms(array $subForms)</code></para></listitem>
  953. <listitem><para><code>getSubForm($name)</code></para></listitem>
  954. <listitem><para><code>getSubForms()</code></para></listitem>
  955. <listitem><para><code>removeSubForm($name)</code></para></listitem>
  956. <listitem><para><code>clearSubForms()</code></para></listitem>
  957. <listitem><para><code>setSubFormDecorators(array $decorators)</code></para></listitem>
  958. </itemizedlist>
  959. </listitem>
  960. <listitem><para> 显示组:</para>
  961. <itemizedlist>
  962. <listitem><para><code>addDisplayGroup(array $elements, $name, $options = null)</code></para></listitem>
  963. <listitem><para><code>addDisplayGroups(array $groups)</code></para></listitem>
  964. <listitem><para><code>setDisplayGroups(array $groups)</code></para></listitem>
  965. <listitem><para><code>getDisplayGroup($name)</code></para></listitem>
  966. <listitem><para><code>getDisplayGroups()</code></para></listitem>
  967. <listitem><para><code>removeDisplayGroup($name)</code></para></listitem>
  968. <listitem><para><code>clearDisplayGroups()</code></para></listitem>
  969. <listitem><para><code>setDisplayGroupDecorators(array $decorators)</code></para></listitem>
  970. </itemizedlist>
  971. </listitem>
  972. <listitem><para> 校验 </para>
  973. <itemizedlist>
  974. <listitem><para><code>populate(array $values)</code></para></listitem>
  975. <listitem><para><code>isValid(array $data)</code></para></listitem>
  976. <listitem><para><code>isValidPartial(array $data)</code></para></listitem>
  977. <listitem><para><code>processAjax(array $data)</code></para></listitem>
  978. <listitem><para><code>persistData()</code></para></listitem>
  979. <listitem><para><code>getErrors($name = null)</code></para></listitem>
  980. <listitem><para><code>getMessages($name = null)</code></para></listitem>
  981. </itemizedlist>
  982. </listitem>
  983. <listitem><para> 解析:</para>
  984. <itemizedlist>
  985. <listitem><para><code>setView(Zend_View_Interface $view = null)</code></para></listitem>
  986. <listitem><para><code>getView()</code></para></listitem>
  987. <listitem><para><code>addDecorator($decorator, $options = null)</code></para></listitem>
  988. <listitem><para><code>addDecorators(array $decorators)</code></para></listitem>
  989. <listitem><para><code>setDecorators(array $decorators)</code></para></listitem>
  990. <listitem><para><code>getDecorator($name)</code></para></listitem>
  991. <listitem><para><code>getDecorators()</code></para></listitem>
  992. <listitem><para><code>removeDecorator($name)</code></para></listitem>
  993. <listitem><para><code>clearDecorators()</code></para></listitem>
  994. <listitem><para><code>render(Zend_View_Interface $view = null)</code></para></listitem>
  995. </itemizedlist>
  996. </listitem>
  997. <listitem><para>I18n:</para>
  998. <itemizedlist>
  999. <listitem><para><code>setTranslator(Zend_Translate_Adapter $translator = null)</code></para></listitem>
  1000. <listitem><para><code>getTranslator()</code></para></listitem>
  1001. <listitem><para><code>setDisableTranslator($flag)</code></para></listitem>
  1002. <listitem><para><code>translatorIsDisabled()</code></para></listitem>
  1003. </itemizedlist>
  1004. </listitem>
  1005. </itemizedlist>
  1006. </sect2>
  1007. <sect2 id="zend.form.forms.config">
  1008. <title> 配置 </title>
  1009. <para>
  1010. <code>Zend_Form</code> 可以通过 <code>setOptions()</code> 和 <code>setConfig()</code> (或者通过传递选项或 <code>Zend_Config</code> 对象 ) 进行全面配置。使用这些方法,你可以指定表单元素、显示组、装饰器和元数据。
  1011. </para>
  1012. <para>
  1013. 作为通用的规则,如果 'set' + 选项键涉及 <code>Zend_Form</code> 方法,那么提供的值将传递给那个方法。
  1014. 如果访问器不存在,键被假定引用一个属性并将传递给 <code>setAttrib()</code> 。
  1015. </para>
  1016. <para>
  1017. 规则的异常包括下列这些:
  1018. </para>
  1019. <itemizedlist>
  1020. <listitem><para>
  1021. <code>prefixPaths</code> 将传递给 <code>addPrefixPaths()</code>
  1022. </para></listitem>
  1023. <listitem><para>
  1024. <code>elementPrefixPaths</code> 将传递给 <code>addElementPrefixPaths()</code>
  1025. </para></listitem>
  1026. <listitem><para>
  1027. <code>displayGroupPrefixPaths</code> 将传递给 <code>addDisplayGroupPrefixPaths()</code>
  1028. </para></listitem>
  1029. <listitem>
  1030. <para> 下列设置器不能用这个方法设置:</para>
  1031. <itemizedlist>
  1032. <listitem><para><code>setAttrib (though setAttribs *will* work)</code></para></listitem>
  1033. <listitem><para><code>setConfig</code></para></listitem>
  1034. <listitem><para><code>setDefault</code></para></listitem>
  1035. <listitem><para><code>setOptions</code></para></listitem>
  1036. <listitem><para><code>setPluginLoader</code></para></listitem>
  1037. <listitem><para><code>setSubForms</code></para></listitem>
  1038. <listitem><para><code>setTranslator</code></para></listitem>
  1039. <listitem><para><code>setView</code></para></listitem>
  1040. </itemizedlist>
  1041. </listitem>
  1042. </itemizedlist>
  1043. <para>
  1044. 作为一个例子,这里是一个为每个可配置数据传递配置的配置文件:
  1045. </para>
  1046. <programlisting role="ini"><![CDATA[
  1047. [element]
  1048. name = "registration"
  1049. action = "/user/register"
  1050. method = "post"
  1051. attribs.class = "zend_form"
  1052. attribs.onclick = "validate(this)"
  1053. disableTranslator = 0
  1054. prefixPath.element.prefix = "My_Element"
  1055. prefixPath.element.path = "My/Element/"
  1056. elementPrefixPath.validate.prefix = "My_Validate"
  1057. elementPrefixPath.validate.path = "My/Validate/"
  1058. displayGroupPrefixPath.prefix = "My_Group"
  1059. displayGroupPrefixPath.path = "My/Group/"
  1060. elements.username.type = "text"
  1061. elements.username.options.label = "Username"
  1062. elements.username.options.validators.alpha.validator = "Alpha"
  1063. elements.username.options.filters.lcase = "StringToLower"
  1064. ; more elements, of course...
  1065. elementFilters.trim = "StringTrim"
  1066. ;elementDecorators.trim = "StringTrim"
  1067. displayGroups.login.elements.username = "username"
  1068. displayGroups.login.elements.password = "password"
  1069. displayGroupDecorators.elements.decorator = "FormElements"
  1070. displayGroupDecorators.fieldset.decorator = "Fieldset"
  1071. decorators.elements.decorator = "FormElements"
  1072. decorators.fieldset.decorator = "FieldSet"
  1073. decorators.fieldset.decorator.options.class = "zend_form"
  1074. decorators.form.decorator = "Form"
  1075. ?>]]></programlisting>
  1076. <para>
  1077. 上述可以很容易被抽象成 XML 或 PHP 基于数组的配置文件。
  1078. </para>
  1079. </sect2>
  1080. <sect2 id="zend.form.forms.custom">
  1081. <title> 定制表单 </title>
  1082. <para>
  1083. 一个使用基于配置的表单的替代方法是继承 <code>Zend_Form</code> 类,有若干优点:
  1084. </para>
  1085. <itemizedlist>
  1086. <listitem><para>
  1087. 可以容易进行单元测试来确保校验和解析如愿执行。
  1088. </para></listitem>
  1089. <listitem><para>
  1090. 精细地控制每个元素。
  1091. </para></listitem>
  1092. <listitem><para>
  1093. 重使用表单对象,最大化可移植性(不需要跟踪配置文件)。
  1094. </para></listitem>
  1095. <listitem><para>
  1096. 实现定制功能。
  1097. </para></listitem>
  1098. </itemizedlist>
  1099. <para>
  1100. 最典型的用例是使用 <code>init()</code> 方法来设置指定的表单元素和配置:
  1101. </para>
  1102. <programlisting role="php"><![CDATA[<?php
  1103. class My_Form_Login extends Zend_Form
  1104. {
  1105. public function init()
  1106. {
  1107. $username = new Zend_Form_Element_Text('username');
  1108. $username->class = 'formtext';
  1109. $username->setLabel('Username:')
  1110. ->setDecorators(array(
  1111. array('ViewHelper', array('helper' => 'formText')),
  1112. array('Label', array('class' => 'label'))
  1113. ));
  1114. $password = new Zend_Form_Element_Password('password');
  1115. $password->class = 'formtext';
  1116. $password->setLabel('Username:')
  1117. ->setDecorators(array(
  1118. array('ViewHelper', array('helper' => 'formPassword')),
  1119. array('Label', array('class' => 'label'))
  1120. ));
  1121. $submit = new Zend_Form_Element_Submit('login');
  1122. $submit->class = 'formsubmit';
  1123. $submit->setValue('Login')
  1124. ->setDecorators(array(
  1125. array('ViewHelper', array('helper' => 'formSubmit'))
  1126. ));
  1127. $this->addElements(array(
  1128. $username,
  1129. $password,
  1130. $submit
  1131. ));
  1132. $this->setDecorators(array(
  1133. 'FormElements',
  1134. 'Fieldset',
  1135. 'Form'
  1136. ));
  1137. }
  1138. }
  1139. ?>]]></programlisting>
  1140. <para>
  1141. 这个表单可以简单地实例化:
  1142. </para>
  1143. <programlisting role="php"><![CDATA[<?php
  1144. $form = new My_Form_Login();
  1145. ?>]]></programlisting>
  1146. <para>
  1147. 所有的功能已经设置并准备好了,不需要配置文件。(注意这个例子非常简化,因为它没有为元素包含校验器和过滤器。)
  1148. </para>
  1149. <para>
  1150. 另一个普通的扩展原因是定义一组缺省的装饰器,可以通过覆盖(overriding)<code>loadDefaultDecorators()</code> 方法来完成:
  1151. </para>
  1152. <programlisting role="php"><![CDATA[<?php
  1153. class My_Form_Login extends Zend_Form
  1154. {
  1155. public function loadDefaultDecorators()
  1156. {
  1157. $this->setDecorators(array(
  1158. 'FormElements',
  1159. 'Fieldset',
  1160. 'Form'
  1161. ));
  1162. }
  1163. }
  1164. ]]></programlisting>
  1165. </sect2>
  1166. </sect1>
  1167. <!--
  1168. vim:se ts=4 sw=4 et:
  1169. -->