Form.php 90 KB

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