Form.php 85 KB

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