Form.php 91 KB

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