Zend_Form-Elements.xml 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.form.elements">
  4. <title>Creating Form Elements Using Zend_Form_Element</title>
  5. <para>
  6. A form is made of elements that typically correspond to HTML form
  7. input. <classname>Zend_Form_Element</classname> encapsulates single form elements, with the
  8. following areas of responsibility:
  9. </para>
  10. <itemizedlist>
  11. <listitem>
  12. <para>
  13. validation (is submitted data valid?)
  14. </para>
  15. <itemizedlist>
  16. <listitem><para>capturing of validation error codes and messages</para></listitem>
  17. </itemizedlist>
  18. </listitem>
  19. <listitem><para>
  20. filtering (how is the element escaped or normalized prior to
  21. validation and/or for output?)
  22. </para></listitem>
  23. <listitem><para>
  24. rendering (how is the element displayed?)
  25. </para></listitem>
  26. <listitem><para>
  27. metadata and attributes (what information further qualifies the element?)
  28. </para></listitem>
  29. </itemizedlist>
  30. <para>
  31. The base class, <classname>Zend_Form_Element</classname>, has reasonable defaults
  32. for many cases, but it is best to extend the class for commonly used
  33. special purpose elements. Additionally, Zend Framework ships with a
  34. number of standard XHTML elements; you can read about them <link
  35. linkend="zend.form.standardElements">in the Standard Elements
  36. chapter</link>.
  37. </para>
  38. <sect2 id="zend.form.elements.loaders">
  39. <title>Plugin Loaders</title>
  40. <para>
  41. <classname>Zend_Form_Element</classname> makes use of <link
  42. linkend="zend.loader.pluginloader">Zend_Loader_PluginLoader</link>
  43. to allow developers to specify locations of alternate validators,
  44. filters, and decorators. Each has its own plugin loader associated
  45. with it, and general accessors are used to retrieve and modify
  46. each.
  47. </para>
  48. <para>
  49. The following loader types are used with the various plugin loader
  50. methods: 'validate', 'filter', and 'decorator'. The type names are
  51. case insensitive.
  52. </para>
  53. <para>
  54. The methods used to interact with plugin loaders are as follows:
  55. </para>
  56. <itemizedlist>
  57. <listitem><para>
  58. <code>setPluginLoader($loader, $type)</code>:
  59. <code>$loader</code> is the plugin loader object itself, while
  60. <code>$type</code> is one of the types specified above. This
  61. sets the plugin loader for the given type to the newly
  62. specified loader object.
  63. </para></listitem>
  64. <listitem><para>
  65. <code>getPluginLoader($type)</code>: retrieves the plugin
  66. loader associated with <code>$type</code>.
  67. </para></listitem>
  68. <listitem><para>
  69. <code>addPrefixPath($prefix, $path, $type = null)</code>: adds
  70. a prefix/path association to the loader specified by
  71. <code>$type</code>. If <code>$type</code> is null, it will
  72. attempt to add the path to all loaders, by appending the prefix
  73. with each of "_Validate", "_Filter", and "_Decorator"; and
  74. appending the path with "Validate/", "Filter/", and
  75. "Decorator/". If you have all your extra form element classes
  76. under a common hierarchy, this is a convenience method for
  77. setting the base prefix for them.
  78. </para></listitem>
  79. <listitem><para>
  80. <code>addPrefixPaths(array $spec)</code>: allows you to add
  81. many paths at once to one or more plugin loaders. It expects
  82. each array item to be an array with the keys 'path', 'prefix',
  83. and 'type'.
  84. </para></listitem>
  85. </itemizedlist>
  86. <para>
  87. Custom validators, filters, and decorators are an easy way to share
  88. functionality between forms and to encapsulate custom functionality.
  89. </para>
  90. <example id="zend.form.elements.loaders.customLabel">
  91. <title>Custom Label</title>
  92. <para>
  93. One common use case for plugins is to provide replacements for
  94. standard classes. For instance, if you want to provide a
  95. different implementation of the 'Label' decorator -- for
  96. instance, to always append a colon -- you could create your own
  97. 'Label' decorator with your own class prefix, and then add it to
  98. your prefix path.
  99. </para>
  100. <para>
  101. Let's start with a custom Label decorator. We'll give it the
  102. class prefix "My_Decorator", and the class itself will be in the
  103. file "My/Decorator/Label.php".
  104. </para>
  105. <programlisting language="php"><![CDATA[
  106. class My_Decorator_Label extends Zend_Form_Decorator_Abstract
  107. {
  108. protected $_placement = 'PREPEND';
  109. public function render($content)
  110. {
  111. if (null === ($element = $this->getElement())) {
  112. return $content;
  113. }
  114. if (!method_exists($element, 'getLabel')) {
  115. return $content;
  116. }
  117. $label = $element->getLabel() . ':';
  118. if (null === ($view = $element->getView())) {
  119. return $this->renderLabel($content, $label);
  120. }
  121. $label = $view->formLabel($element->getName(), $label);
  122. return $this->renderLabel($content, $label);
  123. }
  124. public function renderLabel($content, $label)
  125. {
  126. $placement = $this->getPlacement();
  127. $separator = $this->getSeparator();
  128. switch ($placement) {
  129. case 'APPEND':
  130. return $content . $separator . $label;
  131. case 'PREPEND':
  132. default:
  133. return $label . $separator . $content;
  134. }
  135. }
  136. }
  137. ]]></programlisting>
  138. <para>
  139. Now we can tell the element to use this plugin path when looking
  140. for decorators:
  141. </para>
  142. <programlisting language="php"><![CDATA[
  143. $element->addPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');
  144. ]]></programlisting>
  145. <para>
  146. Alternately, we can do that at the form level to ensure all
  147. decorators use this path:
  148. </para>
  149. <programlisting language="php"><![CDATA[
  150. $form->addElementPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');
  151. ]]></programlisting>
  152. <para>
  153. After it added as in the example above, the 'My/Decorator/' path will be searched
  154. first to see if the decorator exists there when you add a decorator. As a result,
  155. 'My_Decorator_Label' will now be used when the 'Label' decorator is requested.
  156. </para>
  157. </example>
  158. </sect2>
  159. <sect2 id="zend.form.elements.filters">
  160. <title>Filters</title>
  161. <para>
  162. It's often useful and/or necessary to perform some normalization on
  163. input prior to validation. For example, you may want to strip out
  164. all HTML, but run your validations on what remains to ensure the
  165. submission is valid. Or you may want to trim empty space surrounding input so that a
  166. StringLength validator will use the correct length of the input without counting leading
  167. or trailing whitespace characters. These operations may be performed using
  168. <classname>Zend_Filter</classname>. <classname>Zend_Form_Element</classname> has support
  169. for filter chains, allowing you to specify multiple, sequential filters. Filtering
  170. happens both during validation and when you retrieve the element value via
  171. <code>getValue()</code>:
  172. </para>
  173. <programlisting language="php"><![CDATA[
  174. $filtered = $element->getValue();
  175. ]]></programlisting>
  176. <para>
  177. Filters may be added to the chain in two ways:
  178. </para>
  179. <itemizedlist>
  180. <listitem><para>
  181. passing in a concrete filter instance
  182. </para></listitem>
  183. <listitem><para>
  184. providing a filter name – either a short name or fully
  185. qualified class name
  186. </para></listitem>
  187. </itemizedlist>
  188. <para>
  189. Let's see some examples:
  190. </para>
  191. <programlisting language="php"><![CDATA[
  192. // Concrete filter instance:
  193. $element->addFilter(new Zend_Filter_Alnum());
  194. // Fully qualified class name:
  195. $element->addFilter('Zend_Filter_Alnum');
  196. // Short filter name:
  197. $element->addFilter('Alnum');
  198. $element->addFilter('alnum');
  199. ]]></programlisting>
  200. <para>
  201. Short names are typically the filter name minus the prefix. In the
  202. default case, this will mean minus the 'Zend_Filter_' prefix.
  203. The first letter can be upper-cased or lower-cased.
  204. </para>
  205. <note>
  206. <title>Using Custom Filter Classes</title>
  207. <para>
  208. If you have your own set of filter classes, you can tell
  209. <classname>Zend_Form_Element</classname> about these using
  210. <code>addPrefixPath()</code>. For instance, if you have
  211. filters under the 'My_Filter' prefix, you can tell
  212. <classname>Zend_Form_Element</classname> about this as follows:
  213. </para>
  214. <programlisting language="php"><![CDATA[
  215. $element->addPrefixPath('My_Filter', 'My/Filter/', 'filter');
  216. ]]></programlisting>
  217. <para>
  218. (Recall that the third argument indicates which plugin loader
  219. on which to perform the action.)
  220. </para>
  221. </note>
  222. <para>
  223. If at any time you need the unfiltered value, use the
  224. <code>getUnfilteredValue()</code> method:
  225. </para>
  226. <programlisting language="php"><![CDATA[
  227. $unfiltered = $element->getUnfilteredValue();
  228. ]]></programlisting>
  229. <para>
  230. For more information on filters, see the <link
  231. linkend="zend.filter.introduction">Zend_Filter
  232. documentation</link>.
  233. </para>
  234. <para>
  235. Methods associated with filters include:
  236. </para>
  237. <itemizedlist>
  238. <listitem><para>
  239. <code>addFilter($nameOfFilter, array $options = null)</code>
  240. </para></listitem>
  241. <listitem><para>
  242. <code>addFilters(array $filters)</code>
  243. </para></listitem>
  244. <listitem><para>
  245. <code>setFilters(array $filters)</code> (overwrites all filters)
  246. </para></listitem>
  247. <listitem><para>
  248. <code>getFilter($name)</code> (retrieve a filter object by name)
  249. </para></listitem>
  250. <listitem><para>
  251. <code>getFilters()</code> (retrieve all filters)
  252. </para></listitem>
  253. <listitem><para>
  254. <code>removeFilter($name)</code> (remove filter by name)
  255. </para></listitem>
  256. <listitem><para>
  257. <code>clearFilters()</code> (remove all filters)
  258. </para></listitem>
  259. </itemizedlist>
  260. </sect2>
  261. <sect2 id="zend.form.elements.validators">
  262. <title>Validators</title>
  263. <para>
  264. If you subscribe to the security mantra of "filter input, escape
  265. output," you'll should use validator to filter input submitted with your form.
  266. In <classname>Zend_Form</classname>, each element includes its own validator
  267. chain, consisting of <classname>Zend_Validate_*</classname> validators.
  268. </para>
  269. <para>
  270. Validators may be added to the chain in two ways:
  271. </para>
  272. <itemizedlist>
  273. <listitem><para>
  274. passing in a concrete validator instance
  275. </para></listitem>
  276. <listitem><para>
  277. providing a validator name – either a short name or fully
  278. qualified class name
  279. </para></listitem>
  280. </itemizedlist>
  281. <para>
  282. Let's see some examples:
  283. </para>
  284. <programlisting language="php"><![CDATA[
  285. // Concrete validator instance:
  286. $element->addValidator(new Zend_Validate_Alnum());
  287. // Fully qualified class name:
  288. $element->addValidator('Zend_Validate_Alnum');
  289. // Short validator name:
  290. $element->addValidator('Alnum');
  291. $element->addValidator('alnum');
  292. ]]></programlisting>
  293. <para>
  294. Short names are typically the validator name minus the prefix. In
  295. the default case, this will mean minus the 'Zend_Validate_' prefix.
  296. As is the case with filters, the first letter can be upper-cased or lower-cased.
  297. </para>
  298. <note>
  299. <title>Using Custom Validator Classes</title>
  300. <para>
  301. If you have your own set of validator classes, you can tell
  302. <classname>Zend_Form_Element</classname> about these using
  303. <code>addPrefixPath()</code>. For instance, if you have
  304. validators under the 'My_Validator' prefix, you can tell
  305. <classname>Zend_Form_Element</classname> about this as follows:
  306. </para>
  307. <programlisting language="php"><![CDATA[
  308. $element->addPrefixPath('My_Validator', 'My/Validator/', 'validate');
  309. ]]></programlisting>
  310. <para>
  311. (Recall that the third argument indicates which plugin loader
  312. on which to perform the action.)
  313. </para>
  314. </note>
  315. <para>
  316. If failing a particular validation should prevent later validators
  317. from firing, pass boolean <constant>TRUE</constant> as the second parameter:
  318. </para>
  319. <programlisting language="php"><![CDATA[
  320. $element->addValidator('alnum', true);
  321. ]]></programlisting>
  322. <para>
  323. If you are using a string name to add a validator, and the
  324. validator class accepts arguments to the constructor, you may pass
  325. these to the third parameter of <code>addValidator()</code> as an
  326. array:
  327. </para>
  328. <programlisting language="php"><![CDATA[
  329. $element->addValidator('StringLength', false, array(6, 20));
  330. ]]></programlisting>
  331. <para>
  332. Arguments passed in this way should be in the order in which they
  333. are defined in the constructor. The above example will instantiate
  334. the <classname>Zend_Validate_StringLenth</classname> class with its
  335. <code>$min</code> and <code>$max</code> parameters:
  336. </para>
  337. <programlisting language="php"><![CDATA[
  338. $validator = new Zend_Validate_StringLength(6, 20);
  339. ]]></programlisting>
  340. <note>
  341. <title>Providing Custom Validator Error Messages</title>
  342. <para>
  343. Some developers may wish to provide custom error messages for a
  344. validator. The <code>$options</code> argument of the
  345. <classname>Zend_Form_Element::addValidator()</classname> method allows you to do so
  346. by providing the key 'messages' and mapping it to an array of key/value pairs
  347. for setting the message templates. You will need to know the
  348. error codes of the various validation error types for the
  349. particular validator.
  350. </para>
  351. <para>
  352. A better option is to use a <classname>Zend_Translate_Adapter</classname>
  353. with your form. Error codes are automatically passed to the
  354. adapter by the default Errors decorator; you can then specify
  355. your own error message strings by setting up translations for
  356. the various error codes of your validators.
  357. </para>
  358. </note>
  359. <para>
  360. You can also set many validators at once, using
  361. <code>addValidators()</code>. The basic usage is to pass an array
  362. of arrays, with each array containing 1 to 3 values, matching the
  363. constructor of <code>addValidator()</code>:
  364. </para>
  365. <programlisting language="php"><![CDATA[
  366. $element->addValidators(array(
  367. array('NotEmpty', true),
  368. array('alnum'),
  369. array('stringLength', false, array(6, 20)),
  370. ));
  371. ]]></programlisting>
  372. <para>
  373. If you want to be more verbose or explicit, you can use the array
  374. keys 'validator', 'breakChainOnFailure', and 'options':
  375. </para>
  376. <programlisting language="php"><![CDATA[
  377. $element->addValidators(array(
  378. array(
  379. 'validator' => 'NotEmpty',
  380. 'breakChainOnFailure' => true),
  381. array('validator' => 'alnum'),
  382. array(
  383. 'validator' => 'stringLength',
  384. 'options' => array(6, 20)),
  385. ));
  386. ]]></programlisting>
  387. <para>
  388. This usage is good for illustrating how you could then configure
  389. validators in a config file:
  390. </para>
  391. <programlisting language="ini"><![CDATA[
  392. element.validators.notempty.validator = "NotEmpty"
  393. element.validators.notempty.breakChainOnFailure = true
  394. element.validators.alnum.validator = "Alnum"
  395. element.validators.strlen.validator = "StringLength"
  396. element.validators.strlen.options.min = 6
  397. element.validators.strlen.options.max = 20
  398. ]]></programlisting>
  399. <para>
  400. Notice that every item has a key, whether or not it needs one; this
  401. is a limitation of using configuration files -- but it also helps
  402. make explicit what the arguments are for. Just remember that any
  403. validator options must be specified in order.
  404. </para>
  405. <para>
  406. To validate an element, pass the value to
  407. <code>isValid()</code>:
  408. </para>
  409. <programlisting language="php"><![CDATA[
  410. if ($element->isValid($value)) {
  411. // valid
  412. } else {
  413. // invalid
  414. }
  415. ]]></programlisting>
  416. <note>
  417. <title>Validation Operates On Filtered Values</title>
  418. <para>
  419. <classname>Zend_Form_Element::isValid()</classname> filters values through
  420. the provided filter chain prior to validation. See <link
  421. linkend="zend.form.elements.filters">the Filters
  422. section</link> for more information.
  423. </para>
  424. </note>
  425. <note>
  426. <title>Validation Context</title>
  427. <para>
  428. <classname>Zend_Form_Element::isValid()</classname> supports an
  429. additional argument, <code>$context</code>.
  430. <classname>Zend_Form::isValid()</classname> passes the entire array of
  431. data being processed to <code>$context</code> when validating a
  432. form, and <classname>Zend_Form_Element::isValid()</classname>, in turn,
  433. passes it to each validator. This means you can write
  434. validators that are aware of data passed to other form
  435. elements. As an example, consider a standard registration form
  436. that has fields for both password and a password confirmation;
  437. one validation would be that the two fields match. Such a
  438. validator might look like the following:
  439. </para>
  440. <programlisting language="php"><![CDATA[
  441. class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
  442. {
  443. const NOT_MATCH = 'notMatch';
  444. protected $_messageTemplates = array(
  445. self::NOT_MATCH => 'Password confirmation does not match'
  446. );
  447. public function isValid($value, $context = null)
  448. {
  449. $value = (string) $value;
  450. $this->_setValue($value);
  451. if (is_array($context)) {
  452. if (isset($context['password_confirm'])
  453. && ($value == $context['password_confirm']))
  454. {
  455. return true;
  456. }
  457. } elseif (is_string($context) && ($value == $context)) {
  458. return true;
  459. }
  460. $this->_error(self::NOT_MATCH);
  461. return false;
  462. }
  463. }
  464. ]]></programlisting>
  465. </note>
  466. <para>
  467. Validators are processed in order. Each validator is processed,
  468. unless a validator created with a true
  469. <code>breakChainOnFailure</code> value fails its validation. Be
  470. sure to specify your validators in a reasonable order.
  471. </para>
  472. <para>
  473. After a failed validation, you can retrieve the error codes and
  474. messages from the validator chain:
  475. </para>
  476. <programlisting language="php"><![CDATA[
  477. $errors = $element->getErrors();
  478. $messages = $element->getMessages();
  479. ]]></programlisting>
  480. <para>
  481. (Note: error messages returned are an associative array of error
  482. code / error message pairs.)
  483. </para>
  484. <para>
  485. In addition to validators, you can specify that an element is
  486. required, using <code>setRequired(true)</code>. By default, this
  487. flag is false, meaning that your validator chain will be skipped if
  488. no value is passed to <code>isValid()</code>. You can modify this
  489. behavior in a number of ways:
  490. </para>
  491. <itemizedlist>
  492. <listitem>
  493. <para>
  494. By default, when an element is required, a flag,
  495. 'allowEmpty', is also true. This means that if a value
  496. evaluating to empty is passed to <code>isValid()</code>, the
  497. validators will be skipped. You can toggle this flag using
  498. the accessor <code>setAllowEmpty($flag)</code>; when the
  499. flag is false and a value is passed, the validators
  500. will still run.
  501. </para>
  502. </listitem>
  503. <listitem>
  504. <para>
  505. By default, if an element is required but does not contain
  506. a 'NotEmpty' validator, <code>isValid()</code> will add one
  507. to the top of the stack, with the
  508. <code>breakChainOnFailure</code> flag set. This behavior lends
  509. required flag semantic meaning: if no value is passed,
  510. we immediately invalidate the submission and notify the
  511. user, and prevent other validators from running on what we
  512. already know is invalid data.
  513. </para>
  514. <para>
  515. If you do not want this behavior, you can turn it off by
  516. passing a false value to
  517. <code>setAutoInsertNotEmptyValidator($flag)</code>; this
  518. will prevent <code>isValid()</code> from placing the
  519. 'NotEmpty' validator in the validator chain.
  520. </para>
  521. </listitem>
  522. </itemizedlist>
  523. <para>
  524. For more information on validators, see the <link
  525. linkend="zend.validate.introduction">Zend_Validate
  526. documentation</link>.
  527. </para>
  528. <note>
  529. <title>Using Zend_Form_Elements as general-purpose validators</title>
  530. <para>
  531. <classname>Zend_Form_Element</classname> implements
  532. <classname>Zend_Validate_Interface</classname>, meaning an element may
  533. also be used as a validator in other, non-form related
  534. validation chains.
  535. </para>
  536. </note>
  537. <para>
  538. Methods associated with validation include:
  539. </para>
  540. <itemizedlist>
  541. <listitem><para>
  542. <code>setRequired($flag)</code> and
  543. <code>isRequired()</code> allow you to set and retrieve the
  544. status of the 'required' flag. When set to boolean <constant>TRUE</constant>,
  545. this flag requires that the element be in the data processed by
  546. <classname>Zend_Form</classname>.
  547. </para></listitem>
  548. <listitem><para>
  549. <code>setAllowEmpty($flag)</code> and
  550. <code>getAllowEmpty()</code> allow you to modify the
  551. behaviour of optional elements (i.e., elements where the
  552. required flag is false). When the 'allow empty' flag is
  553. true, empty values will not be passed to the validator
  554. chain.
  555. </para></listitem>
  556. <listitem><para>
  557. <code>setAutoInsertNotEmptyValidator($flag)</code> allows
  558. you to specify whether or not a 'NotEmpty' validator will be
  559. prepended to the validator chain when the element is
  560. required. By default, this flag is true.
  561. </para></listitem>
  562. <listitem><para>
  563. <code>addValidator($nameOrValidator, $breakChainOnFailure = false, array $options =
  564. null)</code>
  565. </para></listitem>
  566. <listitem><para>
  567. <code>addValidators(array $validators)</code>
  568. </para></listitem>
  569. <listitem><para>
  570. <code>setValidators(array $validators)</code> (overwrites all validators)
  571. </para></listitem>
  572. <listitem><para>
  573. <code>getValidator($name)</code> (retrieve a validator object by name)
  574. </para></listitem>
  575. <listitem><para>
  576. <code>getValidators()</code> (retrieve all validators)
  577. </para></listitem>
  578. <listitem><para>
  579. <code>removeValidator($name)</code> (remove validator by name)
  580. </para></listitem>
  581. <listitem><para>
  582. <code>clearValidators()</code> (remove all validators)
  583. </para></listitem>
  584. </itemizedlist>
  585. <sect3 id="zend.form.elements.validators.errors">
  586. <title>Custom Error Messages</title>
  587. <para>
  588. At times, you may want to specify one or more specific error
  589. messages to use instead of the error messages generated by the
  590. validators attached to your element. Additionally, at times you
  591. may want to mark the element invalid yourself. As of 1.6.0, this
  592. functionality is possible via the following methods.
  593. </para>
  594. <itemizedlist>
  595. <listitem><para>
  596. <code>addErrorMessage($message)</code>: add an error message
  597. to display on form validation errors. You may call this more
  598. than once, and new messages are appended to the stack.
  599. </para></listitem>
  600. <listitem><para>
  601. <code>addErrorMessages(array $messages)</code>: add multiple
  602. error messages to display on form validation errors.
  603. </para></listitem>
  604. <listitem><para>
  605. <code>setErrorMessages(array $messages)</code>: add multiple
  606. error messages to display on form validation errors,
  607. overwriting all previously set error messages.
  608. </para></listitem>
  609. <listitem><para>
  610. <code>getErrorMessages()</code>: retrieve the list of
  611. custom error messages that have been defined.
  612. </para></listitem>
  613. <listitem><para>
  614. <code>clearErrorMessages()</code>: remove all custom error
  615. messages that have been defined.
  616. </para></listitem>
  617. <listitem><para>
  618. <code>markAsError()</code>: mark the element as having
  619. failed validation.
  620. </para></listitem>
  621. <listitem><para>
  622. <code>hasErrors()</code>: determine whether the element has
  623. either failed validation or been marked as invalid.
  624. </para></listitem>
  625. <listitem><para>
  626. <code>addError($message)</code>: add a message to the custom
  627. error messages stack and flag the element as invalid.
  628. </para></listitem>
  629. <listitem><para>
  630. <code>addErrors(array $messages)</code>: add several
  631. messages to the custom error messages stack and flag the
  632. element as invalid.
  633. </para></listitem>
  634. <listitem><para>
  635. <code>setErrors(array $messages)</code>: overwrite the
  636. custom error messages stack with the provided messages and
  637. flag the element as invalid.
  638. </para></listitem>
  639. </itemizedlist>
  640. <para>
  641. All errors set in this fashion may be translated. Additionally,
  642. you may insert the placeholder "%value%" to represent the
  643. element value; this current element value will be substituted
  644. when the error messages are retrieved.
  645. </para>
  646. </sect3>
  647. </sect2>
  648. <sect2 id="zend.form.elements.decorators">
  649. <title>Decorators</title>
  650. <para>
  651. One particular pain point for many web developers is the creation
  652. of the XHTML forms themselves. For each element, the developer
  653. needs to create markup for the element itself (typically a label)
  654. and special markup for displaying
  655. validation error messages. The more elements on the page, the less
  656. trivial this task becomes.
  657. </para>
  658. <para>
  659. <classname>Zend_Form_Element</classname> tries to solve this issue through
  660. the use of "decorators". Decorators are simply classes that have
  661. access to the element and a method for rendering content. For more
  662. information on how decorators work, please see the section on <link
  663. linkend="zend.form.decorators">Zend_Form_Decorator</link>.
  664. </para>
  665. <para>
  666. The default decorators used by <classname>Zend_Form_Element</classname> are:
  667. </para>
  668. <itemizedlist>
  669. <listitem><para>
  670. <emphasis>ViewHelper</emphasis>: specifies a view helper to use
  671. to render the element. The 'helper' element attribute can be
  672. used to specify which view helper to use. By default,
  673. <classname>Zend_Form_Element</classname> specifies the 'formText' view
  674. helper, but individual subclasses specify different helpers.
  675. </para></listitem>
  676. <listitem><para>
  677. <emphasis>Errors</emphasis>: appends error messages to the
  678. element using <classname>Zend_View_Helper_FormErrors</classname>. If none are
  679. present, nothing is appended.
  680. </para></listitem>
  681. <listitem><para>
  682. <emphasis>Description</emphasis>: appends the element
  683. description. If none is present, nothing is appended. By
  684. default, the description is rendered in a &lt;p&gt; tag with a
  685. class of 'description'.
  686. </para></listitem>
  687. <listitem><para>
  688. <emphasis>HtmlTag</emphasis>: wraps the element and errors in
  689. an HTML &lt;dd&gt; tag.
  690. </para></listitem>
  691. <listitem><para>
  692. <emphasis>Label</emphasis>: prepends a label to the element
  693. using <classname>Zend_View_Helper_FormLabel</classname>, and wraps it in a
  694. &lt;dt&gt; tag. If no label is provided, just the definition term tag is
  695. rendered.
  696. </para></listitem>
  697. </itemizedlist>
  698. <note>
  699. <title>Default Decorators Do Not Need to Be Loaded</title>
  700. <para>
  701. By default, the default decorators are loaded during object
  702. initialization. You can disable this by passing the
  703. 'disableLoadDefaultDecorators' option to the constructor:
  704. </para>
  705. <programlisting language="php"><![CDATA[
  706. $element = new Zend_Form_Element('foo',
  707. array('disableLoadDefaultDecorators' =>
  708. true)
  709. );
  710. ]]></programlisting>
  711. <para>
  712. This option may be mixed with any other options you pass,
  713. both as array options or in a <classname>Zend_Config</classname> object.
  714. </para>
  715. </note>
  716. <para>
  717. Since the order in which decorators are registered matters- the first
  718. decorator registered is executed first- you will need to make
  719. sure you register your decorators in an appropriate order, or
  720. ensure that you set the placement options in a sane fashion. To
  721. give an example, here is the code that registers the default
  722. decorators:
  723. </para>
  724. <programlisting language="php"><![CDATA[
  725. $this->addDecorators(array(
  726. array('ViewHelper'),
  727. array('Errors'),
  728. array('Description', array('tag' => 'p', 'class' => 'description')),
  729. array('HtmlTag', array('tag' => 'dd')),
  730. array('Label', array('tag' => 'dt')),
  731. ));
  732. ]]></programlisting>
  733. <para>
  734. The initial content is created by the 'ViewHelper' decorator, which
  735. creates the form element itself. Next, the 'Errors' decorator
  736. fetches error messages from the element, and, if any are present,
  737. passes them to the 'FormErrors' view helper to render. If a
  738. description is present, the 'Description' decorator will append a
  739. paragraph of class 'description' containing the descriptive text to
  740. the aggregated content. The next decorator, 'HtmlTag', wraps the
  741. element, errors, and description in an HTML &lt;dd&gt; tag.
  742. Finally, the last decorator, 'label', retrieves the element's label
  743. and passes it to the 'FormLabel' view helper, wrapping it in an HTML
  744. &lt;dt&gt; tag; the value is prepended to the content by default.
  745. The resulting output looks basically like this:
  746. </para>
  747. <programlisting language="html"><![CDATA[
  748. <dt><label for="foo" class="optional">Foo</label></dt>
  749. <dd>
  750. <input type="text" name="foo" id="foo" value="123" />
  751. <ul class="errors">
  752. <li>"123" is not an alphanumeric value</li>
  753. </ul>
  754. <p class="description">
  755. This is some descriptive text regarding the element.
  756. </p>
  757. </dd>
  758. ]]></programlisting>
  759. <para>
  760. For more information on decorators, read the <link
  761. linkend="zend.form.decorators">Zend_Form_Decorator
  762. section</link>.
  763. </para>
  764. <note>
  765. <title>Using Multiple Decorators of the Same Type</title>
  766. <para>
  767. Internally, <classname>Zend_Form_Element</classname> uses a decorator's
  768. class as the lookup mechanism when retrieving decorators. As a
  769. result, you cannot register multiple decorators of the same
  770. type; subsequent decorators will simply overwrite those that
  771. existed before.
  772. </para>
  773. <para>
  774. To get around this, you can use <emphasis>aliases</emphasis>.
  775. Instead of passing a decorator or decorator name as the first
  776. argument to <code>addDecorator()</code>, pass an array with a
  777. single element, with the alias pointing to the decorator object
  778. or name:
  779. </para>
  780. <programlisting language="php"><![CDATA[
  781. // Alias to 'FooBar':
  782. $element->addDecorator(array('FooBar' => 'HtmlTag'),
  783. array('tag' => 'div'));
  784. // And retrieve later:
  785. $decorator = $element->getDecorator('FooBar');
  786. ]]></programlisting>
  787. <para>
  788. In the <code>addDecorators()</code> and
  789. <code>setDecorators()</code> methods, you will need to pass
  790. the 'decorator' option in the array representing the decorator:
  791. </para>
  792. <programlisting language="php"><![CDATA[
  793. // Add two 'HtmlTag' decorators, aliasing one to 'FooBar':
  794. $element->addDecorators(
  795. array('HtmlTag', array('tag' => 'div')),
  796. array(
  797. 'decorator' => array('FooBar' => 'HtmlTag'),
  798. 'options' => array('tag' => 'dd')
  799. ),
  800. );
  801. // And retrieve later:
  802. $htmlTag = $element->getDecorator('HtmlTag');
  803. $fooBar = $element->getDecorator('FooBar');
  804. ]]></programlisting>
  805. </note>
  806. <para>
  807. Methods associated with decorators include:
  808. </para>
  809. <itemizedlist>
  810. <listitem><para>
  811. <code>addDecorator($nameOrDecorator, array $options = null)</code>
  812. </para></listitem>
  813. <listitem><para>
  814. <code>addDecorators(array $decorators)</code>
  815. </para></listitem>
  816. <listitem><para>
  817. <code>setDecorators(array $decorators)</code> (overwrites all decorators)
  818. </para></listitem>
  819. <listitem><para>
  820. <code>getDecorator($name)</code> (retrieve a decorator object by name)
  821. </para></listitem>
  822. <listitem><para>
  823. <code>getDecorators()</code> (retrieve all decorators)
  824. </para></listitem>
  825. <listitem><para>
  826. <code>removeDecorator($name)</code> (remove decorator by name)
  827. </para></listitem>
  828. <listitem><para>
  829. <code>clearDecorators()</code> (remove all decorators)
  830. </para></listitem>
  831. </itemizedlist>
  832. <para>
  833. <classname>Zend_Form_Element</classname> also uses overloading to allow rendering
  834. specific decorators. <code>__call()</code> will intercept methods
  835. that lead with the text 'render' and use the remainder of the method
  836. name to lookup a decorator; if found, it will then render that
  837. <emphasis>single</emphasis> decorator. Any arguments passed to the
  838. method call will be used as content to pass to the decorator's
  839. <code>render()</code> method. As an example:
  840. </para>
  841. <programlisting language="php"><![CDATA[
  842. // Render only the ViewHelper decorator:
  843. echo $element->renderViewHelper();
  844. // Render only the HtmlTag decorator, passing in content:
  845. echo $element->renderHtmlTag("This is the html tag content");
  846. ]]></programlisting>
  847. <para>
  848. If the decorator does not exist, an exception is raised.
  849. </para>
  850. </sect2>
  851. <sect2 id="zend.form.elements.metadata">
  852. <title>Metadata and Attributes</title>
  853. <para>
  854. <classname>Zend_Form_Element</classname> handles a variety of attributes and
  855. element metadata. Basic attributes include:
  856. </para>
  857. <itemizedlist>
  858. <listitem><para>
  859. <emphasis>name</emphasis>: the element name. Uses the
  860. <code>setName()</code> and <code>getName()</code> accessors.
  861. </para></listitem>
  862. <listitem><para>
  863. <emphasis>label</emphasis>: the element label. Uses the
  864. <code>setLabel()</code> and <code>getLabel()</code> accessors.
  865. </para></listitem>
  866. <listitem><para>
  867. <emphasis>order</emphasis>: the index at which an element
  868. should appear in the form. Uses the <code>setOrder()</code> and
  869. <code>getOrder()</code> accessors.
  870. </para></listitem>
  871. <listitem><para>
  872. <emphasis>value</emphasis>: the current element value. Uses the
  873. <code>setValue()</code> and <code>getValue()</code> accessors.
  874. </para></listitem>
  875. <listitem><para>
  876. <emphasis>description</emphasis>: a description of the element;
  877. often used to provide tooltip or javascript contextual hinting
  878. describing the purpose of the element. Uses the
  879. <code>setDescription()</code> and <code>getDescription()</code>
  880. accessors.
  881. </para></listitem>
  882. <listitem><para>
  883. <emphasis>required</emphasis>: flag indicating whether or not
  884. the element is required when performing form validation. Uses
  885. the <code>setRequired()</code> and <code>getRequired()</code>
  886. accessors. This flag is false by default.
  887. </para></listitem>
  888. <listitem><para>
  889. <emphasis>allowEmpty</emphasis>: flag indicating whether or not
  890. a non-required (optional) element should attempt to validate
  891. empty values. If it is set to true and the required flag is false, empty
  892. values are not passed to the validator chain and are presumed true.
  893. Uses the <code>setAllowEmpty()</code> and <code>getAllowEmpty()</code>
  894. accessors. This flag is true by default.
  895. </para></listitem>
  896. <listitem><para>
  897. <emphasis>autoInsertNotEmptyValidator</emphasis>: flag
  898. indicating whether or not to insert a 'NotEmpty' validator when
  899. the element is required. By default, this flag is true. Set the
  900. flag with <code>setAutoInsertNotEmptyValidator($flag)</code> and
  901. determine the value with
  902. <code>autoInsertNotEmptyValidator()</code>.
  903. </para></listitem>
  904. </itemizedlist>
  905. <para>
  906. Form elements may require additional metadata. For XHTML form
  907. elements, for instance, you may want to specify attributes such as
  908. the class or id. To facilitate this are a set of accessors:
  909. </para>
  910. <itemizedlist>
  911. <listitem><para>
  912. <emphasis>setAttrib($name, $value)</emphasis>: add an attribute
  913. </para></listitem>
  914. <listitem><para>
  915. <emphasis>setAttribs(array $attribs)</emphasis>: like
  916. addAttribs(), but overwrites
  917. </para></listitem>
  918. <listitem><para>
  919. <emphasis>getAttrib($name)</emphasis>: retrieve a single
  920. attribute value
  921. </para></listitem>
  922. <listitem><para>
  923. <emphasis>getAttribs()</emphasis>: retrieve all attributes as
  924. key/value pairs
  925. </para></listitem>
  926. </itemizedlist>
  927. <para>
  928. Most of the time, however, you can simply access them as object
  929. properties, as <classname>Zend_Form_Element</classname> utilizes overloading
  930. to facilitate access to them:
  931. </para>
  932. <programlisting language="php"><![CDATA[
  933. // Equivalent to $element->setAttrib('class', 'text'):
  934. $element->class = 'text;
  935. ]]></programlisting>
  936. <para>
  937. By default, all attributes are passed to the view helper used by
  938. the element during rendering, and rendered as HTML attributes of
  939. the element tag.
  940. </para>
  941. </sect2>
  942. <sect2 id="zend.form.elements.standard">
  943. <title>Standard Elements</title>
  944. <para>
  945. <classname>Zend_Form</classname> ships with a number of standard elements; please read
  946. the <link linkend="zend.form.standardElements">Standard Elements</link>
  947. chapter for full details.
  948. </para>
  949. </sect2>
  950. <sect2 id="zend.form.elements.methods">
  951. <title>Zend_Form_Element Methods</title>
  952. <para>
  953. <classname>Zend_Form_Element</classname> has many, many methods. What follows
  954. is a quick summary of their signatures, grouped by type:
  955. </para>
  956. <itemizedlist>
  957. <listitem><para>Configuration:</para>
  958. <itemizedlist>
  959. <listitem><para><code>setOptions(array $options)</code></para></listitem>
  960. <listitem><para><code>setConfig(Zend_Config $config)</code></para></listitem>
  961. </itemizedlist>
  962. </listitem>
  963. <listitem><para>I18n:</para>
  964. <itemizedlist>
  965. <listitem>
  966. <para>
  967. <code>setTranslator(Zend_Translate_Adapter $translator = null)</code>
  968. </para>
  969. </listitem>
  970. <listitem><para><code>getTranslator()</code></para></listitem>
  971. <listitem><para><code>setDisableTranslator($flag)</code></para></listitem>
  972. <listitem><para><code>translatorIsDisabled()</code></para></listitem>
  973. </itemizedlist>
  974. </listitem>
  975. <listitem><para>Properties:</para>
  976. <itemizedlist>
  977. <listitem><para><code>setName($name)</code></para></listitem>
  978. <listitem><para><code>getName()</code></para></listitem>
  979. <listitem><para><code>setValue($value)</code></para></listitem>
  980. <listitem><para><code>getValue()</code></para></listitem>
  981. <listitem><para><code>getUnfilteredValue()</code></para></listitem>
  982. <listitem><para><code>setLabel($label)</code></para></listitem>
  983. <listitem><para><code>getLabel()</code></para></listitem>
  984. <listitem><para><code>setDescription($description)</code></para></listitem>
  985. <listitem><para><code>getDescription()</code></para></listitem>
  986. <listitem><para><code>setOrder($order)</code></para></listitem>
  987. <listitem><para><code>getOrder()</code></para></listitem>
  988. <listitem><para><code>setRequired($flag)</code></para></listitem>
  989. <listitem><para><code>getRequired()</code></para></listitem>
  990. <listitem><para><code>setAllowEmpty($flag)</code></para></listitem>
  991. <listitem><para><code>getAllowEmpty()</code></para></listitem>
  992. <listitem>
  993. <para><code>setAutoInsertNotEmptyValidator($flag)</code></para>
  994. </listitem>
  995. <listitem><para><code>autoInsertNotEmptyValidator()</code></para></listitem>
  996. <listitem><para><code>setIgnore($flag)</code></para></listitem>
  997. <listitem><para><code>getIgnore()</code></para></listitem>
  998. <listitem><para><code>getType()</code></para></listitem>
  999. <listitem><para><code>setAttrib($name, $value)</code></para></listitem>
  1000. <listitem><para><code>setAttribs(array $attribs)</code></para></listitem>
  1001. <listitem><para><code>getAttrib($name)</code></para></listitem>
  1002. <listitem><para><code>getAttribs()</code></para></listitem>
  1003. </itemizedlist>
  1004. </listitem>
  1005. <listitem><para>Plugin loaders and paths:</para>
  1006. <itemizedlist>
  1007. <listitem>
  1008. <para>
  1009. <code>setPluginLoader(Zend_Loader_PluginLoader_Interface $loader,
  1010. $type)</code>
  1011. </para>
  1012. </listitem>
  1013. <listitem><para><code>getPluginLoader($type)</code></para></listitem>
  1014. <listitem>
  1015. <para><code>addPrefixPath($prefix, $path, $type = null)</code></para>
  1016. </listitem>
  1017. <listitem><para><code>addPrefixPaths(array $spec)</code></para></listitem>
  1018. </itemizedlist>
  1019. </listitem>
  1020. <listitem><para>Validation:</para>
  1021. <itemizedlist>
  1022. <listitem>
  1023. <para>
  1024. <code>addValidator($validator, $breakChainOnFailure = false, $options =
  1025. array())</code>
  1026. </para>
  1027. </listitem>
  1028. <listitem><para><code>addValidators(array $validators)</code></para></listitem>
  1029. <listitem><para><code>setValidators(array $validators)</code></para></listitem>
  1030. <listitem><para><code>getValidator($name)</code></para></listitem>
  1031. <listitem><para><code>getValidators()</code></para></listitem>
  1032. <listitem><para><code>removeValidator($name)</code></para></listitem>
  1033. <listitem><para><code>clearValidators()</code></para></listitem>
  1034. <listitem><para><code>isValid($value, $context = null)</code></para></listitem>
  1035. <listitem><para><code>getErrors()</code></para></listitem>
  1036. <listitem><para><code>getMessages()</code></para></listitem>
  1037. </itemizedlist>
  1038. </listitem>
  1039. <listitem><para>Filters:</para>
  1040. <itemizedlist>
  1041. <listitem>
  1042. <para><code>addFilter($filter, $options = array())</code></para>
  1043. </listitem>
  1044. <listitem><para><code>addFilters(array $filters)</code></para></listitem>
  1045. <listitem><para><code>setFilters(array $filters)</code></para></listitem>
  1046. <listitem><para><code>getFilter($name)</code></para></listitem>
  1047. <listitem><para><code>getFilters()</code></para></listitem>
  1048. <listitem><para><code>removeFilter($name)</code></para></listitem>
  1049. <listitem><para><code>clearFilters()</code></para></listitem>
  1050. </itemizedlist>
  1051. </listitem>
  1052. <listitem><para>Rendering:</para>
  1053. <itemizedlist>
  1054. <listitem>
  1055. <para><code>setView(Zend_View_Interface $view = null)</code></para>
  1056. </listitem>
  1057. <listitem><para><code>getView()</code></para></listitem>
  1058. <listitem>
  1059. <para><code>addDecorator($decorator, $options = null)</code></para>
  1060. </listitem>
  1061. <listitem><para><code>addDecorators(array $decorators)</code></para></listitem>
  1062. <listitem><para><code>setDecorators(array $decorators)</code></para></listitem>
  1063. <listitem><para><code>getDecorator($name)</code></para></listitem>
  1064. <listitem><para><code>getDecorators()</code></para></listitem>
  1065. <listitem><para><code>removeDecorator($name)</code></para></listitem>
  1066. <listitem><para><code>clearDecorators()</code></para></listitem>
  1067. <listitem>
  1068. <para><code>render(Zend_View_Interface $view = null)</code></para>
  1069. </listitem>
  1070. </itemizedlist>
  1071. </listitem>
  1072. </itemizedlist>
  1073. </sect2>
  1074. <sect2 id="zend.form.elements.config">
  1075. <title>Configuration</title>
  1076. <para>
  1077. <classname>Zend_Form_Element</classname>'s constructor accepts either an
  1078. array of options or a <classname>Zend_Config</classname> object containing
  1079. options, and it can also be configured using either
  1080. <code>setOptions()</code> or <code>setConfig()</code>. Generally
  1081. speaking, keys are named as follows:
  1082. </para>
  1083. <itemizedlist>
  1084. <listitem><para>
  1085. If 'set' + key refers to a <classname>Zend_Form_Element</classname>
  1086. method, then the value provided will be passed to that method.
  1087. </para></listitem>
  1088. <listitem><para>
  1089. Otherwise, the value will be used to set an attribute.
  1090. </para></listitem>
  1091. </itemizedlist>
  1092. <para>
  1093. Exceptions to the rule include the following:
  1094. </para>
  1095. <itemizedlist>
  1096. <listitem><para>
  1097. <code>prefixPath</code> will be passed to
  1098. <code>addPrefixPaths()</code>
  1099. </para></listitem>
  1100. <listitem>
  1101. <para>
  1102. The following setters cannot be set in this way:
  1103. </para>
  1104. <itemizedlist>
  1105. <listitem><para>
  1106. <code>setAttrib</code> (though
  1107. <code>setAttribs</code> <emphasis>will</emphasis>
  1108. work)
  1109. </para></listitem>
  1110. <listitem><para><code>setConfig</code></para></listitem>
  1111. <listitem><para><code>setOptions</code></para></listitem>
  1112. <listitem><para><code>setPluginLoader</code></para></listitem>
  1113. <listitem><para><code>setTranslator</code></para></listitem>
  1114. <listitem><para><code>setView</code></para></listitem>
  1115. </itemizedlist>
  1116. </listitem>
  1117. </itemizedlist>
  1118. <para>
  1119. As an example, here is a config file that passes configuration for
  1120. every type of configurable data:
  1121. </para>
  1122. <programlisting language="ini"><![CDATA[
  1123. [element]
  1124. name = "foo"
  1125. value = "foobar"
  1126. label = "Foo:"
  1127. order = 10
  1128. required = true
  1129. allowEmpty = false
  1130. autoInsertNotEmptyValidator = true
  1131. description = "Foo elements are for examples"
  1132. ignore = false
  1133. attribs.id = "foo"
  1134. attribs.class = "element"
  1135. ; sets 'onclick' attribute
  1136. onclick = "autoComplete(this, '/form/autocomplete/element')"
  1137. prefixPaths.decorator.prefix = "My_Decorator"
  1138. prefixPaths.decorator.path = "My/Decorator/"
  1139. disableTranslator = 0
  1140. validators.required.validator = "NotEmpty"
  1141. validators.required.breakChainOnFailure = true
  1142. validators.alpha.validator = "alpha"
  1143. validators.regex.validator = "regex"
  1144. validators.regex.options.pattern = "/^[A-F].*/$"
  1145. filters.ucase.filter = "StringToUpper"
  1146. decorators.element.decorator = "ViewHelper"
  1147. decorators.element.options.helper = "FormText"
  1148. decorators.label.decorator = "Label"
  1149. ]]></programlisting>
  1150. </sect2>
  1151. <sect2 id="zend.form.elements.custom">
  1152. <title>Custom Elements</title>
  1153. <para>
  1154. You can create your own custom elements by simply extending the
  1155. <classname>Zend_Form_Element</classname> class. Common reasons to do so
  1156. include:
  1157. </para>
  1158. <itemizedlist>
  1159. <listitem><para>
  1160. Elements that share common validators and/or filters
  1161. </para></listitem>
  1162. <listitem><para>
  1163. Elements that have custom decorator functionality
  1164. </para></listitem>
  1165. </itemizedlist>
  1166. <para>
  1167. There are two methods typically used to extend an element:
  1168. <code>init()</code>, which can be used to add custom initialization
  1169. logic to your element, and <code>loadDefaultDecorators()</code>,
  1170. which can be used to set a list of default decorators used by your
  1171. element.
  1172. </para>
  1173. <para>
  1174. As an example, let's say that all text elements in a form you are
  1175. creating need to be filtered with <code>StringTrim</code>,
  1176. validated with a common regular expression, and that you want to
  1177. use a custom decorator you've created for displaying them,
  1178. 'My_Decorator_TextItem'. In addition, you have a number of standard
  1179. attributes, including 'size', 'maxLength', and 'class' you wish to
  1180. specify. You could define an element to accomplish this as follows:
  1181. </para>
  1182. <programlisting language="php"><![CDATA[
  1183. class My_Element_Text extends Zend_Form_Element
  1184. {
  1185. public function init()
  1186. {
  1187. $this->addPrefixPath('My_Decorator', 'My/Decorator/', 'decorator')
  1188. ->addFilters('StringTrim')
  1189. ->addValidator('Regex', false, array('/^[a-z0-9]{6,}$/i'))
  1190. ->addDecorator('TextItem')
  1191. ->setAttrib('size', 30)
  1192. ->setAttrib('maxLength', 45)
  1193. ->setAttrib('class', 'text');
  1194. }
  1195. }
  1196. ]]></programlisting>
  1197. <para>
  1198. You could then inform your form object about the prefix path for
  1199. such elements, and start creating elements:
  1200. </para>
  1201. <programlisting language="php"><![CDATA[
  1202. $form->addPrefixPath('My_Element', 'My/Element/', 'element')
  1203. ->addElement('text', 'foo');
  1204. ]]></programlisting>
  1205. <para>
  1206. The 'foo' element will now be of type <code>My_Element_Text</code>,
  1207. and exhibit the behaviour you've outlined.
  1208. </para>
  1209. <para>
  1210. Another method you may want to override when extending
  1211. <classname>Zend_Form_Element</classname> is the
  1212. <code>loadDefaultDecorators()</code> method. This method
  1213. conditionally loads a set of default decorators for your element;
  1214. you may wish to substitute your own decorators in your extending
  1215. class:
  1216. </para>
  1217. <programlisting language="php"><![CDATA[
  1218. class My_Element_Text extends Zend_Form_Element
  1219. {
  1220. public function loadDefaultDecorators()
  1221. {
  1222. $this->addDecorator('ViewHelper')
  1223. ->addDecorator('DisplayError')
  1224. ->addDecorator('Label')
  1225. ->addDecorator('HtmlTag',
  1226. array('tag' => 'div', 'class' => 'element'));
  1227. }
  1228. }
  1229. ]]></programlisting>
  1230. <para>
  1231. There are many ways to customize elements. Read the API
  1232. documentation of <classname>Zend_Form_Element</classname> to learn about all of the
  1233. available methods.
  1234. </para>
  1235. </sect2>
  1236. </sect1>
  1237. <!--
  1238. vim:se ts=4 sw=4 tw=80 et:
  1239. -->