Form.php 93 KB

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