Form.php 97 KB

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