Form.php 92 KB

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