Zend_Form-Elements.xml 65 KB

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