Zend_Form-Elements.xml 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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 role="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 role="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 role="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 first to see if the
  154. decorator exists there when you add a decorator. As a result, 'My_Decorator_Label' will
  155. 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 StringLength validator will use the correct length of the input without counting leading or trailing whitespace characters.
  166. These operations may be performed using
  167. <classname>Zend_Filter</classname>. <classname>Zend_Form_Element</classname> has
  168. support for filter chains, allowing you to specify multiple,
  169. sequential filters. Filtering happens both during
  170. validation and when you retrieve the element value via
  171. <code>getValue()</code>:
  172. </para>
  173. <programlisting role="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 role="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 role="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 role="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 role="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 role="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 <code>true</code> as the second parameter:
  318. </para>
  319. <programlisting role="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 role="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 role="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 <classname>Zend_Form_Element::addValidator()</classname> method
  345. allows you to do so by providing
  346. 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 role="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 role="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 role="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 role="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 role="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 role="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 <code>true</code>, this
  545. 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 = null)</code>
  564. </para></listitem>
  565. <listitem><para>
  566. <code>addValidators(array $validators)</code>
  567. </para></listitem>
  568. <listitem><para>
  569. <code>setValidators(array $validators)</code> (overwrites all validators)
  570. </para></listitem>
  571. <listitem><para>
  572. <code>getValidator($name)</code> (retrieve a validator object by name)
  573. </para></listitem>
  574. <listitem><para>
  575. <code>getValidators()</code> (retrieve all validators)
  576. </para></listitem>
  577. <listitem><para>
  578. <code>removeValidator($name)</code> (remove validator by name)
  579. </para></listitem>
  580. <listitem><para>
  581. <code>clearValidators()</code> (remove all validators)
  582. </para></listitem>
  583. </itemizedlist>
  584. <sect3 id="zend.form.elements.validators.errors">
  585. <title>Custom Error Messages</title>
  586. <para>
  587. At times, you may want to specify one or more specific error
  588. messages to use instead of the error messages generated by the
  589. validators attached to your element. Additionally, at times you
  590. may want to mark the element invalid yourself. As of 1.6.0, this
  591. functionality is possible via the following methods.
  592. </para>
  593. <itemizedlist>
  594. <listitem><para>
  595. <code>addErrorMessage($message)</code>: add an error message
  596. to display on form validation errors. You may call this more
  597. than once, and new messages are appended to the stack.
  598. </para></listitem>
  599. <listitem><para>
  600. <code>addErrorMessages(array $messages)</code>: add multiple
  601. error messages to display on form validation errors.
  602. </para></listitem>
  603. <listitem><para>
  604. <code>setErrorMessages(array $messages)</code>: add multiple
  605. error messages to display on form validation errors,
  606. overwriting all previously set error messages.
  607. </para></listitem>
  608. <listitem><para>
  609. <code>getErrorMessages()</code>: retrieve the list of
  610. custom error messages that have been defined.
  611. </para></listitem>
  612. <listitem><para>
  613. <code>clearErrorMessages()</code>: remove all custom error
  614. messages that have been defined.
  615. </para></listitem>
  616. <listitem><para>
  617. <code>markAsError()</code>: mark the element as having
  618. failed validation.
  619. </para></listitem>
  620. <listitem><para>
  621. <code>hasErrors()</code>: determine whether the element has
  622. either failed validation or been marked as invalid.
  623. </para></listitem>
  624. <listitem><para>
  625. <code>addError($message)</code>: add a message to the custom
  626. error messages stack and flag the element as invalid.
  627. </para></listitem>
  628. <listitem><para>
  629. <code>addErrors(array $messages)</code>: add several
  630. messages to the custom error messages stack and flag the
  631. element as invalid.
  632. </para></listitem>
  633. <listitem><para>
  634. <code>setErrors(array $messages)</code>: overwrite the
  635. custom error messages stack with the provided messages and
  636. flag the element as invalid.
  637. </para></listitem>
  638. </itemizedlist>
  639. <para>
  640. All errors set in this fashion may be translated. Additionally,
  641. you may insert the placeholder "%value%" to represent the
  642. element value; this current element value will be substituted
  643. when the error messages are retrieved.
  644. </para>
  645. </sect3>
  646. </sect2>
  647. <sect2 id="zend.form.elements.decorators">
  648. <title>Decorators</title>
  649. <para>
  650. One particular pain point for many web developers is the creation
  651. of the XHTML forms themselves. For each element, the developer
  652. needs to create markup for the element itself (typically a label)
  653. and special markup for displaying
  654. validation error messages. The more elements on the page, the less
  655. trivial this task becomes.
  656. </para>
  657. <para>
  658. <classname>Zend_Form_Element</classname> tries to solve this issue through
  659. the use of "decorators". Decorators are simply classes that have
  660. access to the element and a method for rendering content. For more
  661. information on how decorators work, please see the section on <link
  662. linkend="zend.form.decorators">Zend_Form_Decorator</link>.
  663. </para>
  664. <para>
  665. The default decorators used by <classname>Zend_Form_Element</classname> are:
  666. </para>
  667. <itemizedlist>
  668. <listitem><para>
  669. <emphasis>ViewHelper</emphasis>: specifies a view helper to use
  670. to render the element. The 'helper' element attribute can be
  671. used to specify which view helper to use. By default,
  672. <classname>Zend_Form_Element</classname> specifies the 'formText' view
  673. helper, but individual subclasses specify different helpers.
  674. </para></listitem>
  675. <listitem><para>
  676. <emphasis>Errors</emphasis>: appends error messages to the
  677. element using <classname>Zend_View_Helper_FormErrors</classname>. If none are present,
  678. nothing is appended.
  679. </para></listitem>
  680. <listitem><para>
  681. <emphasis>Description</emphasis>: appends the element
  682. description. If none is present, nothing is appended. By
  683. default, the description is rendered in a &lt;p&gt; tag with a
  684. class of 'description'.
  685. </para></listitem>
  686. <listitem><para>
  687. <emphasis>HtmlTag</emphasis>: wraps the element and errors in
  688. an HTML &lt;dd&gt; tag.
  689. </para></listitem>
  690. <listitem><para>
  691. <emphasis>Label</emphasis>: prepends a label to the element
  692. using <classname>Zend_View_Helper_FormLabel</classname>, and wraps it in a &lt;dt&gt;
  693. tag. If no label is provided, just the definition term tag is
  694. rendered.
  695. </para></listitem>
  696. </itemizedlist>
  697. <note>
  698. <title>Default Decorators Do Not Need to Be Loaded</title>
  699. <para>
  700. By default, the default decorators are loaded during object
  701. initialization. You can disable this by passing the
  702. 'disableLoadDefaultDecorators' option to the constructor:
  703. </para>
  704. <programlisting role="php"><![CDATA[
  705. $element = new Zend_Form_Element('foo',
  706. array('disableLoadDefaultDecorators' =>
  707. true)
  708. );
  709. ]]></programlisting>
  710. <para>
  711. This option may be mixed with any other options you pass,
  712. both as array options or in a <classname>Zend_Config</classname> object.
  713. </para>
  714. </note>
  715. <para>
  716. Since the order in which decorators are registered matters- the first
  717. decorator registered is executed first- you will need to make
  718. sure you register your decorators in an appropriate order, or
  719. ensure that you set the placement options in a sane fashion. To
  720. give an example, here is the code that registers the default
  721. decorators:
  722. </para>
  723. <programlisting role="php"><![CDATA[
  724. $this->addDecorators(array(
  725. array('ViewHelper'),
  726. array('Errors'),
  727. array('Description', array('tag' => 'p', 'class' => 'description')),
  728. array('HtmlTag', array('tag' => 'dd')),
  729. array('Label', array('tag' => 'dt')),
  730. ));
  731. ]]></programlisting>
  732. <para>
  733. The initial content is created by the 'ViewHelper' decorator, which
  734. creates the form element itself. Next, the 'Errors' decorator
  735. fetches error messages from the element, and, if any are present,
  736. passes them to the 'FormErrors' view helper to render. If a
  737. description is present, the 'Description' decorator will append a
  738. paragraph of class 'description' containing the descriptive text to
  739. the aggregated content. The next decorator, 'HtmlTag', wraps the
  740. element, errors, and description in an HTML &lt;dd&gt; tag.
  741. Finally, the last decorator, 'label', retrieves the element's label
  742. and passes it to the 'FormLabel' view helper, wrapping it in an HTML
  743. &lt;dt&gt; tag; the value is prepended to the content by default.
  744. The resulting output looks basically like this:
  745. </para>
  746. <programlisting role="html"><![CDATA[
  747. <dt><label for="foo" class="optional">Foo</label></dt>
  748. <dd>
  749. <input type="text" name="foo" id="foo" value="123" />
  750. <ul class="errors">
  751. <li>"123" is not an alphanumeric value</li>
  752. </ul>
  753. <p class="description">
  754. This is some descriptive text regarding the element.
  755. </p>
  756. </dd>
  757. ]]></programlisting>
  758. <para>
  759. For more information on decorators, read the <link
  760. linkend="zend.form.decorators">Zend_Form_Decorator
  761. section</link>.
  762. </para>
  763. <note>
  764. <title>Using Multiple Decorators of the Same Type</title>
  765. <para>
  766. Internally, <classname>Zend_Form_Element</classname> uses a decorator's
  767. class as the lookup mechanism when retrieving decorators. As a
  768. result, you cannot register multiple decorators of the same
  769. type; subsequent decorators will simply overwrite those that
  770. existed before.
  771. </para>
  772. <para>
  773. To get around this, you can use <emphasis>aliases</emphasis>.
  774. Instead of passing a decorator or decorator name as the first
  775. argument to <code>addDecorator()</code>, pass an array with a
  776. single element, with the alias pointing to the decorator object
  777. or name:
  778. </para>
  779. <programlisting role="php"><![CDATA[
  780. // Alias to 'FooBar':
  781. $element->addDecorator(array('FooBar' => 'HtmlTag'),
  782. array('tag' => 'div'));
  783. // And retrieve later:
  784. $decorator = $element->getDecorator('FooBar');
  785. ]]></programlisting>
  786. <para>
  787. In the <code>addDecorators()</code> and
  788. <code>setDecorators()</code> methods, you will need to pass
  789. the 'decorator' option in the array representing the decorator:
  790. </para>
  791. <programlisting role="php"><![CDATA[
  792. // Add two 'HtmlTag' decorators, aliasing one to 'FooBar':
  793. $element->addDecorators(
  794. array('HtmlTag', array('tag' => 'div')),
  795. array(
  796. 'decorator' => array('FooBar' => 'HtmlTag'),
  797. 'options' => array('tag' => 'dd')
  798. ),
  799. );
  800. // And retrieve later:
  801. $htmlTag = $element->getDecorator('HtmlTag');
  802. $fooBar = $element->getDecorator('FooBar');
  803. ]]></programlisting>
  804. </note>
  805. <para>
  806. Methods associated with decorators include:
  807. </para>
  808. <itemizedlist>
  809. <listitem><para>
  810. <code>addDecorator($nameOrDecorator, array $options = null)</code>
  811. </para></listitem>
  812. <listitem><para>
  813. <code>addDecorators(array $decorators)</code>
  814. </para></listitem>
  815. <listitem><para>
  816. <code>setDecorators(array $decorators)</code> (overwrites all decorators)
  817. </para></listitem>
  818. <listitem><para>
  819. <code>getDecorator($name)</code> (retrieve a decorator object by name)
  820. </para></listitem>
  821. <listitem><para>
  822. <code>getDecorators()</code> (retrieve all decorators)
  823. </para></listitem>
  824. <listitem><para>
  825. <code>removeDecorator($name)</code> (remove decorator by name)
  826. </para></listitem>
  827. <listitem><para>
  828. <code>clearDecorators()</code> (remove all decorators)
  829. </para></listitem>
  830. </itemizedlist>
  831. <para>
  832. <classname>Zend_Form_Element</classname> also uses overloading to allow rendering
  833. specific decorators. <code>__call()</code> will intercept methods
  834. that lead with the text 'render' and use the remainder of the method
  835. name to lookup a decorator; if found, it will then render that
  836. <emphasis>single</emphasis> decorator. Any arguments passed to the
  837. method call will be used as content to pass to the decorator's
  838. <code>render()</code> method. As an example:
  839. </para>
  840. <programlisting role="php"><![CDATA[
  841. // Render only the ViewHelper decorator:
  842. echo $element->renderViewHelper();
  843. // Render only the HtmlTag decorator, passing in content:
  844. echo $element->renderHtmlTag("This is the html tag content");
  845. ]]></programlisting>
  846. <para>
  847. If the decorator does not exist, an exception is raised.
  848. </para>
  849. </sect2>
  850. <sect2 id="zend.form.elements.metadata">
  851. <title>Metadata and Attributes</title>
  852. <para>
  853. <classname>Zend_Form_Element</classname> handles a variety of attributes and
  854. element metadata. Basic attributes include:
  855. </para>
  856. <itemizedlist>
  857. <listitem><para>
  858. <emphasis>name</emphasis>: the element name. Uses the
  859. <code>setName()</code> and <code>getName()</code> accessors.
  860. </para></listitem>
  861. <listitem><para>
  862. <emphasis>label</emphasis>: the element label. Uses the
  863. <code>setLabel()</code> and <code>getLabel()</code> accessors.
  864. </para></listitem>
  865. <listitem><para>
  866. <emphasis>order</emphasis>: the index at which an element
  867. should appear in the form. Uses the <code>setOrder()</code> and
  868. <code>getOrder()</code> accessors.
  869. </para></listitem>
  870. <listitem><para>
  871. <emphasis>value</emphasis>: the current element value. Uses the
  872. <code>setValue()</code> and <code>getValue()</code> accessors.
  873. </para></listitem>
  874. <listitem><para>
  875. <emphasis>description</emphasis>: a description of the element;
  876. often used to provide tooltip or javascript contextual hinting
  877. describing the purpose of the element. Uses the
  878. <code>setDescription()</code> and <code>getDescription()</code>
  879. accessors.
  880. </para></listitem>
  881. <listitem><para>
  882. <emphasis>required</emphasis>: flag indicating whether or not
  883. the element is required when performing form validation. Uses
  884. the <code>setRequired()</code> and <code>getRequired()</code>
  885. accessors. This flag is false by default.
  886. </para></listitem>
  887. <listitem><para>
  888. <emphasis>allowEmpty</emphasis>: flag indicating whether or not
  889. a non-required (optional) element should attempt to validate
  890. empty values. If it is set to true and the required flag is false, empty
  891. values are not passed to the validator chain and are presumed true.
  892. Uses the <code>setAllowEmpty()</code> and <code>getAllowEmpty()</code>
  893. accessors. This flag is true by default.
  894. </para></listitem>
  895. <listitem><para>
  896. <emphasis>autoInsertNotEmptyValidator</emphasis>: flag
  897. indicating whether or not to insert a 'NotEmpty' validator when
  898. the element is required. By default, this flag is true. Set the
  899. flag with <code>setAutoInsertNotEmptyValidator($flag)</code> and
  900. determine the value with
  901. <code>autoInsertNotEmptyValidator()</code>.
  902. </para></listitem>
  903. </itemizedlist>
  904. <para>
  905. Form elements may require additional metadata. For XHTML form
  906. elements, for instance, you may want to specify attributes such as
  907. the class or id. To facilitate this are a set of accessors:
  908. </para>
  909. <itemizedlist>
  910. <listitem><para>
  911. <emphasis>setAttrib($name, $value)</emphasis>: add an attribute
  912. </para></listitem>
  913. <listitem><para>
  914. <emphasis>setAttribs(array $attribs)</emphasis>: like
  915. addAttribs(), but overwrites
  916. </para></listitem>
  917. <listitem><para>
  918. <emphasis>getAttrib($name)</emphasis>: retrieve a single
  919. attribute value
  920. </para></listitem>
  921. <listitem><para>
  922. <emphasis>getAttribs()</emphasis>: retrieve all attributes as
  923. key/value pairs
  924. </para></listitem>
  925. </itemizedlist>
  926. <para>
  927. Most of the time, however, you can simply access them as object
  928. properties, as <classname>Zend_Form_Element</classname> utilizes overloading
  929. to facilitate access to them:
  930. </para>
  931. <programlisting role="php"><![CDATA[
  932. // Equivalent to $element->setAttrib('class', 'text'):
  933. $element->class = 'text;
  934. ]]></programlisting>
  935. <para>
  936. By default, all attributes are passed to the view helper used by
  937. the element during rendering, and rendered as HTML attributes of
  938. the element tag.
  939. </para>
  940. </sect2>
  941. <sect2 id="zend.form.elements.standard">
  942. <title>Standard Elements</title>
  943. <para>
  944. <classname>Zend_Form</classname> ships with a number of standard elements; please read the
  945. <link linkend="zend.form.standardElements">Standard Elements</link>
  946. chapter for full details.
  947. </para>
  948. </sect2>
  949. <sect2 id="zend.form.elements.methods">
  950. <title>Zend_Form_Element Methods</title>
  951. <para>
  952. <classname>Zend_Form_Element</classname> has many, many methods. What follows
  953. is a quick summary of their signatures, grouped by type:
  954. </para>
  955. <itemizedlist>
  956. <listitem><para>Configuration:</para>
  957. <itemizedlist>
  958. <listitem><para><code>setOptions(array $options)</code></para></listitem>
  959. <listitem><para><code>setConfig(Zend_Config $config)</code></para></listitem>
  960. </itemizedlist>
  961. </listitem>
  962. <listitem><para>I18n:</para>
  963. <itemizedlist>
  964. <listitem><para><code>setTranslator(Zend_Translate_Adapter $translator = null)</code></para></listitem>
  965. <listitem><para><code>getTranslator()</code></para></listitem>
  966. <listitem><para><code>setDisableTranslator($flag)</code></para></listitem>
  967. <listitem><para><code>translatorIsDisabled()</code></para></listitem>
  968. </itemizedlist>
  969. </listitem>
  970. <listitem><para>Properties:</para>
  971. <itemizedlist>
  972. <listitem><para><code>setName($name)</code></para></listitem>
  973. <listitem><para><code>getName()</code></para></listitem>
  974. <listitem><para><code>setValue($value)</code></para></listitem>
  975. <listitem><para><code>getValue()</code></para></listitem>
  976. <listitem><para><code>getUnfilteredValue()</code></para></listitem>
  977. <listitem><para><code>setLabel($label)</code></para></listitem>
  978. <listitem><para><code>getLabel()</code></para></listitem>
  979. <listitem><para><code>setDescription($description)</code></para></listitem>
  980. <listitem><para><code>getDescription()</code></para></listitem>
  981. <listitem><para><code>setOrder($order)</code></para></listitem>
  982. <listitem><para><code>getOrder()</code></para></listitem>
  983. <listitem><para><code>setRequired($flag)</code></para></listitem>
  984. <listitem><para><code>getRequired()</code></para></listitem>
  985. <listitem><para><code>setAllowEmpty($flag)</code></para></listitem>
  986. <listitem><para><code>getAllowEmpty()</code></para></listitem>
  987. <listitem><para><code>setAutoInsertNotEmptyValidator($flag)</code></para></listitem>
  988. <listitem><para><code>autoInsertNotEmptyValidator()</code></para></listitem>
  989. <listitem><para><code>setIgnore($flag)</code></para></listitem>
  990. <listitem><para><code>getIgnore()</code></para></listitem>
  991. <listitem><para><code>getType()</code></para></listitem>
  992. <listitem><para><code>setAttrib($name, $value)</code></para></listitem>
  993. <listitem><para><code>setAttribs(array $attribs)</code></para></listitem>
  994. <listitem><para><code>getAttrib($name)</code></para></listitem>
  995. <listitem><para><code>getAttribs()</code></para></listitem>
  996. </itemizedlist>
  997. </listitem>
  998. <listitem><para>Plugin loaders and paths:</para>
  999. <itemizedlist>
  1000. <listitem><para><code>setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type)</code></para></listitem>
  1001. <listitem><para><code>getPluginLoader($type)</code></para></listitem>
  1002. <listitem><para><code>addPrefixPath($prefix, $path, $type = null)</code></para></listitem>
  1003. <listitem><para><code>addPrefixPaths(array $spec)</code></para></listitem>
  1004. </itemizedlist>
  1005. </listitem>
  1006. <listitem><para>Validation:</para>
  1007. <itemizedlist>
  1008. <listitem><para><code>addValidator($validator, $breakChainOnFailure = false, $options = array())</code></para></listitem>
  1009. <listitem><para><code>addValidators(array $validators)</code></para></listitem>
  1010. <listitem><para><code>setValidators(array $validators)</code></para></listitem>
  1011. <listitem><para><code>getValidator($name)</code></para></listitem>
  1012. <listitem><para><code>getValidators()</code></para></listitem>
  1013. <listitem><para><code>removeValidator($name)</code></para></listitem>
  1014. <listitem><para><code>clearValidators()</code></para></listitem>
  1015. <listitem><para><code>isValid($value, $context = null)</code></para></listitem>
  1016. <listitem><para><code>getErrors()</code></para></listitem>
  1017. <listitem><para><code>getMessages()</code></para></listitem>
  1018. </itemizedlist>
  1019. </listitem>
  1020. <listitem><para>Filters:</para>
  1021. <itemizedlist>
  1022. <listitem><para><code>addFilter($filter, $options = array())</code></para></listitem>
  1023. <listitem><para><code>addFilters(array $filters)</code></para></listitem>
  1024. <listitem><para><code>setFilters(array $filters)</code></para></listitem>
  1025. <listitem><para><code>getFilter($name)</code></para></listitem>
  1026. <listitem><para><code>getFilters()</code></para></listitem>
  1027. <listitem><para><code>removeFilter($name)</code></para></listitem>
  1028. <listitem><para><code>clearFilters()</code></para></listitem>
  1029. </itemizedlist>
  1030. </listitem>
  1031. <listitem><para>Rendering:</para>
  1032. <itemizedlist>
  1033. <listitem><para><code>setView(Zend_View_Interface $view = null)</code></para></listitem>
  1034. <listitem><para><code>getView()</code></para></listitem>
  1035. <listitem><para><code>addDecorator($decorator, $options = null)</code></para></listitem>
  1036. <listitem><para><code>addDecorators(array $decorators)</code></para></listitem>
  1037. <listitem><para><code>setDecorators(array $decorators)</code></para></listitem>
  1038. <listitem><para><code>getDecorator($name)</code></para></listitem>
  1039. <listitem><para><code>getDecorators()</code></para></listitem>
  1040. <listitem><para><code>removeDecorator($name)</code></para></listitem>
  1041. <listitem><para><code>clearDecorators()</code></para></listitem>
  1042. <listitem><para><code>render(Zend_View_Interface $view = null)</code></para></listitem>
  1043. </itemizedlist>
  1044. </listitem>
  1045. </itemizedlist>
  1046. </sect2>
  1047. <sect2 id="zend.form.elements.config">
  1048. <title>Configuration</title>
  1049. <para>
  1050. <classname>Zend_Form_Element</classname>'s constructor accepts either an
  1051. array of options or a <classname>Zend_Config</classname> object containing
  1052. options, and it can also be configured using either
  1053. <code>setOptions()</code> or <code>setConfig()</code>. Generally
  1054. speaking, keys are named as follows:
  1055. </para>
  1056. <itemizedlist>
  1057. <listitem><para>
  1058. If 'set' + key refers to a <classname>Zend_Form_Element</classname>
  1059. method, then the value provided will be passed to that method.
  1060. </para></listitem>
  1061. <listitem><para>
  1062. Otherwise, the value will be used to set an attribute.
  1063. </para></listitem>
  1064. </itemizedlist>
  1065. <para>
  1066. Exceptions to the rule include the following:
  1067. </para>
  1068. <itemizedlist>
  1069. <listitem><para>
  1070. <code>prefixPath</code> will be passed to
  1071. <code>addPrefixPaths()</code>
  1072. </para></listitem>
  1073. <listitem>
  1074. <para>
  1075. The following setters cannot be set in this way:
  1076. </para>
  1077. <itemizedlist>
  1078. <listitem><para>
  1079. <code>setAttrib</code> (though
  1080. <code>setAttribs</code> <emphasis>will</emphasis>
  1081. work)
  1082. </para></listitem>
  1083. <listitem><para><code>setConfig</code></para></listitem>
  1084. <listitem><para><code>setOptions</code></para></listitem>
  1085. <listitem><para><code>setPluginLoader</code></para></listitem>
  1086. <listitem><para><code>setTranslator</code></para></listitem>
  1087. <listitem><para><code>setView</code></para></listitem>
  1088. </itemizedlist>
  1089. </listitem>
  1090. </itemizedlist>
  1091. <para>
  1092. As an example, here is a config file that passes configuration for
  1093. every type of configurable data:
  1094. </para>
  1095. <programlisting role="ini"><![CDATA[
  1096. [element]
  1097. name = "foo"
  1098. value = "foobar"
  1099. label = "Foo:"
  1100. order = 10
  1101. required = true
  1102. allowEmpty = false
  1103. autoInsertNotEmptyValidator = true
  1104. description = "Foo elements are for examples"
  1105. ignore = false
  1106. attribs.id = "foo"
  1107. attribs.class = "element"
  1108. ; sets 'onclick' attribute
  1109. onclick = "autoComplete(this, '/form/autocomplete/element')"
  1110. prefixPaths.decorator.prefix = "My_Decorator"
  1111. prefixPaths.decorator.path = "My/Decorator/"
  1112. disableTranslator = 0
  1113. validators.required.validator = "NotEmpty"
  1114. validators.required.breakChainOnFailure = true
  1115. validators.alpha.validator = "alpha"
  1116. validators.regex.validator = "regex"
  1117. validators.regex.options.pattern = "/^[A-F].*/$"
  1118. filters.ucase.filter = "StringToUpper"
  1119. decorators.element.decorator = "ViewHelper"
  1120. decorators.element.options.helper = "FormText"
  1121. decorators.label.decorator = "Label"
  1122. ]]></programlisting>
  1123. </sect2>
  1124. <sect2 id="zend.form.elements.custom">
  1125. <title>Custom Elements</title>
  1126. <para>
  1127. You can create your own custom elements by simply extending the
  1128. <classname>Zend_Form_Element</classname> class. Common reasons to do so
  1129. include:
  1130. </para>
  1131. <itemizedlist>
  1132. <listitem><para>
  1133. Elements that share common validators and/or filters
  1134. </para></listitem>
  1135. <listitem><para>
  1136. Elements that have custom decorator functionality
  1137. </para></listitem>
  1138. </itemizedlist>
  1139. <para>
  1140. There are two methods typically used to extend an element:
  1141. <code>init()</code>, which can be used to add custom initialization
  1142. logic to your element, and <code>loadDefaultDecorators()</code>,
  1143. which can be used to set a list of default decorators used by your
  1144. element.
  1145. </para>
  1146. <para>
  1147. As an example, let's say that all text elements in a form you are
  1148. creating need to be filtered with <code>StringTrim</code>,
  1149. validated with a common regular expression, and that you want to
  1150. use a custom decorator you've created for displaying them,
  1151. 'My_Decorator_TextItem'. In addition, you have a number of standard
  1152. attributes, including 'size', 'maxLength', and 'class' you wish to
  1153. specify. You could define an element to accomplish this as follows:
  1154. </para>
  1155. <programlisting role="php"><![CDATA[
  1156. class My_Element_Text extends Zend_Form_Element
  1157. {
  1158. public function init()
  1159. {
  1160. $this->addPrefixPath('My_Decorator', 'My/Decorator/', 'decorator')
  1161. ->addFilters('StringTrim')
  1162. ->addValidator('Regex', false, array('/^[a-z0-9]{6,}$/i'))
  1163. ->addDecorator('TextItem')
  1164. ->setAttrib('size', 30)
  1165. ->setAttrib('maxLength', 45)
  1166. ->setAttrib('class', 'text');
  1167. }
  1168. }
  1169. ]]></programlisting>
  1170. <para>
  1171. You could then inform your form object about the prefix path for
  1172. such elements, and start creating elements:
  1173. </para>
  1174. <programlisting role="php"><![CDATA[
  1175. $form->addPrefixPath('My_Element', 'My/Element/', 'element')
  1176. ->addElement('text', 'foo');
  1177. ]]></programlisting>
  1178. <para>
  1179. The 'foo' element will now be of type <code>My_Element_Text</code>,
  1180. and exhibit the behaviour you've outlined.
  1181. </para>
  1182. <para>
  1183. Another method you may want to override when extending
  1184. <classname>Zend_Form_Element</classname> is the
  1185. <code>loadDefaultDecorators()</code> method. This method
  1186. conditionally loads a set of default decorators for your element;
  1187. you may wish to substitute your own decorators in your extending
  1188. class:
  1189. </para>
  1190. <programlisting role="php"><![CDATA[
  1191. class My_Element_Text extends Zend_Form_Element
  1192. {
  1193. public function loadDefaultDecorators()
  1194. {
  1195. $this->addDecorator('ViewHelper')
  1196. ->addDecorator('DisplayError')
  1197. ->addDecorator('Label')
  1198. ->addDecorator('HtmlTag',
  1199. array('tag' => 'div', 'class' => 'element'));
  1200. }
  1201. }
  1202. ]]></programlisting>
  1203. <para>
  1204. There are many ways to customize elements. Read the API
  1205. documentation of <classname>Zend_Form_Element</classname> to learn about all of the
  1206. available methods.
  1207. </para>
  1208. </sect2>
  1209. </sect1>
  1210. <!--
  1211. vim:se ts=4 sw=4 tw=80 et:
  1212. -->