Form.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** @see Zend_Validate_Interface */
  21. require_once 'Zend/Validate/Interface.php';
  22. /**
  23. * Zend_Form
  24. *
  25. * @category Zend
  26. * @package Zend_Form
  27. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @version $Id$
  30. */
  31. class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
  32. {
  33. /**#@+
  34. * Plugin loader type constants
  35. */
  36. const DECORATOR = 'DECORATOR';
  37. const ELEMENT = 'ELEMENT';
  38. /**#@-*/
  39. /**#@+
  40. * Method type constants
  41. */
  42. const METHOD_DELETE = 'delete';
  43. const METHOD_GET = 'get';
  44. const METHOD_POST = 'post';
  45. const METHOD_PUT = 'put';
  46. /**#@-*/
  47. /**#@+
  48. * Encoding type constants
  49. */
  50. const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded';
  51. const ENCTYPE_MULTIPART = 'multipart/form-data';
  52. /**#@-*/
  53. /**
  54. * Form metadata and attributes
  55. * @var array
  56. */
  57. protected $_attribs = array();
  58. /**
  59. * Decorators for rendering
  60. * @var array
  61. */
  62. protected $_decorators = array();
  63. /**
  64. * Default display group class
  65. * @var string
  66. */
  67. protected $_defaultDisplayGroupClass = 'Zend_Form_DisplayGroup';
  68. /**
  69. * Form description
  70. * @var string
  71. */
  72. protected $_description;
  73. /**
  74. * Should we disable loading the default decorators?
  75. * @var bool
  76. */
  77. protected $_disableLoadDefaultDecorators = false;
  78. /**
  79. * Display group prefix paths
  80. * @var array
  81. */
  82. protected $_displayGroupPrefixPaths = array();
  83. /**
  84. * Groups of elements grouped for display purposes
  85. * @var array
  86. */
  87. protected $_displayGroups = array();
  88. /**
  89. * Global decorators to apply to all elements
  90. * @var null|array
  91. */
  92. protected $_elementDecorators;
  93. /**
  94. * Prefix paths to use when creating elements
  95. * @var array
  96. */
  97. protected $_elementPrefixPaths = array();
  98. /**
  99. * Form elements
  100. * @var array
  101. */
  102. protected $_elements = array();
  103. /**
  104. * Array to which elements belong (if any)
  105. * @var string
  106. */
  107. protected $_elementsBelongTo;
  108. /**
  109. * Custom form-level error messages
  110. * @var array
  111. */
  112. protected $_errorMessages = array();
  113. /**
  114. * Are there errors in the form?
  115. * @var bool
  116. */
  117. protected $_errorsExist = false;
  118. /**
  119. * Form order
  120. * @var int|null
  121. */
  122. protected $_formOrder;
  123. /**
  124. * Whether or not form elements are members of an array
  125. * @var bool
  126. */
  127. protected $_isArray = false;
  128. /**
  129. * Form legend
  130. * @var string
  131. */
  132. protected $_legend;
  133. /**
  134. * Plugin loaders
  135. * @var array
  136. */
  137. protected $_loaders = array();
  138. /**
  139. * Allowed form methods
  140. * @var array
  141. */
  142. protected $_methods = array('delete', 'get', 'post', 'put');
  143. /**
  144. * Order in which to display and iterate elements
  145. * @var array
  146. */
  147. protected $_order = array();
  148. /**
  149. * Whether internal order has been updated or not
  150. * @var bool
  151. */
  152. protected $_orderUpdated = false;
  153. /**
  154. * Sub form prefix paths
  155. * @var array
  156. */
  157. protected $_subFormPrefixPaths = array();
  158. /**
  159. * Sub forms
  160. * @var array
  161. */
  162. protected $_subForms = array();
  163. /**
  164. * @var Zend_Translate
  165. */
  166. protected $_translator;
  167. /**
  168. * Global default translation adapter
  169. * @var Zend_Translate
  170. */
  171. protected static $_translatorDefault;
  172. /**
  173. * is the translator disabled?
  174. * @var bool
  175. */
  176. protected $_translatorDisabled = false;
  177. /**
  178. * @var Zend_View_Interface
  179. */
  180. protected $_view;
  181. /**
  182. * Constructor
  183. *
  184. * Registers form view helper as decorator
  185. *
  186. * @param mixed $options
  187. * @return void
  188. */
  189. public function __construct($options = null)
  190. {
  191. if (is_array($options)) {
  192. $this->setOptions($options);
  193. } elseif ($options instanceof Zend_Config) {
  194. $this->setConfig($options);
  195. }
  196. // Extensions...
  197. $this->init();
  198. $this->loadDefaultDecorators();
  199. }
  200. /**
  201. * Clone form object and all children
  202. *
  203. * @return void
  204. */
  205. public function __clone()
  206. {
  207. $elements = array();
  208. foreach ($this->getElements() as $name => $element) {
  209. $elements[] = clone $element;
  210. }
  211. $this->setElements($elements);
  212. $subForms = array();
  213. foreach ($this->getSubForms() as $name => $subForm) {
  214. $subForms[$name] = clone $subForm;
  215. }
  216. $this->setSubForms($subForms);
  217. $displayGroups = array();
  218. foreach ($this->_displayGroups as $group) {
  219. $clone = clone $group;
  220. $elements = array();
  221. foreach ($clone->getElements() as $name => $e) {
  222. $elements[] = $this->getElement($name);
  223. }
  224. $clone->setElements($elements);
  225. $displayGroups[] = $clone;
  226. }
  227. $this->setDisplayGroups($displayGroups);
  228. }
  229. /**
  230. * Reset values of form
  231. *
  232. * @return Zend_Form
  233. */
  234. public function reset()
  235. {
  236. foreach ($this->getElements() as $element) {
  237. $element->setValue(null);
  238. }
  239. foreach ($this->getSubForms() as $subForm) {
  240. $subForm->reset();
  241. }
  242. return $this;
  243. }
  244. /**
  245. * Initialize form (used by extending classes)
  246. *
  247. * @return void
  248. */
  249. public function init()
  250. {
  251. }
  252. /**
  253. * Set form state from options array
  254. *
  255. * @param array $options
  256. * @return Zend_Form
  257. */
  258. public function setOptions(array $options)
  259. {
  260. if (isset($options['prefixPath'])) {
  261. $this->addPrefixPaths($options['prefixPath']);
  262. unset($options['prefixPath']);
  263. }
  264. if (isset($options['elementPrefixPath'])) {
  265. $this->addElementPrefixPaths($options['elementPrefixPath']);
  266. unset($options['elementPrefixPath']);
  267. }
  268. if (isset($options['displayGroupPrefixPath'])) {
  269. $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']);
  270. unset($options['displayGroupPrefixPath']);
  271. }
  272. if (isset($options['elementDecorators'])) {
  273. $this->_elementDecorators = $options['elementDecorators'];
  274. unset($options['elementDecorators']);
  275. }
  276. if (isset($options['elements'])) {
  277. $this->setElements($options['elements']);
  278. unset($options['elements']);
  279. }
  280. if (isset($options['defaultDisplayGroupClass'])) {
  281. $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
  282. unset($options['defaultDisplayGroupClass']);
  283. }
  284. if (isset($options['displayGroupDecorators'])) {
  285. $displayGroupDecorators = $options['displayGroupDecorators'];
  286. unset($options['displayGroupDecorators']);
  287. }
  288. if (isset($options['elementsBelongTo'])) {
  289. $elementsBelongTo = $options['elementsBelongTo'];
  290. unset($options['elementsBelongTo']);
  291. }
  292. if (isset($options['attribs'])) {
  293. $this->addAttribs($options['attribs']);
  294. unset($options['attribs']);
  295. }
  296. $forbidden = array(
  297. 'Options', 'Config', 'PluginLoader', 'SubForms', 'View', 'Translator',
  298. 'Attrib', 'Default',
  299. );
  300. foreach ($options as $key => $value) {
  301. $normalized = ucfirst($key);
  302. if (in_array($normalized, $forbidden)) {
  303. continue;
  304. }
  305. $method = 'set' . $normalized;
  306. if (method_exists($this, $method)) {
  307. $this->$method($value);
  308. } else {
  309. $this->setAttrib($key, $value);
  310. }
  311. }
  312. if (isset($displayGroupDecorators)) {
  313. $this->setDisplayGroupDecorators($displayGroupDecorators);
  314. }
  315. if (isset($elementsBelongTo)) {
  316. $this->setElementsBelongTo($elementsBelongTo);
  317. }
  318. return $this;
  319. }
  320. /**
  321. * Set form state from config object
  322. *
  323. * @param Zend_Config $config
  324. * @return Zend_Form
  325. */
  326. public function setConfig(Zend_Config $config)
  327. {
  328. return $this->setOptions($config->toArray());
  329. }
  330. // Loaders
  331. /**
  332. * Set plugin loaders for use with decorators and elements
  333. *
  334. * @param Zend_Loader_PluginLoader_Interface $loader
  335. * @param string $type 'decorator' or 'element'
  336. * @return Zend_Form
  337. * @throws Zend_Form_Exception on invalid type
  338. */
  339. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)
  340. {
  341. $type = strtoupper($type);
  342. switch ($type) {
  343. case self::DECORATOR:
  344. case self::ELEMENT:
  345. $this->_loaders[$type] = $loader;
  346. return $this;
  347. default:
  348. require_once 'Zend/Form/Exception.php';
  349. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
  350. }
  351. }
  352. /**
  353. * Retrieve plugin loader for given type
  354. *
  355. * $type may be one of:
  356. * - decorator
  357. * - element
  358. *
  359. * If a plugin loader does not exist for the given type, defaults are
  360. * created.
  361. *
  362. * @param string $type
  363. * @return Zend_Loader_PluginLoader_Interface
  364. */
  365. public function getPluginLoader($type = null)
  366. {
  367. $type = strtoupper($type);
  368. if (!isset($this->_loaders[$type])) {
  369. switch ($type) {
  370. case self::DECORATOR:
  371. $prefixSegment = 'Form_Decorator';
  372. $pathSegment = 'Form/Decorator';
  373. break;
  374. case self::ELEMENT:
  375. $prefixSegment = 'Form_Element';
  376. $pathSegment = 'Form/Element';
  377. break;
  378. default:
  379. require_once 'Zend/Form/Exception.php';
  380. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  381. }
  382. require_once 'Zend/Loader/PluginLoader.php';
  383. $this->_loaders[$type] = new Zend_Loader_PluginLoader(
  384. array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
  385. );
  386. }
  387. return $this->_loaders[$type];
  388. }
  389. /**
  390. * Add prefix path for plugin loader
  391. *
  392. * If no $type specified, assumes it is a base path for both filters and
  393. * validators, and sets each according to the following rules:
  394. * - decorators: $prefix = $prefix . '_Decorator'
  395. * - elements: $prefix = $prefix . '_Element'
  396. *
  397. * Otherwise, the path prefix is set on the appropriate plugin loader.
  398. *
  399. * If $type is 'decorators', sets the path in the decorator plugin loader
  400. * for all elements. Additionally, if no $type is provided,
  401. * {@link Zend_Form_Element::addPrefixPath()} is called on each element.
  402. *
  403. * @param string $prefix
  404. * @param string $path
  405. * @param string $type
  406. * @return Zend_Form
  407. * @throws Zend_Form_Exception for invalid type
  408. */
  409. public function addPrefixPath($prefix, $path, $type = null)
  410. {
  411. $type = strtoupper($type);
  412. switch ($type) {
  413. case self::DECORATOR:
  414. case self::ELEMENT:
  415. $loader = $this->getPluginLoader($type);
  416. $loader->addPrefixPath($prefix, $path);
  417. return $this;
  418. case null:
  419. $prefix = rtrim($prefix, '_');
  420. $path = rtrim($path, DIRECTORY_SEPARATOR);
  421. foreach (array(self::DECORATOR, self::ELEMENT) as $type) {
  422. $cType = ucfirst(strtolower($type));
  423. $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
  424. $pluginPrefix = $prefix . '_' . $cType;
  425. $loader = $this->getPluginLoader($type);
  426. $loader->addPrefixPath($pluginPrefix, $pluginPath);
  427. }
  428. return $this;
  429. default:
  430. require_once 'Zend/Form/Exception.php';
  431. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  432. }
  433. }
  434. /**
  435. * Add many prefix paths at once
  436. *
  437. * @param array $spec
  438. * @return Zend_Form
  439. */
  440. public function addPrefixPaths(array $spec)
  441. {
  442. if (isset($spec['prefix']) && isset($spec['path'])) {
  443. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  444. }
  445. foreach ($spec as $type => $paths) {
  446. if (is_numeric($type) && is_array($paths)) {
  447. $type = null;
  448. if (isset($paths['prefix']) && isset($paths['path'])) {
  449. if (isset($paths['type'])) {
  450. $type = $paths['type'];
  451. }
  452. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  453. }
  454. } elseif (!is_numeric($type)) {
  455. if (!isset($paths['prefix']) || !isset($paths['path'])) {
  456. continue;
  457. }
  458. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  459. }
  460. }
  461. return $this;
  462. }
  463. /**
  464. * Add prefix path for all elements
  465. *
  466. * @param string $prefix
  467. * @param string $path
  468. * @param string $type
  469. * @return Zend_Form
  470. */
  471. public function addElementPrefixPath($prefix, $path, $type = null)
  472. {
  473. $this->_elementPrefixPaths[] = array(
  474. 'prefix' => $prefix,
  475. 'path' => $path,
  476. 'type' => $type,
  477. );
  478. foreach ($this->getElements() as $element) {
  479. $element->addPrefixPath($prefix, $path, $type);
  480. }
  481. foreach ($this->getSubForms() as $subForm) {
  482. $subForm->addElementPrefixPath($prefix, $path, $type);
  483. }
  484. return $this;
  485. }
  486. /**
  487. * Add prefix paths for all elements
  488. *
  489. * @param array $spec
  490. * @return Zend_Form
  491. */
  492. public function addElementPrefixPaths(array $spec)
  493. {
  494. $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec;
  495. foreach ($this->getElements() as $element) {
  496. $element->addPrefixPaths($spec);
  497. }
  498. return $this;
  499. }
  500. /**
  501. * Add prefix path for all display groups
  502. *
  503. * @param string $prefix
  504. * @param string $path
  505. * @return Zend_Form
  506. */
  507. public function addDisplayGroupPrefixPath($prefix, $path)
  508. {
  509. $this->_displayGroupPrefixPaths[] = array(
  510. 'prefix' => $prefix,
  511. 'path' => $path,
  512. );
  513. foreach ($this->getDisplayGroups() as $group) {
  514. $group->addPrefixPath($prefix, $path);
  515. }
  516. return $this;
  517. }
  518. /**
  519. * Add multiple display group prefix paths at once
  520. *
  521. * @param array $spec
  522. * @return Zend_Form
  523. */
  524. public function addDisplayGroupPrefixPaths(array $spec)
  525. {
  526. foreach ($spec as $key => $value) {
  527. if (is_string($value) && !is_numeric($key)) {
  528. $this->addDisplayGroupPrefixPath($key, $value);
  529. continue;
  530. }
  531. if (is_string($value) && is_numeric($key)) {
  532. continue;
  533. }
  534. if (is_array($value)) {
  535. $count = count($value);
  536. if (array_keys($value) === range(0, $count - 1)) {
  537. if ($count < 2) {
  538. continue;
  539. }
  540. $prefix = array_shift($value);
  541. $path = array_shift($value);
  542. $this->addDisplayGroupPrefixPath($prefix, $path);
  543. continue;
  544. }
  545. if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) {
  546. $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']);
  547. }
  548. }
  549. }
  550. return $this;
  551. }
  552. // Form metadata:
  553. /**
  554. * Set form attribute
  555. *
  556. * @param string $key
  557. * @param mixed $value
  558. * @return Zend_Form
  559. */
  560. public function setAttrib($key, $value)
  561. {
  562. $key = (string) $key;
  563. $this->_attribs[$key] = $value;
  564. return $this;
  565. }
  566. /**
  567. * Add multiple form attributes at once
  568. *
  569. * @param array $attribs
  570. * @return Zend_Form
  571. */
  572. public function addAttribs(array $attribs)
  573. {
  574. foreach ($attribs as $key => $value) {
  575. $this->setAttrib($key, $value);
  576. }
  577. return $this;
  578. }
  579. /**
  580. * Set multiple form attributes at once
  581. *
  582. * Overwrites any previously set attributes.
  583. *
  584. * @param array $attribs
  585. * @return Zend_Form
  586. */
  587. public function setAttribs(array $attribs)
  588. {
  589. $this->clearAttribs();
  590. return $this->addAttribs($attribs);
  591. }
  592. /**
  593. * Retrieve a single form attribute
  594. *
  595. * @param string $key
  596. * @return mixed
  597. */
  598. public function getAttrib($key)
  599. {
  600. $key = (string) $key;
  601. if (!isset($this->_attribs[$key])) {
  602. return null;
  603. }
  604. return $this->_attribs[$key];
  605. }
  606. /**
  607. * Retrieve all form attributes/metadata
  608. *
  609. * @return array
  610. */
  611. public function getAttribs()
  612. {
  613. return $this->_attribs;
  614. }
  615. /**
  616. * Remove attribute
  617. *
  618. * @param string $key
  619. * @return bool
  620. */
  621. public function removeAttrib($key)
  622. {
  623. if (isset($this->_attribs[$key])) {
  624. unset($this->_attribs[$key]);
  625. return true;
  626. }
  627. return false;
  628. }
  629. /**
  630. * Clear all form attributes
  631. *
  632. * @return Zend_Form
  633. */
  634. public function clearAttribs()
  635. {
  636. $this->_attribs = array();
  637. return $this;
  638. }
  639. /**
  640. * Set form action
  641. *
  642. * @param string $action
  643. * @return Zend_Form
  644. */
  645. public function setAction($action)
  646. {
  647. return $this->setAttrib('action', (string) $action);
  648. }
  649. /**
  650. * Get form action
  651. *
  652. * Sets default to '' if not set.
  653. *
  654. * @return string
  655. */
  656. public function getAction()
  657. {
  658. $action = $this->getAttrib('action');
  659. if (null === $action) {
  660. $action = '';
  661. $this->setAction($action);
  662. }
  663. return $action;
  664. }
  665. /**
  666. * Set form method
  667. *
  668. * Only values in {@link $_methods()} allowed
  669. *
  670. * @param string $method
  671. * @return Zend_Form
  672. * @throws Zend_Form_Exception
  673. */
  674. public function setMethod($method)
  675. {
  676. $method = strtolower($method);
  677. if (!in_array($method, $this->_methods)) {
  678. require_once 'Zend/Form/Exception.php';
  679. throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method));
  680. }
  681. $this->setAttrib('method', $method);
  682. return $this;
  683. }
  684. /**
  685. * Retrieve form method
  686. *
  687. * @return string
  688. */
  689. public function getMethod()
  690. {
  691. if (null === ($method = $this->getAttrib('method'))) {
  692. $method = self::METHOD_POST;
  693. $this->setAttrib('method', $method);
  694. }
  695. return strtolower($method);
  696. }
  697. /**
  698. * Set encoding type
  699. *
  700. * @param string $value
  701. * @return Zend_Form
  702. */
  703. public function setEnctype($value)
  704. {
  705. $this->setAttrib('enctype', $value);
  706. return $this;
  707. }
  708. /**
  709. * Get encoding type
  710. *
  711. * @return string
  712. */
  713. public function getEnctype()
  714. {
  715. if (null === ($enctype = $this->getAttrib('enctype'))) {
  716. $enctype = self::ENCTYPE_URLENCODED;
  717. $this->setAttrib('enctype', $enctype);
  718. }
  719. return $this->getAttrib('enctype');
  720. }
  721. /**
  722. * Filter a name to only allow valid variable characters
  723. *
  724. * @param string $value
  725. * @param bool $allowBrackets
  726. * @return string
  727. */
  728. public function filterName($value, $allowBrackets = false)
  729. {
  730. $charset = '^a-zA-Z0-9_\x7f-\xff';
  731. if ($allowBrackets) {
  732. $charset .= '\[\]';
  733. }
  734. return preg_replace('/[' . $charset . ']/', '', (string) $value);
  735. }
  736. /**
  737. * Set form name
  738. *
  739. * @param string $name
  740. * @return Zend_Form
  741. */
  742. public function setName($name)
  743. {
  744. $name = $this->filterName($name);
  745. if (('0' !== $name) && empty($name)) {
  746. require_once 'Zend/Form/Exception.php';
  747. throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
  748. }
  749. return $this->setAttrib('name', $name);
  750. }
  751. /**
  752. * Get name attribute
  753. *
  754. * @return null|string
  755. */
  756. public function getName()
  757. {
  758. return $this->getAttrib('name');
  759. }
  760. /**
  761. * Get fully qualified name
  762. *
  763. * Places name as subitem of array and/or appends brackets.
  764. *
  765. * @return string
  766. */
  767. public function getFullyQualifiedName()
  768. {
  769. return $this->getName();
  770. }
  771. /**
  772. * Get element id
  773. *
  774. * @return string
  775. */
  776. public function getId()
  777. {
  778. if (null !== ($id = $this->getAttrib('id'))) {
  779. return $id;
  780. }
  781. $id = $this->getFullyQualifiedName();
  782. // Bail early if no array notation detected
  783. if (!strstr($id, '[')) {
  784. return $id;
  785. }
  786. // Strip array notation
  787. if ('[]' == substr($id, -2)) {
  788. $id = substr($id, 0, strlen($id) - 2);
  789. }
  790. $id = str_replace('][', '-', $id);
  791. $id = str_replace(array(']', '['), '-', $id);
  792. $id = trim($id, '-');
  793. return $id;
  794. }
  795. /**
  796. * Set form legend
  797. *
  798. * @param string $value
  799. * @return Zend_Form
  800. */
  801. public function setLegend($value)
  802. {
  803. $this->_legend = (string) $value;
  804. return $this;
  805. }
  806. /**
  807. * Get form legend
  808. *
  809. * @return string
  810. */
  811. public function getLegend()
  812. {
  813. return $this->_legend;
  814. }
  815. /**
  816. * Set form description
  817. *
  818. * @param string $value
  819. * @return Zend_Form
  820. */
  821. public function setDescription($value)
  822. {
  823. $this->_description = (string) $value;
  824. return $this;
  825. }
  826. /**
  827. * Retrieve form description
  828. *
  829. * @return string
  830. */
  831. public function getDescription()
  832. {
  833. return $this->_description;
  834. }
  835. /**
  836. * Set form order
  837. *
  838. * @param int $index
  839. * @return Zend_Form
  840. */
  841. public function setOrder($index)
  842. {
  843. $this->_formOrder = (int) $index;
  844. return $this;
  845. }
  846. /**
  847. * Get form order
  848. *
  849. * @return int|null
  850. */
  851. public function getOrder()
  852. {
  853. return $this->_formOrder;
  854. }
  855. // Element interaction:
  856. /**
  857. * Add a new element
  858. *
  859. * $element may be either a string element type, or an object of type
  860. * Zend_Form_Element. If a string element type is provided, $name must be
  861. * provided, and $options may be optionally provided for configuring the
  862. * element.
  863. *
  864. * If a Zend_Form_Element is provided, $name may be optionally provided,
  865. * and any provided $options will be ignored.
  866. *
  867. * @param string|Zend_Form_Element $element
  868. * @param string $name
  869. * @param array|Zend_Config $options
  870. * @return Zend_Form
  871. */
  872. public function addElement($element, $name = null, $options = null)
  873. {
  874. if (is_string($element)) {
  875. if (null === $name) {
  876. require_once 'Zend/Form/Exception.php';
  877. throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
  878. }
  879. if (is_array($this->_elementDecorators)) {
  880. if (null === $options) {
  881. $options = array('decorators' => $this->_elementDecorators);
  882. } elseif ($options instanceof Zend_Config) {
  883. $options = $options->toArray();
  884. }
  885. if (is_array($options)
  886. && !array_key_exists('decorators', $options)
  887. ) {
  888. $options['decorators'] = $this->_elementDecorators;
  889. }
  890. }
  891. $this->_elements[$name] = $this->createElement($element, $name, $options);
  892. } elseif ($element instanceof Zend_Form_Element) {
  893. $prefixPaths = array();
  894. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  895. if (!empty($this->_elementPrefixPaths)) {
  896. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  897. }
  898. if (null === $name) {
  899. $name = $element->getName();
  900. }
  901. $this->_elements[$name] = $element;
  902. $this->_elements[$name]->addPrefixPaths($prefixPaths);
  903. }
  904. $this->_order[$name] = $this->_elements[$name]->getOrder();
  905. $this->_orderUpdated = true;
  906. $this->_setElementsBelongTo($name);
  907. return $this;
  908. }
  909. /**
  910. * Create an element
  911. *
  912. * Acts as a factory for creating elements. Elements created with this
  913. * method will not be attached to the form, but will contain element
  914. * settings as specified in the form object (including plugin loader
  915. * prefix paths, default decorators, etc.).
  916. *
  917. * @param string $type
  918. * @param string $name
  919. * @param array|Zend_Config $options
  920. * @return Zend_Form_Element
  921. */
  922. public function createElement($type, $name, $options = null)
  923. {
  924. if (!is_string($type)) {
  925. require_once 'Zend/Form/Exception.php';
  926. throw new Zend_Form_Exception('Element type must be a string indicating type');
  927. }
  928. if (!is_string($name)) {
  929. require_once 'Zend/Form/Exception.php';
  930. throw new Zend_Form_Exception('Element name must be a string');
  931. }
  932. $prefixPaths = array();
  933. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  934. if (!empty($this->_elementPrefixPaths)) {
  935. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  936. }
  937. if ($options instanceof Zend_Config) {
  938. $options = $options->toArray();
  939. }
  940. if ((null === $options) || !is_array($options)) {
  941. $options = array('prefixPath' => $prefixPaths);
  942. } elseif (is_array($options)) {
  943. if (array_key_exists('prefixPath', $options)) {
  944. $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
  945. } else {
  946. $options['prefixPath'] = $prefixPaths;
  947. }
  948. }
  949. $class = $this->getPluginLoader(self::ELEMENT)->load($type);
  950. $element = new $class($name, $options);
  951. return $element;
  952. }
  953. /**
  954. * Add multiple elements at once
  955. *
  956. * @param array $elements
  957. * @return Zend_Form
  958. */
  959. public function addElements(array $elements)
  960. {
  961. foreach ($elements as $key => $spec) {
  962. $name = null;
  963. if (!is_numeric($key)) {
  964. $name = $key;
  965. }
  966. if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
  967. $this->addElement($spec, $name);
  968. continue;
  969. }
  970. if (is_array($spec)) {
  971. $argc = count($spec);
  972. $options = array();
  973. if (isset($spec['type'])) {
  974. $type = $spec['type'];
  975. if (isset($spec['name'])) {
  976. $name = $spec['name'];
  977. }
  978. if (isset($spec['options'])) {
  979. $options = $spec['options'];
  980. }
  981. $this->addElement($type, $name, $options);
  982. } else {
  983. switch ($argc) {
  984. case 0:
  985. continue;
  986. case (1 <= $argc):
  987. $type = array_shift($spec);
  988. case (2 <= $argc):
  989. if (null === $name) {
  990. $name = array_shift($spec);
  991. } else {
  992. $options = array_shift($spec);
  993. }
  994. case (3 <= $argc):
  995. if (empty($options)) {
  996. $options = array_shift($spec);
  997. }
  998. default:
  999. $this->addElement($type, $name, $options);
  1000. }
  1001. }
  1002. }
  1003. }
  1004. return $this;
  1005. }
  1006. /**
  1007. * Set form elements (overwrites existing elements)
  1008. *
  1009. * @param array $elements
  1010. * @return Zend_Form
  1011. */
  1012. public function setElements(array $elements)
  1013. {
  1014. $this->clearElements();
  1015. return $this->addElements($elements);
  1016. }
  1017. /**
  1018. * Retrieve a single element
  1019. *
  1020. * @param string $name
  1021. * @return Zend_Form_Element|null
  1022. */
  1023. public function getElement($name)
  1024. {
  1025. if (array_key_exists($name, $this->_elements)) {
  1026. return $this->_elements[$name];
  1027. }
  1028. return null;
  1029. }
  1030. /**
  1031. * Retrieve all elements
  1032. *
  1033. * @return array
  1034. */
  1035. public function getElements()
  1036. {
  1037. return $this->_elements;
  1038. }
  1039. /**
  1040. * Remove element
  1041. *
  1042. * @param string $name
  1043. * @return boolean
  1044. */
  1045. public function removeElement($name)
  1046. {
  1047. $name = (string) $name;
  1048. if (isset($this->_elements[$name])) {
  1049. unset($this->_elements[$name]);
  1050. if (array_key_exists($name, $this->_order)) {
  1051. unset($this->_order[$name]);
  1052. $this->_orderUpdated = true;
  1053. } else {
  1054. foreach ($this->_displayGroups as $group) {
  1055. if (null !== $group->getElement($name)) {
  1056. $group->removeElement($name);
  1057. }
  1058. }
  1059. }
  1060. return true;
  1061. }
  1062. return false;
  1063. }
  1064. /**
  1065. * Remove all form elements
  1066. *
  1067. * @return Zend_Form
  1068. */
  1069. public function clearElements()
  1070. {
  1071. foreach (array_keys($this->_elements) as $key) {
  1072. if (array_key_exists($key, $this->_order)) {
  1073. unset($this->_order[$key]);
  1074. }
  1075. }
  1076. $this->_elements = array();
  1077. $this->_orderUpdated = true;
  1078. return $this;
  1079. }
  1080. /**
  1081. * Set default values for elements
  1082. *
  1083. * If an element's name is not specified as a key in the array, its value
  1084. * is set to null.
  1085. *
  1086. * @param array $defaults
  1087. * @return Zend_Form
  1088. */
  1089. public function setDefaults(array $defaults)
  1090. {
  1091. foreach ($this->getElements() as $name => $element) {
  1092. if (array_key_exists($name, $defaults)) {
  1093. $this->setDefault($name, $defaults[$name]);
  1094. }
  1095. }
  1096. foreach ($this->getSubForms() as $name => $form) {
  1097. if (array_key_exists($name, $defaults)) {
  1098. $form->setDefaults($defaults[$name]);
  1099. } else {
  1100. $form->setDefaults($defaults);
  1101. }
  1102. }
  1103. return $this;
  1104. }
  1105. /**
  1106. * Set default value for an element
  1107. *
  1108. * @param string $name
  1109. * @param mixed $value
  1110. * @return Zend_Form
  1111. */
  1112. public function setDefault($name, $value)
  1113. {
  1114. $name = (string) $name;
  1115. if ($element = $this->getElement($name)) {
  1116. $element->setValue($value);
  1117. } else {
  1118. if (is_scalar($value)) {
  1119. foreach ($this->getSubForms() as $subForm) {
  1120. $subForm->setDefault($name, $value);
  1121. }
  1122. } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
  1123. $subForm->setDefaults($value);
  1124. }
  1125. }
  1126. return $this;
  1127. }
  1128. /**
  1129. * Retrieve value for single element
  1130. *
  1131. * @param string $name
  1132. * @return mixed
  1133. */
  1134. public function getValue($name)
  1135. {
  1136. if ($element = $this->getElement($name)) {
  1137. return $element->getValue();
  1138. }
  1139. if ($subForm = $this->getSubForm($name)) {
  1140. return $subForm->getValues(true);
  1141. }
  1142. foreach ($this->getSubForms() as $subForm) {
  1143. if ($name == $subForm->getElementsBelongTo()) {
  1144. return $subForm->getValues(true);
  1145. }
  1146. }
  1147. return null;
  1148. }
  1149. /**
  1150. * Retrieve all form element values
  1151. *
  1152. * @param bool $suppressArrayNotation
  1153. * @return array
  1154. */
  1155. public function getValues($suppressArrayNotation = false)
  1156. {
  1157. $values = array();
  1158. foreach ($this->getElements() as $key => $element) {
  1159. if (!$element->getIgnore()) {
  1160. $values[$key] = $element->getValue();
  1161. }
  1162. }
  1163. foreach ($this->getSubForms() as $key => $subForm) {
  1164. $fValues = $this->_attachToArray($subForm->getValues(true), $subForm->getElementsBelongTo());
  1165. $values = array_merge($values, $fValues);
  1166. }
  1167. if (!$suppressArrayNotation && $this->isArray()) {
  1168. $values = $this->_attachToArray($values, $this->getElementsBelongTo());
  1169. }
  1170. return $values;
  1171. }
  1172. /**
  1173. * Get unfiltered element value
  1174. *
  1175. * @param string $name
  1176. * @return mixed
  1177. */
  1178. public function getUnfilteredValue($name)
  1179. {
  1180. if ($element = $this->getElement($name)) {
  1181. return $element->getUnfilteredValue();
  1182. }
  1183. return null;
  1184. }
  1185. /**
  1186. * Retrieve all unfiltered element values
  1187. *
  1188. * @return array
  1189. */
  1190. public function getUnfilteredValues()
  1191. {
  1192. $values = array();
  1193. foreach ($this->getElements() as $key => $element) {
  1194. $values[$key] = $element->getUnfilteredValue();
  1195. }
  1196. return $values;
  1197. }
  1198. /**
  1199. * Set all elements' filters
  1200. *
  1201. * @param array $filters
  1202. * @return Zend_Form
  1203. */
  1204. public function setElementFilters(array $filters)
  1205. {
  1206. foreach ($this->getElements() as $element) {
  1207. $element->setFilters($filters);
  1208. }
  1209. return $this;
  1210. }
  1211. /**
  1212. * Set name of array elements belong to
  1213. *
  1214. * @param string $array
  1215. * @return Zend_Form
  1216. */
  1217. public function setElementsBelongTo($array)
  1218. {
  1219. $origName = $this->getElementsBelongTo();
  1220. $name = $this->filterName($array, true);
  1221. if (empty($name)) {
  1222. $name = null;
  1223. }
  1224. $this->_elementsBelongTo = $name;
  1225. if (null === $name) {
  1226. $this->setIsArray(false);
  1227. if (null !== $origName) {
  1228. $this->_setElementsBelongTo();
  1229. }
  1230. } else {
  1231. $this->setIsArray(true);
  1232. $this->_setElementsBelongTo();
  1233. }
  1234. return $this;
  1235. }
  1236. /**
  1237. * Set array to which elements belong
  1238. *
  1239. * @param string $name Element name
  1240. * @return void
  1241. */
  1242. protected function _setElementsBelongTo($name = null)
  1243. {
  1244. $array = $this->getElementsBelongTo();
  1245. if (null === $array) {
  1246. return;
  1247. }
  1248. if (null === $name) {
  1249. foreach ($this->getElements() as $element) {
  1250. $element->setBelongsTo($array);
  1251. }
  1252. } else {
  1253. if (null !== ($element = $this->getElement($name))) {
  1254. $element->setBelongsTo($array);
  1255. }
  1256. }
  1257. }
  1258. /**
  1259. * Get name of array elements belong to
  1260. *
  1261. * @return string|null
  1262. */
  1263. public function getElementsBelongTo()
  1264. {
  1265. if ((null === $this->_elementsBelongTo) && $this->isArray()) {
  1266. $name = $this->getName();
  1267. if (!empty($name)) {
  1268. return $name;
  1269. }
  1270. }
  1271. return $this->_elementsBelongTo;
  1272. }
  1273. /**
  1274. * Set flag indicating elements belong to array
  1275. *
  1276. * @param bool $flag Value of flag
  1277. * @return Zend_Form
  1278. */
  1279. public function setIsArray($flag)
  1280. {
  1281. $this->_isArray = (bool) $flag;
  1282. return $this;
  1283. }
  1284. /**
  1285. * Get flag indicating if elements belong to an array
  1286. *
  1287. * @return bool
  1288. */
  1289. public function isArray()
  1290. {
  1291. return $this->_isArray;
  1292. }
  1293. // Element groups:
  1294. /**
  1295. * Add a form group/subform
  1296. *
  1297. * @param Zend_Form $form
  1298. * @param string $name
  1299. * @param int $order
  1300. * @return Zend_Form
  1301. */
  1302. public function addSubForm(Zend_Form $form, $name, $order = null)
  1303. {
  1304. $name = (string) $name;
  1305. foreach ($this->_loaders as $type => $loader) {
  1306. $loaderPaths = $loader->getPaths();
  1307. foreach ($loaderPaths as $prefix => $paths) {
  1308. foreach ($paths as $path) {
  1309. $form->addPrefixPath($prefix, $path, $type);
  1310. }
  1311. }
  1312. }
  1313. if (!empty($this->_elementPrefixPaths)) {
  1314. foreach ($this->_elementPrefixPaths as $spec) {
  1315. list($prefix, $path, $type) = array_values($spec);
  1316. $form->addElementPrefixPath($prefix, $path, $type);
  1317. }
  1318. }
  1319. if (!empty($this->_displayGroupPrefixPaths)) {
  1320. foreach ($this->_displayGroupPrefixPaths as $spec) {
  1321. list($prefix, $path) = array_values($spec);
  1322. $form->addDisplayGroupPrefixPath($prefix, $path);
  1323. }
  1324. }
  1325. if (null !== $order) {
  1326. $form->setOrder($order);
  1327. }
  1328. $form->setName($name);
  1329. $this->_subForms[$name] = $form;
  1330. $this->_order[$name] = $order;
  1331. $this->_orderUpdated = true;
  1332. return $this;
  1333. }
  1334. /**
  1335. * Add multiple form subForms/subforms at once
  1336. *
  1337. * @param array $subForms
  1338. * @return Zend_Form
  1339. */
  1340. public function addSubForms(array $subForms)
  1341. {
  1342. foreach ($subForms as $key => $spec) {
  1343. $name = null;
  1344. if (!is_numeric($key)) {
  1345. $name = $key;
  1346. }
  1347. if ($spec instanceof Zend_Form) {
  1348. $this->addSubForm($spec, $name);
  1349. continue;
  1350. }
  1351. if (is_array($spec)) {
  1352. $argc = count($spec);
  1353. $order = null;
  1354. switch ($argc) {
  1355. case 0:
  1356. continue;
  1357. case (1 <= $argc):
  1358. $subForm = array_shift($spec);
  1359. case (2 <= $argc):
  1360. $name = array_shift($spec);
  1361. case (3 <= $argc):
  1362. $order = array_shift($spec);
  1363. default:
  1364. $this->addSubForm($subForm, $name, $order);
  1365. }
  1366. }
  1367. }
  1368. return $this;
  1369. }
  1370. /**
  1371. * Set multiple form subForms/subforms (overwrites)
  1372. *
  1373. * @param array $subForms
  1374. * @return Zend_Form
  1375. */
  1376. public function setSubForms(array $subForms)
  1377. {
  1378. $this->clearSubForms();
  1379. return $this->addSubForms($subForms);
  1380. }
  1381. /**
  1382. * Retrieve a form subForm/subform
  1383. *
  1384. * @param string $name
  1385. * @return Zend_Form|null
  1386. */
  1387. public function getSubForm($name)
  1388. {
  1389. $name = (string) $name;
  1390. if (isset($this->_subForms[$name])) {
  1391. return $this->_subForms[$name];
  1392. }
  1393. return null;
  1394. }
  1395. /**
  1396. * Retrieve all form subForms/subforms
  1397. *
  1398. * @return array
  1399. */
  1400. public function getSubForms()
  1401. {
  1402. return $this->_subForms;
  1403. }
  1404. /**
  1405. * Remove form subForm/subform
  1406. *
  1407. * @param string $name
  1408. * @return boolean
  1409. */
  1410. public function removeSubForm($name)
  1411. {
  1412. $name = (string) $name;
  1413. if (array_key_exists($name, $this->_subForms)) {
  1414. unset($this->_subForms[$name]);
  1415. if (array_key_exists($name, $this->_order)) {
  1416. unset($this->_order[$name]);
  1417. $this->_orderUpdated = true;
  1418. }
  1419. return true;
  1420. }
  1421. return false;
  1422. }
  1423. /**
  1424. * Remove all form subForms/subforms
  1425. *
  1426. * @return Zend_Form
  1427. */
  1428. public function clearSubForms()
  1429. {
  1430. foreach (array_keys($this->_subForms) as $key) {
  1431. if (array_key_exists($key, $this->_order)) {
  1432. unset($this->_order[$key]);
  1433. }
  1434. }
  1435. $this->_subForms = array();
  1436. $this->_orderUpdated = true;
  1437. return $this;
  1438. }
  1439. // Display groups:
  1440. /**
  1441. * Set default display group class
  1442. *
  1443. * @param string $class
  1444. * @return Zend_Form
  1445. */
  1446. public function setDefaultDisplayGroupClass($class)
  1447. {
  1448. $this->_defaultDisplayGroupClass = (string) $class;
  1449. return $this;
  1450. }
  1451. /**
  1452. * Retrieve default display group class
  1453. *
  1454. * @return string
  1455. */
  1456. public function getDefaultDisplayGroupClass()
  1457. {
  1458. return $this->_defaultDisplayGroupClass;
  1459. }
  1460. /**
  1461. * Add a display group
  1462. *
  1463. * Groups named elements for display purposes.
  1464. *
  1465. * If a referenced element does not yet exist in the form, it is omitted.
  1466. *
  1467. * @param array $elements
  1468. * @param string $name
  1469. * @param array|Zend_Config $options
  1470. * @return Zend_Form
  1471. * @throws Zend_Form_Exception if no valid elements provided
  1472. */
  1473. public function addDisplayGroup(array $elements, $name, $options = null)
  1474. {
  1475. $group = array();
  1476. foreach ($elements as $element) {
  1477. if (isset($this->_elements[$element])) {
  1478. $add = $this->getElement($element);
  1479. if (null !== $add) {
  1480. unset($this->_order[$element]);
  1481. $group[] = $add;
  1482. }
  1483. }
  1484. }
  1485. if (empty($group)) {
  1486. require_once 'Zend/Form/Exception.php';
  1487. throw new Zend_Form_Exception('No valid elements specified for display group');
  1488. }
  1489. $name = (string) $name;
  1490. if (is_array($options)) {
  1491. $options['elements'] = $group;
  1492. } elseif ($options instanceof Zend_Config) {
  1493. $options = $options->toArray();
  1494. $options['elements'] = $group;
  1495. } else {
  1496. $options = array('elements' => $group);
  1497. }
  1498. if (isset($options['displayGroupClass'])) {
  1499. $class = $options['displayGroupClass'];
  1500. unset($options['displayGroupClass']);
  1501. } else {
  1502. $class = $this->getDefaultDisplayGroupClass();
  1503. }
  1504. if (!class_exists($class)) {
  1505. require_once 'Zend/Loader.php';
  1506. Zend_Loader::loadClass($class);
  1507. }
  1508. $this->_displayGroups[$name] = new $class(
  1509. $name,
  1510. $this->getPluginLoader(self::DECORATOR),
  1511. $options
  1512. );
  1513. if (!empty($this->_displayGroupPrefixPaths)) {
  1514. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1515. }
  1516. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1517. $this->_orderUpdated = true;
  1518. return $this;
  1519. }
  1520. /**
  1521. * Add a display group object (used with cloning)
  1522. *
  1523. * @param Zend_Form_DisplayGroup $group
  1524. * @param string|null $name
  1525. * @return Zend_Form
  1526. */
  1527. protected function _addDisplayGroupObject(Zend_Form_DisplayGroup $group, $name = null)
  1528. {
  1529. if (null === $name) {
  1530. $name = $group->getName();
  1531. if (empty($name)) {
  1532. require_once 'Zend/Form/Exception.php';
  1533. throw new Zend_Form_Exception('Invalid display group added; requires name');
  1534. }
  1535. }
  1536. $this->_displayGroups[$name] = $group;
  1537. if (!empty($this->_displayGroupPrefixPaths)) {
  1538. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1539. }
  1540. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1541. $this->_orderUpdated = true;
  1542. return $this;
  1543. }
  1544. /**
  1545. * Add multiple display groups at once
  1546. *
  1547. * @param array $groups
  1548. * @return Zend_Form
  1549. */
  1550. public function addDisplayGroups(array $groups)
  1551. {
  1552. foreach ($groups as $key => $spec) {
  1553. $name = null;
  1554. if (!is_numeric($key)) {
  1555. $name = $key;
  1556. }
  1557. if ($spec instanceof Zend_Form_DisplayGroup) {
  1558. $this->_addDisplayGroupObject($spec);
  1559. }
  1560. if (!is_array($spec) || empty($spec)) {
  1561. continue;
  1562. }
  1563. $argc = count($spec);
  1564. $options = array();
  1565. if (isset($spec['elements'])) {
  1566. $elements = $spec['elements'];
  1567. if (isset($spec['name'])) {
  1568. $name = $spec['name'];
  1569. }
  1570. if (isset($spec['options'])) {
  1571. $options = $spec['options'];
  1572. }
  1573. $this->addDisplayGroup($elements, $name, $options);
  1574. } else {
  1575. switch ($argc) {
  1576. case (1 <= $argc):
  1577. $elements = array_shift($spec);
  1578. if (!is_array($elements) && (null !== $name)) {
  1579. $elements = array_merge((array) $elements, $spec);
  1580. $this->addDisplayGroup($elements, $name);
  1581. break;
  1582. }
  1583. case (2 <= $argc):
  1584. if (null !== $name) {
  1585. $options = array_shift($spec);
  1586. $this->addDisplayGroup($elements, $name, $options);
  1587. break;
  1588. }
  1589. $name = array_shift($spec);
  1590. case (3 <= $argc):
  1591. $options = array_shift($spec);
  1592. default:
  1593. $this->addDisplayGroup($elements, $name, $options);
  1594. }
  1595. }
  1596. }
  1597. return $this;
  1598. }
  1599. /**
  1600. * Add multiple display groups (overwrites)
  1601. *
  1602. * @param array $groups
  1603. * @return Zend_Form
  1604. */
  1605. public function setDisplayGroups(array $groups)
  1606. {
  1607. return $this->clearDisplayGroups()
  1608. ->addDisplayGroups($groups);
  1609. }
  1610. /**
  1611. * Return a display group
  1612. *
  1613. * @param string $name
  1614. * @return Zend_Form_DisplayGroup|null
  1615. */
  1616. public function getDisplayGroup($name)
  1617. {
  1618. $name = (string) $name;
  1619. if (isset($this->_displayGroups[$name])) {
  1620. return $this->_displayGroups[$name];
  1621. }
  1622. return null;
  1623. }
  1624. /**
  1625. * Return all display groups
  1626. *
  1627. * @return array
  1628. */
  1629. public function getDisplayGroups()
  1630. {
  1631. return $this->_displayGroups;
  1632. }
  1633. /**
  1634. * Remove a display group by name
  1635. *
  1636. * @param string $name
  1637. * @return boolean
  1638. */
  1639. public function removeDisplayGroup($name)
  1640. {
  1641. $name = (string) $name;
  1642. if (array_key_exists($name, $this->_displayGroups)) {
  1643. foreach ($this->_displayGroups[$name] as $key => $element) {
  1644. if (array_key_exists($key, $this->_elements)) {
  1645. $this->_order[$key] = $element->getOrder();
  1646. $this->_orderUpdated = true;
  1647. }
  1648. }
  1649. unset($this->_displayGroups[$name]);
  1650. if (array_key_exists($name, $this->_order)) {
  1651. unset($this->_order[$name]);
  1652. $this->_orderUpdated = true;
  1653. }
  1654. return true;
  1655. }
  1656. return false;
  1657. }
  1658. /**
  1659. * Remove all display groups
  1660. *
  1661. * @return Zend_Form
  1662. */
  1663. public function clearDisplayGroups()
  1664. {
  1665. foreach ($this->_displayGroups as $key => $group) {
  1666. if (array_key_exists($key, $this->_order)) {
  1667. unset($this->_order[$key]);
  1668. }
  1669. foreach ($group as $name => $element) {
  1670. if (isset($this->_elements[$name])) {
  1671. $this->_order[$name] = $element->getOrder();
  1672. }
  1673. $this->_order[$name] = $element->getOrder();
  1674. }
  1675. }
  1676. $this->_displayGroups = array();
  1677. $this->_orderUpdated = true;
  1678. return $this;
  1679. }
  1680. // Processing
  1681. /**
  1682. * Populate form
  1683. *
  1684. * Proxies to {@link setDefaults()}
  1685. *
  1686. * @param array $values
  1687. * @return Zend_Form
  1688. */
  1689. public function populate(array $values)
  1690. {
  1691. return $this->setDefaults($values);
  1692. }
  1693. /**
  1694. * Determine array key name from given value
  1695. *
  1696. * Given a value such as foo[bar][baz], returns the last element (in this case, 'baz').
  1697. *
  1698. * @param string $value
  1699. * @return string
  1700. */
  1701. protected function _getArrayName($value)
  1702. {
  1703. if (empty($value) || !is_string($value)) {
  1704. return $value;
  1705. }
  1706. if (!strstr($value, '[')) {
  1707. return $value;
  1708. }
  1709. $endPos = strlen($value) - 1;
  1710. if (']' != $value[$endPos]) {
  1711. return $value;
  1712. }
  1713. $start = strrpos($value, '[') + 1;
  1714. $name = substr($value, $start, $endPos - $start);
  1715. return $name;
  1716. }
  1717. /**
  1718. * Extract the value by walking the array using given array path.
  1719. *
  1720. * Given an array path such as foo[bar][baz], returns the value of the last
  1721. * element (in this case, 'baz').
  1722. *
  1723. * @param array $value Array to walk
  1724. * @param string $arrayPath Array notation path of the part to extract
  1725. * @return string
  1726. */
  1727. protected function _dissolveArrayValue($value, $arrayPath)
  1728. {
  1729. // As long as we have more levels
  1730. while ($arrayPos = strpos($arrayPath, '[')) {
  1731. // Get the next key in the path
  1732. $arrayKey = trim(substr($arrayPath, 0, $arrayPos), ']');
  1733. // Set the potentially final value or the next search point in the array
  1734. if (isset($value[$arrayKey])) {
  1735. $value = $value[$arrayKey];
  1736. }
  1737. // Set the next search point in the path
  1738. $arrayPath = trim(substr($arrayPath, $arrayPos + 1), ']');
  1739. }
  1740. if (isset($value[$arrayPath])) {
  1741. $value = $value[$arrayPath];
  1742. }
  1743. return $value;
  1744. }
  1745. /**
  1746. * Converts given arrayPath to an array and attaches given value at the end of it.
  1747. *
  1748. * @param mixed $value The value to attach
  1749. * @param string $arrayPath Given array path to convert and attach to.
  1750. * @return array
  1751. */
  1752. protected function _attachToArray($value, $arrayPath)
  1753. {
  1754. // As long as we have more levels
  1755. while ($arrayPos = strrpos($arrayPath, '[')) {
  1756. // Get the next key in the path
  1757. $arrayKey = trim(substr($arrayPath, $arrayPos + 1), ']');
  1758. // Attach
  1759. $value = array($arrayKey => $value);
  1760. // Set the next search point in the path
  1761. $arrayPath = trim(substr($arrayPath, 0, $arrayPos), ']');
  1762. }
  1763. $value = array($arrayPath => $value);
  1764. return $value;
  1765. }
  1766. /**
  1767. * Validate the form
  1768. *
  1769. * @param array $data
  1770. * @return boolean
  1771. */
  1772. public function isValid($data)
  1773. {
  1774. if (!is_array($data)) {
  1775. require_once 'Zend/Form/Exception.php';
  1776. throw new Zend_Form_Exception(__CLASS__ . '::' . __METHOD__ . ' expects an array');
  1777. }
  1778. $translator = $this->getTranslator();
  1779. $valid = true;
  1780. if ($this->isArray()) {
  1781. $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
  1782. }
  1783. foreach ($this->getElements() as $key => $element) {
  1784. $element->setTranslator($translator);
  1785. if (!isset($data[$key])) {
  1786. $valid = $element->isValid(null, $data) && $valid;
  1787. } else {
  1788. $valid = $element->isValid($data[$key], $data) && $valid;
  1789. }
  1790. }
  1791. foreach ($this->getSubForms() as $key => $form) {
  1792. $form->setTranslator($translator);
  1793. if (isset($data[$key])) {
  1794. $valid = $form->isValid($data[$key]) && $valid;
  1795. } else {
  1796. $valid = $form->isValid($data) && $valid;
  1797. }
  1798. }
  1799. $this->_errorsExist = !$valid;
  1800. return $valid;
  1801. }
  1802. /**
  1803. * Validate a partial form
  1804. *
  1805. * Does not check for required flags.
  1806. *
  1807. * @param array $data
  1808. * @return boolean
  1809. */
  1810. public function isValidPartial(array $data)
  1811. {
  1812. if ($this->isArray()) {
  1813. $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
  1814. }
  1815. $translator = $this->getTranslator();
  1816. $valid = true;
  1817. $validatedSubForms = array();
  1818. foreach ($data as $key => $value) {
  1819. if (null !== ($element = $this->getElement($key))) {
  1820. if (null !== $translator) {
  1821. $element->setTranslator($translator);
  1822. }
  1823. $valid = $element->isValid($value, $data) && $valid;
  1824. } elseif (null !== ($subForm = $this->getSubForm($key))) {
  1825. if (null !== $translator) {
  1826. $subForm->setTranslator($translator);
  1827. }
  1828. $valid = $subForm->isValidPartial($data[$key]) && $valid;
  1829. $validatedSubForms[] = $key;
  1830. }
  1831. }
  1832. foreach ($this->getSubForms() as $key => $subForm) {
  1833. if (!in_array($key, $validatedSubForms)) {
  1834. if (null !== $translator) {
  1835. $subForm->setTranslator($translator);
  1836. }
  1837. $valid = $subForm->isValidPartial($data) && $valid;
  1838. }
  1839. }
  1840. $this->_errorsExist = !$valid;
  1841. return $valid;
  1842. }
  1843. /**
  1844. * Process submitted AJAX data
  1845. *
  1846. * Checks if provided $data is valid, via {@link isValidPartial()}. If so,
  1847. * it returns JSON-encoded boolean true. If not, it returns JSON-encoded
  1848. * error messages (as returned by {@link getMessages()}).
  1849. *
  1850. * @param array $data
  1851. * @return string JSON-encoded boolean true or error messages
  1852. */
  1853. public function processAjax(array $data)
  1854. {
  1855. require_once 'Zend/Json.php';
  1856. if ($this->isValidPartial($data)) {
  1857. return Zend_Json::encode(true);
  1858. }
  1859. $messages = $this->getMessages();
  1860. return Zend_Json::encode($messages);
  1861. }
  1862. /**
  1863. * Add a custom error message to return in the event of failed validation
  1864. *
  1865. * @param string $message
  1866. * @return Zend_Form
  1867. */
  1868. public function addErrorMessage($message)
  1869. {
  1870. $this->_errorMessages[] = (string) $message;
  1871. return $this;
  1872. }
  1873. /**
  1874. * Add multiple custom error messages to return in the event of failed validation
  1875. *
  1876. * @param array $messages
  1877. * @return Zend_Form
  1878. */
  1879. public function addErrorMessages(array $messages)
  1880. {
  1881. foreach ($messages as $message) {
  1882. $this->addErrorMessage($message);
  1883. }
  1884. return $this;
  1885. }
  1886. /**
  1887. * Same as addErrorMessages(), but clears custom error message stack first
  1888. *
  1889. * @param array $messages
  1890. * @return Zend_Form
  1891. */
  1892. public function setErrorMessages(array $messages)
  1893. {
  1894. $this->clearErrorMessages();
  1895. return $this->addErrorMessages($messages);
  1896. }
  1897. /**
  1898. * Retrieve custom error messages
  1899. *
  1900. * @return array
  1901. */
  1902. public function getErrorMessages()
  1903. {
  1904. return $this->_errorMessages;
  1905. }
  1906. /**
  1907. * Clear custom error messages stack
  1908. *
  1909. * @return Zend_Form
  1910. */
  1911. public function clearErrorMessages()
  1912. {
  1913. $this->_errorMessages = array();
  1914. return $this;
  1915. }
  1916. /**
  1917. * Mark the element as being in a failed validation state
  1918. *
  1919. * @return Zend_Form
  1920. */
  1921. public function markAsError()
  1922. {
  1923. $this->_errorsExist = true;
  1924. return $this;
  1925. }
  1926. /**
  1927. * Add an error message and mark element as failed validation
  1928. *
  1929. * @param string $message
  1930. * @return Zend_Form
  1931. */
  1932. public function addError($message)
  1933. {
  1934. $this->addErrorMessage($message);
  1935. $this->markAsError();
  1936. return $this;
  1937. }
  1938. /**
  1939. * Add multiple error messages and flag element as failed validation
  1940. *
  1941. * @param array $messages
  1942. * @return Zend_Form
  1943. */
  1944. public function addErrors(array $messages)
  1945. {
  1946. foreach ($messages as $message) {
  1947. $this->addError($message);
  1948. }
  1949. return $this;
  1950. }
  1951. /**
  1952. * Overwrite any previously set error messages and flag as failed validation
  1953. *
  1954. * @param array $messages
  1955. * @return Zend_Form
  1956. */
  1957. public function setErrors(array $messages)
  1958. {
  1959. $this->clearErrorMessages();
  1960. return $this->addErrors($messages);
  1961. }
  1962. public function persistData()
  1963. {
  1964. }
  1965. /**
  1966. * Are there errors in the form?
  1967. *
  1968. * @return bool
  1969. */
  1970. public function isErrors()
  1971. {
  1972. return $this->_errorsExist;
  1973. }
  1974. /**
  1975. * Get error codes for all elements failing validation
  1976. *
  1977. * @param string $name
  1978. * @return array
  1979. */
  1980. public function getErrors($name = null)
  1981. {
  1982. $errors = array();
  1983. if ((null !== $name) && isset($this->_elements[$name])) {
  1984. $errors = $this->getElement($name)->getErrors();
  1985. } elseif ((null !== $name) && isset($this->_subForms[$name])) {
  1986. $errors = $this->getSubForm($name)->getErrors();
  1987. } else {
  1988. foreach ($this->_elements as $key => $element) {
  1989. $errors[$key] = $element->getErrors();
  1990. }
  1991. foreach ($this->getSubForms() as $key => $subForm) {
  1992. $fErrors = $this->_attachToArray($subForm->getErrors(), $subForm->getElementsBelongTo());
  1993. $errors = array_merge($errors, $fErrors);
  1994. }
  1995. }
  1996. return $errors;
  1997. }
  1998. /**
  1999. * Retrieve error messages from elements failing validations
  2000. *
  2001. * @param string $name
  2002. * @param bool $suppressArrayNotation
  2003. * @return array
  2004. */
  2005. public function getMessages($name = null, $suppressArrayNotation = false)
  2006. {
  2007. if ((null !== $name) && isset($this->_elements[$name])) {
  2008. return $this->getElement($name)->getMessages();
  2009. }
  2010. if ((null !== $name) && isset($this->_subForms[$name])) {
  2011. return $this->getSubForm($name)->getMessages(null, true);
  2012. }
  2013. $arrayKeys = array();
  2014. foreach ($this->getSubForms() as $key => $subForm) {
  2015. $array = $this->_getArrayName($subForm->getElementsBelongTo());
  2016. if (!empty($array)) {
  2017. if ($name == $array) {
  2018. return $subForm->getMessages(null, true);
  2019. }
  2020. $arrayKeys[$key] = $subForm->getElementsBelongTo();
  2021. }
  2022. }
  2023. $customMessages = $this->_getErrorMessages();
  2024. if ($this->isErrors() && !empty($customMessages)) {
  2025. return $customMessages;
  2026. }
  2027. $messages = array();
  2028. foreach ($this->getElements() as $name => $element) {
  2029. $eMessages = $element->getMessages();
  2030. if (!empty($eMessages)) {
  2031. $messages[$name] = $eMessages;
  2032. }
  2033. }
  2034. foreach ($this->getSubForms() as $key => $subForm) {
  2035. $fMessages = $subForm->getMessages(null, true);
  2036. if (!empty($fMessages)) {
  2037. if (array_key_exists($key, $arrayKeys)) {
  2038. $fMessages = $this->_attachToArray($fMessages, $arrayKeys[$key]);
  2039. $messages = array_merge($messages, $fMessages);
  2040. } else {
  2041. $messages[$key] = $fMessages;
  2042. }
  2043. }
  2044. }
  2045. if (!$suppressArrayNotation && $this->isArray()) {
  2046. $messages = $this->_attachToArray($messages, $this->getElementsBelongTo());
  2047. }
  2048. return $messages;
  2049. }
  2050. // Rendering
  2051. /**
  2052. * Set view object
  2053. *
  2054. * @param Zend_View_Interface $view
  2055. * @return Zend_Form
  2056. */
  2057. public function setView(Zend_View_Interface $view = null)
  2058. {
  2059. $this->_view = $view;
  2060. return $this;
  2061. }
  2062. /**
  2063. * Retrieve view object
  2064. *
  2065. * If none registered, attempts to pull from ViewRenderer.
  2066. *
  2067. * @return Zend_View_Interface|null
  2068. */
  2069. public function getView()
  2070. {
  2071. if (null === $this->_view) {
  2072. require_once 'Zend/Controller/Action/HelperBroker.php';
  2073. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2074. $this->setView($viewRenderer->view);
  2075. }
  2076. return $this->_view;
  2077. }
  2078. /**
  2079. * Instantiate a decorator based on class name or class name fragment
  2080. *
  2081. * @param string $name
  2082. * @param null|array $options
  2083. * @return Zend_Form_Decorator_Interface
  2084. */
  2085. protected function _getDecorator($name, $options)
  2086. {
  2087. $class = $this->getPluginLoader(self::DECORATOR)->load($name);
  2088. if (null === $options) {
  2089. $decorator = new $class;
  2090. } else {
  2091. $decorator = new $class($options);
  2092. }
  2093. return $decorator;
  2094. }
  2095. /**
  2096. * Add a decorator for rendering the element
  2097. *
  2098. * @param string|Zend_Form_Decorator_Interface $decorator
  2099. * @param array|Zend_Config $options Options with which to initialize decorator
  2100. * @return Zend_Form
  2101. */
  2102. public function addDecorator($decorator, $options = null)
  2103. {
  2104. if ($decorator instanceof Zend_Form_Decorator_Interface) {
  2105. $name = get_class($decorator);
  2106. } elseif (is_string($decorator)) {
  2107. $name = $decorator;
  2108. $decorator = array(
  2109. 'decorator' => $name,
  2110. 'options' => $options,
  2111. );
  2112. } elseif (is_array($decorator)) {
  2113. foreach ($decorator as $name => $spec) {
  2114. break;
  2115. }
  2116. if (is_numeric($name)) {
  2117. require_once 'Zend/Form/Exception.php';
  2118. throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
  2119. }
  2120. if (is_string($spec)) {
  2121. $decorator = array(
  2122. 'decorator' => $spec,
  2123. 'options' => $options,
  2124. );
  2125. } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
  2126. $decorator = $spec;
  2127. }
  2128. } else {
  2129. require_once 'Zend/Form/Exception.php';
  2130. throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
  2131. }
  2132. $this->_decorators[$name] = $decorator;
  2133. return $this;
  2134. }
  2135. /**
  2136. * Add many decorators at once
  2137. *
  2138. * @param array $decorators
  2139. * @return Zend_Form
  2140. */
  2141. public function addDecorators(array $decorators)
  2142. {
  2143. foreach ($decorators as $decoratorInfo) {
  2144. if (is_string($decoratorInfo)) {
  2145. $this->addDecorator($decoratorInfo);
  2146. } elseif ($decoratorInfo instanceof Zend_Form_Decorator_Interface) {
  2147. $this->addDecorator($decoratorInfo);
  2148. } elseif (is_array($decoratorInfo)) {
  2149. $argc = count($decoratorInfo);
  2150. $options = array();
  2151. if (isset($decoratorInfo['decorator'])) {
  2152. $decorator = $decoratorInfo['decorator'];
  2153. if (isset($decoratorInfo['options'])) {
  2154. $options = $decoratorInfo['options'];
  2155. }
  2156. $this->addDecorator($decorator, $options);
  2157. } else {
  2158. switch (true) {
  2159. case (0 == $argc):
  2160. break;
  2161. case (1 <= $argc):
  2162. $decorator = array_shift($decoratorInfo);
  2163. case (2 <= $argc):
  2164. $options = array_shift($decoratorInfo);
  2165. default:
  2166. $this->addDecorator($decorator, $options);
  2167. break;
  2168. }
  2169. }
  2170. } else {
  2171. require_once 'Zend/Form/Exception.php';
  2172. throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()');
  2173. }
  2174. }
  2175. return $this;
  2176. }
  2177. /**
  2178. * Overwrite all decorators
  2179. *
  2180. * @param array $decorators
  2181. * @return Zend_Form
  2182. */
  2183. public function setDecorators(array $decorators)
  2184. {
  2185. $this->clearDecorators();
  2186. return $this->addDecorators($decorators);
  2187. }
  2188. /**
  2189. * Retrieve a registered decorator
  2190. *
  2191. * @param string $name
  2192. * @return false|Zend_Form_Decorator_Abstract
  2193. */
  2194. public function getDecorator($name)
  2195. {
  2196. if (!isset($this->_decorators[$name])) {
  2197. $len = strlen($name);
  2198. foreach ($this->_decorators as $localName => $decorator) {
  2199. if ($len > strlen($localName)) {
  2200. continue;
  2201. }
  2202. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  2203. if (is_array($decorator)) {
  2204. return $this->_loadDecorator($decorator, $localName);
  2205. }
  2206. return $decorator;
  2207. }
  2208. }
  2209. return false;
  2210. }
  2211. if (is_array($this->_decorators[$name])) {
  2212. return $this->_loadDecorator($this->_decorators[$name], $name);
  2213. }
  2214. return $this->_decorators[$name];
  2215. }
  2216. /**
  2217. * Retrieve all decorators
  2218. *
  2219. * @return array
  2220. */
  2221. public function getDecorators()
  2222. {
  2223. foreach ($this->_decorators as $key => $value) {
  2224. if (is_array($value)) {
  2225. $this->_loadDecorator($value, $key);
  2226. }
  2227. }
  2228. return $this->_decorators;
  2229. }
  2230. /**
  2231. * Remove a single decorator
  2232. *
  2233. * @param string $name
  2234. * @return bool
  2235. */
  2236. public function removeDecorator($name)
  2237. {
  2238. $decorator = $this->getDecorator($name);
  2239. if ($decorator) {
  2240. if (array_key_exists($name, $this->_decorators)) {
  2241. unset($this->_decorators[$name]);
  2242. } else {
  2243. $class = get_class($decorator);
  2244. if (!array_key_exists($class, $this->_decorators)) {
  2245. return false;
  2246. }
  2247. unset($this->_decorators[$class]);
  2248. }
  2249. return true;
  2250. }
  2251. return false;
  2252. }
  2253. /**
  2254. * Clear all decorators
  2255. *
  2256. * @return Zend_Form
  2257. */
  2258. public function clearDecorators()
  2259. {
  2260. $this->_decorators = array();
  2261. return $this;
  2262. }
  2263. /**
  2264. * Set all element decorators as specified
  2265. *
  2266. * @param array $decorators
  2267. * @param array|null $elements Specific elements to decorate or exclude from decoration
  2268. * @param bool $include Whether $elements is an inclusion or exclusion list
  2269. * @return Zend_Form
  2270. */
  2271. public function setElementDecorators(array $decorators, array $elements = null, $include = true)
  2272. {
  2273. if (is_array($elements)) {
  2274. if ($include) {
  2275. $elementObjs = array();
  2276. foreach ($elements as $name) {
  2277. if (null !== ($element = $this->getElement($name))) {
  2278. $elementObjs[] = $element;
  2279. }
  2280. }
  2281. } else {
  2282. $elementObjs = $this->getElements();
  2283. foreach ($elements as $name) {
  2284. if (array_key_exists($name, $elementObjs)) {
  2285. unset($elementObjs[$name]);
  2286. }
  2287. }
  2288. }
  2289. } else {
  2290. $elementObjs = $this->getElements();
  2291. }
  2292. foreach ($elementObjs as $element) {
  2293. $element->setDecorators($decorators);
  2294. }
  2295. $this->_elementDecorators = $decorators;
  2296. return $this;
  2297. }
  2298. /**
  2299. * Set all display group decorators as specified
  2300. *
  2301. * @param array $decorators
  2302. * @return Zend_Form
  2303. */
  2304. public function setDisplayGroupDecorators(array $decorators)
  2305. {
  2306. foreach ($this->getDisplayGroups() as $group) {
  2307. $group->setDecorators($decorators);
  2308. }
  2309. return $this;
  2310. }
  2311. /**
  2312. * Set all subform decorators as specified
  2313. *
  2314. * @param array $decorators
  2315. * @return Zend_Form
  2316. */
  2317. public function setSubFormDecorators(array $decorators)
  2318. {
  2319. foreach ($this->getSubForms() as $form) {
  2320. $form->setDecorators($decorators);
  2321. }
  2322. return $this;
  2323. }
  2324. /**
  2325. * Render form
  2326. *
  2327. * @param Zend_View_Interface $view
  2328. * @return string
  2329. */
  2330. public function render(Zend_View_Interface $view = null)
  2331. {
  2332. if (null !== $view) {
  2333. $this->setView($view);
  2334. }
  2335. $content = '';
  2336. foreach ($this->getDecorators() as $decorator) {
  2337. $decorator->setElement($this);
  2338. $content = $decorator->render($content);
  2339. }
  2340. return $content;
  2341. }
  2342. /**
  2343. * Serialize as string
  2344. *
  2345. * Proxies to {@link render()}.
  2346. *
  2347. * @return string
  2348. */
  2349. public function __toString()
  2350. {
  2351. try {
  2352. $return = $this->render();
  2353. return $return;
  2354. } catch (Exception $e) {
  2355. $message = "Exception caught by form: " . $e->getMessage()
  2356. . "\nStack Trace:\n" . $e->getTraceAsString();
  2357. trigger_error($message, E_USER_WARNING);
  2358. return '';
  2359. }
  2360. }
  2361. // Localization:
  2362. /**
  2363. * Set translator object
  2364. *
  2365. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2366. * @return Zend_Form
  2367. */
  2368. public function setTranslator($translator = null)
  2369. {
  2370. if (null === $translator) {
  2371. $this->_translator = null;
  2372. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2373. $this->_translator = $translator;
  2374. } elseif ($translator instanceof Zend_Translate) {
  2375. $this->_translator = $translator->getAdapter();
  2376. } else {
  2377. require_once 'Zend/Form/Exception.php';
  2378. throw new Zend_Form_Exception('Invalid translator specified');
  2379. }
  2380. return $this;
  2381. }
  2382. /**
  2383. * Set global default translator object
  2384. *
  2385. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2386. * @return void
  2387. */
  2388. public static function setDefaultTranslator($translator = null)
  2389. {
  2390. if (null === $translator) {
  2391. self::$_translatorDefault = null;
  2392. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2393. self::$_translatorDefault = $translator;
  2394. } elseif ($translator instanceof Zend_Translate) {
  2395. self::$_translatorDefault = $translator->getAdapter();
  2396. } else {
  2397. require_once 'Zend/Form/Exception.php';
  2398. throw new Zend_Form_Exception('Invalid translator specified');
  2399. }
  2400. }
  2401. /**
  2402. * Retrieve translator object
  2403. *
  2404. * @return Zend_Translate|null
  2405. */
  2406. public function getTranslator()
  2407. {
  2408. if ($this->translatorIsDisabled()) {
  2409. return null;
  2410. }
  2411. if (null === $this->_translator) {
  2412. return self::getDefaultTranslator();
  2413. }
  2414. return $this->_translator;
  2415. }
  2416. /**
  2417. * Get global default translator object
  2418. *
  2419. * @return null|Zend_Translate
  2420. */
  2421. public static function getDefaultTranslator()
  2422. {
  2423. if (null === self::$_translatorDefault) {
  2424. require_once 'Zend/Registry.php';
  2425. if (Zend_Registry::isRegistered('Zend_Translate')) {
  2426. $translator = Zend_Registry::get('Zend_Translate');
  2427. if ($translator instanceof Zend_Translate_Adapter) {
  2428. return $translator;
  2429. } elseif ($translator instanceof Zend_Translate) {
  2430. return $translator->getAdapter();
  2431. }
  2432. }
  2433. }
  2434. return self::$_translatorDefault;
  2435. }
  2436. /**
  2437. * Indicate whether or not translation should be disabled
  2438. *
  2439. * @param bool $flag
  2440. * @return Zend_Form
  2441. */
  2442. public function setDisableTranslator($flag)
  2443. {
  2444. $this->_translatorDisabled = (bool) $flag;
  2445. return $this;
  2446. }
  2447. /**
  2448. * Is translation disabled?
  2449. *
  2450. * @return bool
  2451. */
  2452. public function translatorIsDisabled()
  2453. {
  2454. return $this->_translatorDisabled;
  2455. }
  2456. /**
  2457. * Overloading: access to elements, form groups, and display groups
  2458. *
  2459. * @param string $name
  2460. * @return Zend_Form_Element|Zend_Form|null
  2461. */
  2462. public function __get($name)
  2463. {
  2464. if (isset($this->_elements[$name])) {
  2465. return $this->_elements[$name];
  2466. } elseif (isset($this->_subForms[$name])) {
  2467. return $this->_subForms[$name];
  2468. } elseif (isset($this->_displayGroups[$name])) {
  2469. return $this->_displayGroups[$name];
  2470. }
  2471. return null;
  2472. }
  2473. /**
  2474. * Overloading: access to elements, form groups, and display groups
  2475. *
  2476. * @param string $name
  2477. * @param Zend_Form_Element|Zend_Form $value
  2478. * @return void
  2479. * @throws Zend_Form_Exception for invalid $value
  2480. */
  2481. public function __set($name, $value)
  2482. {
  2483. if ($value instanceof Zend_Form_Element) {
  2484. $this->addElement($value, $name);
  2485. return;
  2486. } elseif ($value instanceof Zend_Form) {
  2487. $this->addSubForm($value, $name);
  2488. return;
  2489. } elseif (is_array($value)) {
  2490. $this->addDisplayGroup($value, $name);
  2491. return;
  2492. }
  2493. require_once 'Zend/Form/Exception.php';
  2494. if (is_object($value)) {
  2495. $type = get_class($value);
  2496. } else {
  2497. $type = gettype($value);
  2498. }
  2499. throw new Zend_Form_Exception('Only form elements and groups may be overloaded; variable of type "' . $type . '" provided');
  2500. }
  2501. /**
  2502. * Overloading: access to elements, form groups, and display groups
  2503. *
  2504. * @param string $name
  2505. * @return boolean
  2506. */
  2507. public function __isset($name)
  2508. {
  2509. if (isset($this->_elements[$name])
  2510. || isset($this->_subForms[$name])
  2511. || isset($this->_displayGroups[$name]))
  2512. {
  2513. return true;
  2514. }
  2515. return false;
  2516. }
  2517. /**
  2518. * Overloading: access to elements, form groups, and display groups
  2519. *
  2520. * @param string $name
  2521. * @return void
  2522. */
  2523. public function __unset($name)
  2524. {
  2525. if (isset($this->_elements[$name])) {
  2526. unset($this->_elements[$name]);
  2527. } elseif (isset($this->_subForms[$name])) {
  2528. unset($this->_subForms[$name]);
  2529. } elseif (isset($this->_displayGroups[$name])) {
  2530. unset($this->_displayGroups[$name]);
  2531. }
  2532. }
  2533. /**
  2534. * Overloading: allow rendering specific decorators
  2535. *
  2536. * Call renderDecoratorName() to render a specific decorator.
  2537. *
  2538. * @param string $method
  2539. * @param array $args
  2540. * @return string
  2541. * @throws Zend_Form_Exception for invalid decorator or invalid method call
  2542. */
  2543. public function __call($method, $args)
  2544. {
  2545. if ('render' == substr($method, 0, 6)) {
  2546. $decoratorName = substr($method, 6);
  2547. if (false !== ($decorator = $this->getDecorator($decoratorName))) {
  2548. $decorator->setElement($this);
  2549. $seed = '';
  2550. if (0 < count($args)) {
  2551. $seed = array_shift($args);
  2552. }
  2553. return $decorator->render($seed);
  2554. }
  2555. require_once 'Zend/Form/Exception.php';
  2556. throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
  2557. }
  2558. require_once 'Zend/Form/Exception.php';
  2559. throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));
  2560. }
  2561. // Interfaces: Iterator, Countable
  2562. /**
  2563. * Current element/subform/display group
  2564. *
  2565. * @return Zend_Form_Element|Zend_Form_DisplayGroup|Zend_Form
  2566. */
  2567. public function current()
  2568. {
  2569. $this->_sort();
  2570. current($this->_order);
  2571. $key = key($this->_order);
  2572. if (isset($this->_elements[$key])) {
  2573. return $this->getElement($key);
  2574. } elseif (isset($this->_subForms[$key])) {
  2575. return $this->getSubForm($key);
  2576. } elseif (isset($this->_displayGroups[$key])) {
  2577. return $this->getDisplayGroup($key);
  2578. } else {
  2579. require_once 'Zend/Form/Exception.php';
  2580. throw new Zend_Form_Exception(sprintf('Corruption detected in form; invalid key ("%s") found in internal iterator', (string) $key));
  2581. }
  2582. }
  2583. /**
  2584. * Current element/subform/display group name
  2585. *
  2586. * @return string
  2587. */
  2588. public function key()
  2589. {
  2590. $this->_sort();
  2591. return key($this->_order);
  2592. }
  2593. /**
  2594. * Move pointer to next element/subform/display group
  2595. *
  2596. * @return void
  2597. */
  2598. public function next()
  2599. {
  2600. $this->_sort();
  2601. next($this->_order);
  2602. }
  2603. /**
  2604. * Move pointer to beginning of element/subform/display group loop
  2605. *
  2606. * @return void
  2607. */
  2608. public function rewind()
  2609. {
  2610. $this->_sort();
  2611. reset($this->_order);
  2612. }
  2613. /**
  2614. * Determine if current element/subform/display group is valid
  2615. *
  2616. * @return bool
  2617. */
  2618. public function valid()
  2619. {
  2620. $this->_sort();
  2621. return (current($this->_order) !== false);
  2622. }
  2623. /**
  2624. * Count of elements/subforms that are iterable
  2625. *
  2626. * @return int
  2627. */
  2628. public function count()
  2629. {
  2630. return count($this->_order);
  2631. }
  2632. /**
  2633. * Set flag to disable loading default decorators
  2634. *
  2635. * @param bool $flag
  2636. * @return Zend_Form
  2637. */
  2638. public function setDisableLoadDefaultDecorators($flag)
  2639. {
  2640. $this->_disableLoadDefaultDecorators = (bool) $flag;
  2641. return $this;
  2642. }
  2643. /**
  2644. * Should we load the default decorators?
  2645. *
  2646. * @return bool
  2647. */
  2648. public function loadDefaultDecoratorsIsDisabled()
  2649. {
  2650. return $this->_disableLoadDefaultDecorators;
  2651. }
  2652. /**
  2653. * Load the default decorators
  2654. *
  2655. * @return void
  2656. */
  2657. public function loadDefaultDecorators()
  2658. {
  2659. if ($this->loadDefaultDecoratorsIsDisabled()) {
  2660. return;
  2661. }
  2662. $decorators = $this->getDecorators();
  2663. if (empty($decorators)) {
  2664. $this->addDecorator('FormElements')
  2665. ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
  2666. ->addDecorator('Form');
  2667. }
  2668. }
  2669. /**
  2670. * Sort items according to their order
  2671. *
  2672. * @return void
  2673. */
  2674. protected function _sort()
  2675. {
  2676. if ($this->_orderUpdated) {
  2677. $items = array();
  2678. $index = 0;
  2679. foreach ($this->_order as $key => $order) {
  2680. if (null === $order) {
  2681. if (null === ($order = $this->{$key}->getOrder())) {
  2682. while (array_search($index, $this->_order, true)) {
  2683. ++$index;
  2684. }
  2685. $items[$index] = $key;
  2686. ++$index;
  2687. } else {
  2688. $items[$order] = $key;
  2689. }
  2690. } else {
  2691. $items[$order] = $key;
  2692. }
  2693. }
  2694. $items = array_flip($items);
  2695. asort($items);
  2696. $this->_order = $items;
  2697. $this->_orderUpdated = false;
  2698. }
  2699. }
  2700. /**
  2701. * Lazy-load a decorator
  2702. *
  2703. * @param array $decorator Decorator type and options
  2704. * @param mixed $name Decorator name or alias
  2705. * @return Zend_Form_Decorator_Interface
  2706. */
  2707. protected function _loadDecorator(array $decorator, $name)
  2708. {
  2709. $sameName = false;
  2710. if ($name == $decorator['decorator']) {
  2711. $sameName = true;
  2712. }
  2713. $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']);
  2714. if ($sameName) {
  2715. $newName = get_class($instance);
  2716. $decoratorNames = array_keys($this->_decorators);
  2717. $order = array_flip($decoratorNames);
  2718. $order[$newName] = $order[$name];
  2719. $decoratorsExchange = array();
  2720. unset($order[$name]);
  2721. asort($order);
  2722. foreach ($order as $key => $index) {
  2723. if ($key == $newName) {
  2724. $decoratorsExchange[$key] = $instance;
  2725. continue;
  2726. }
  2727. $decoratorsExchange[$key] = $this->_decorators[$key];
  2728. }
  2729. $this->_decorators = $decoratorsExchange;
  2730. } else {
  2731. $this->_decorators[$name] = $instance;
  2732. }
  2733. return $instance;
  2734. }
  2735. /**
  2736. * Retrieve optionally translated custom error messages
  2737. *
  2738. * @return array
  2739. */
  2740. protected function _getErrorMessages()
  2741. {
  2742. $messages = $this->getErrorMessages();
  2743. $translator = $this->getTranslator();
  2744. if (null !== $translator) {
  2745. foreach ($messages as $key => $message) {
  2746. $messages[$key] = $translator->translate($message);
  2747. }
  2748. }
  2749. return $messages;
  2750. }
  2751. }