2
0

Form.php 93 KB

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