Source for file Form.php

Documentation is available at Form.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Forms
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Forms/FormContainer.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * Creates, validates and renders HTML forms.
  28. 28:  *
  29. 29:  * @author     David Grudl
  30. 30:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  31. 31:  * @package    Nette\Forms
  32. 32:  *
  33. 33:  * @example    forms/basic-example.php  Form definition using fluent interfaces
  34. 34:  * @example    forms/manual-rendering.php  Manual form rendering and separated form and rules definition
  35. 35:  * @example    forms/localization.php  Localization (with Zend_Translate)
  36. 36:  * @example    forms/custom-rendering.php  Custom form rendering
  37. 37:  * @example    forms/custom-validator.php  How to use custom validator
  38. 38:  * @example    forms/naming-containers.php  How to use naming containers
  39. 39:  * @example    forms/CSRF-protection.php  How to use Cross-Site Request Forgery (CSRF) form protection
  40. 40:  * @example    forms/custom-encoding.php  How to change charset
  41. 41:  *
  42. 42:  * @property   string $action 
  43. 43:  * @property   string $method 
  44. 44:  * @property-read array $groups 
  45. 45:  * @property   string $encoding 
  46. 46:  * @property   ITranslator $translator 
  47. 47:  * @property   array $values 
  48. 48:  * @property-read array $errors 
  49. 49:  * @property-read Html $elementPrototype 
  50. 50:  * @property   IFormRenderer $renderer 
  51. 51:  * @property-read boold $submitted 
  52. 52:  * @property-read boold $populated 
  53. 53:  * @property-read boold $valid 
  54. 54:  */
  55. 55: class Form extends FormContainer
  56. 56: {
  57. 57:     /**#@+ operation name */
  58. 58:     const EQUAL = ':equal';
  59. 59:     const IS_IN = ':equal';
  60. 60:     const FILLED = ':filled';
  61. 61:     const VALID = ':valid';
  62. 62:  
  63. 63:     // button
  64. 64:     const SUBMITTED = ':submitted';
  65. 65:  
  66. 66:     // text
  67. 67:     const MIN_LENGTH = ':minLength';
  68. 68:     const MAX_LENGTH = ':maxLength';
  69. 69:     const LENGTH = ':length';
  70. 70:     const EMAIL = ':email';
  71. 71:     const URL = ':url';
  72. 72:     const REGEXP = ':regexp';
  73. 73:     const INTEGER = ':integer';
  74. 74:     const NUMERIC = ':integer';
  75. 75:     const FLOAT = ':float';
  76. 76:     const RANGE = ':range';
  77. 77:  
  78. 78:     // file upload
  79. 79:     const MAX_FILE_SIZE = ':fileSize';
  80. 80:     const MIME_TYPE = ':mimeType';
  81. 81:  
  82. 82:     // special case
  83. 83:     const SCRIPT = 'Nette\Forms\InstantClientScript::javascript';
  84. 84:     /**#@-*/
  85. 85:  
  86. 86:     /**#@+ method */
  87. 87:     const GET = 'get';
  88. 88:     const POST = 'post';
  89. 89:     /**#@-*/
  90. 90:  
  91. 91:     /** tracker ID */
  92. 92:     const TRACKER_ID = '_form_';
  93. 93:  
  94. 94:     /** protection token ID */
  95. 95:     const PROTECTOR_ID = '_token_';
  96. 96:  
  97. 97:     /** @var array of function(Form $sender); Occurs when the form is submitted and successfully validated */
  98. 98:     public $onSubmit;
  99. 99:  
  100. 100:     /** @var array of function(Form $sender); Occurs when the form is submitted and not validated */
  101. 101:     public $onInvalidSubmit;
  102. 102:  
  103. 103:     /** @var mixed */
  104. 104:     protected $submittedBy;
  105. 105:  
  106. 106:     /** @var Html  <form> element */
  107. 107:     private $element;
  108. 108:  
  109. 109:     /** @var IFormRenderer */
  110. 110:     private $renderer;
  111. 111:  
  112. 112:     /** @var ITranslator */
  113. 113:     private $translator;
  114. 114:  
  115. 115:     /** @var array of FormGroup */
  116. 116:     private $groups array();
  117. 117:  
  118. 118:     /** @var bool */
  119. 119:     private $isPopulated FALSE;
  120. 120:  
  121. 121:     /** @var bool */
  122. 122:     private $valid;
  123. 123:  
  124. 124:     /** @var array */
  125. 125:     private $errors array();
  126. 126:  
  127. 127:     /** @var array */
  128. 128:     private $encoding 'UTF-8';
  129. 129:  
  130. 130:  
  131. 131:  
  132. 132:     /**
  133. 133:      * Form constructor.
  134. 134:      */
  135. 135:     public function __construct($name NULL$parent NULL)
  136. 136:     {
  137. 137:         $this->element Html::el('form');
  138. 138:         $this->element->action ''// RFC 1808 -> empty uri means 'this'
  139. 139:         $this->element->method 'post';
  140. 140:         $this->monitor(__CLASS__);
  141. 141:         if ($name !== NULL{
  142. 142:             $this->addTracker($name);
  143. 143:         }
  144. 144:         parent::__construct($parent$name);
  145. 145:     }
  146. 146:  
  147. 147:  
  148. 148:  
  149. 149:     /**
  150. 150:      * This method will be called when the component (or component's parent)
  151. 151:      * becomes attached to a monitored object. Do not call this method yourself.
  152. 152:      * @param  IComponent 
  153. 153:      * @return void 
  154. 154:      */
  155. 155:     protected function attached($obj)
  156. 156:     {
  157. 157:         if ($obj instanceof self{
  158. 158:             throw new InvalidStateException('Nested forms are forbidden.');
  159. 159:         }
  160. 160:     }
  161. 161:  
  162. 162:  
  163. 163:  
  164. 164:     /**
  165. 165:      * Returns self.
  166. 166:      * @return Form 
  167. 167:      */
  168. 168:     final public function getForm($need TRUE)
  169. 169:     {
  170. 170:         return $this;
  171. 171:     }
  172. 172:  
  173. 173:  
  174. 174:  
  175. 175:     /**
  176. 176:      * Sets form's action.
  177. 177:      * @param  mixed URI
  178. 178:      * @return void 
  179. 179:      */
  180. 180:     public function setAction($url)
  181. 181:     {
  182. 182:         $this->element->action $url;
  183. 183:     }
  184. 184:  
  185. 185:  
  186. 186:  
  187. 187:     /**
  188. 188:      * Returns form's action.
  189. 189:      * @return mixed URI
  190. 190:      */
  191. 191:     public function getAction()
  192. 192:     {
  193. 193:         return $this->element->action;
  194. 194:     }
  195. 195:  
  196. 196:  
  197. 197:  
  198. 198:     /**
  199. 199:      * Sets form's method.
  200. 200:      * @param  string get | post
  201. 201:      * @return void 
  202. 202:      */
  203. 203:     public function setMethod($method)
  204. 204:     {
  205. 205:         $this->element->method strtolower($method);
  206. 206:     }
  207. 207:  
  208. 208:  
  209. 209:  
  210. 210:     /**
  211. 211:      * Returns form's method.
  212. 212:      * @return string get | post
  213. 213:      */
  214. 214:     public function getMethod()
  215. 215:     {
  216. 216:         return $this->element->method;
  217. 217:     }
  218. 218:  
  219. 219:  
  220. 220:  
  221. 221:     /**
  222. 222:      * Adds distinguishing mark.
  223. 223:      * @param  string 
  224. 224:      * @return HiddenField 
  225. 225:      */
  226. 226:     public function addTracker($name)
  227. 227:     {
  228. 228:         return $this[self::TRACKER_IDnew HiddenField($name);
  229. 229:     }
  230. 230:  
  231. 231:  
  232. 232:  
  233. 233:     /**
  234. 234:      * Cross-Site Request Forgery (CSRF) form protection.
  235. 235:      * @param  string 
  236. 236:      * @param  int 
  237. 237:      * @return void 
  238. 238:      */
  239. 239:     public function addProtection($message NULL$timeout NULL)
  240. 240:     {
  241. 241:         $session $this->getSession()->getNamespace('Nette.Forms.Form/CSRF');
  242. 242:         $key "key$timeout";
  243. 243:         if (isset($session->$key)) {
  244. 244:             $token $session->$key;
  245. 245:         else {
  246. 246:             $session->$key $token md5(uniqid(''TRUE));
  247. 247:         }
  248. 248:         $session->setExpiration($timeout$key);
  249. 249:         $this[self::PROTECTOR_IDnew HiddenField($token);
  250. 250:         $this[self::PROTECTOR_ID]->addRule(':equal'empty($message'Security token did not match. Possible CSRF attack.' $message$token);
  251. 251:     }
  252. 252:  
  253. 253:  
  254. 254:  
  255. 255:     /**
  256. 256:      * Adds fieldset group to the form.
  257. 257:      * @param  string  caption
  258. 258:      * @param  bool    set this group as current
  259. 259:      * @return FormGroup 
  260. 260:      */
  261. 261:     public function addGroup($caption NULL$setAsCurrent TRUE)
  262. 262:     {
  263. 263:         $group new FormGroup;
  264. 264:         $group->setOption('label'$caption);
  265. 265:         $group->setOption('visual'TRUE);
  266. 266:  
  267. 267:         if ($setAsCurrent{
  268. 268:             $this->setCurrentGroup($group);
  269. 269:         }
  270. 270:  
  271. 271:         if (isset($this->groups[$caption])) {
  272. 272:             return $this->groups[$group;
  273. 273:         else {
  274. 274:             return $this->groups[$caption$group;
  275. 275:         }
  276. 276:     }
  277. 277:  
  278. 278:  
  279. 279:  
  280. 280:     /**
  281. 281:      * Removes fieldset group from form.
  282. 282:      * @param  string|FormGroup
  283. 283:      * @return void 
  284. 284:      */
  285. 285:     public function removeGroup($name)
  286. 286:     {
  287. 287:         if (is_string($name&& isset($this->groups[$name])) {
  288. 288:             $group $this->groups[$name];
  289. 289:  
  290. 290:         elseif ($name instanceof FormGroup && in_array($name$this->groupsTRUE)) {
  291. 291:             $group $name;
  292. 292:             $name array_search($group$this->groupsTRUE);
  293. 293:  
  294. 294:         else {
  295. 295:             throw new InvalidArgumentException("Group not found in form '$this->name'");
  296. 296:         }
  297. 297:  
  298. 298:         foreach ($group->getControls(as $control{
  299. 299:             $this->removeComponent($control);
  300. 300:         }
  301. 301:  
  302. 302:         unset($this->groups[$name]);
  303. 303:     }
  304. 304:  
  305. 305:  
  306. 306:  
  307. 307:     /**
  308. 308:      * Returns all defined groups.
  309. 309:      * @return array of FormGroup
  310. 310:      */
  311. 311:     public function getGroups()
  312. 312:     {
  313. 313:         return $this->groups;
  314. 314:     }
  315. 315:  
  316. 316:  
  317. 317:  
  318. 318:     /**
  319. 319:      * Returns the specified group.
  320. 320:      * @param  string  name
  321. 321:      * @return FormGroup 
  322. 322:      */
  323. 323:     public function getGroup($name)
  324. 324:     {
  325. 325:         return isset($this->groups[$name]$this->groups[$nameNULL;
  326. 326:     }
  327. 327:  
  328. 328:  
  329. 329:  
  330. 330:     /**
  331. 331:      * Set the encoding for the values.
  332. 332:      * @param  string 
  333. 333:      * @return void 
  334. 334:      */
  335. 335:     public function setEncoding($value)
  336. 336:     {
  337. 337:         $this->encoding empty($value'UTF-8' strtoupper($value);
  338. 338:         if ($this->encoding !== 'UTF-8' && !extension_loaded('mbstring')) {
  339. 339:             throw new Exception("The PHP extension 'mbstring' is required for this encoding but is not loaded.");
  340. 340:         }
  341. 341:     }
  342. 342:  
  343. 343:  
  344. 344:  
  345. 345:     /**
  346. 346:      * Returns the encoding.
  347. 347:      * @return string 
  348. 348:      */
  349. 349:     final public function getEncoding()
  350. 350:     {
  351. 351:         return $this->encoding;
  352. 352:     }
  353. 353:  
  354. 354:  
  355. 355:  
  356. 356:     /********************* translator ****************d*g**/
  357. 357:  
  358. 358:  
  359. 359:  
  360. 360:     /**
  361. 361:      * Sets translate adapter.
  362. 362:      * @param  ITranslator 
  363. 363:      * @return void 
  364. 364:      */
  365. 365:     public function setTranslator(ITranslator $translator NULL)
  366. 366:     {
  367. 367:         $this->translator $translator;
  368. 368:     }
  369. 369:  
  370. 370:  
  371. 371:  
  372. 372:     /**
  373. 373:      * Returns translate adapter.
  374. 374:      * @return ITranslator|NULL
  375. 375:      */
  376. 376:     final public function getTranslator()
  377. 377:     {
  378. 378:         return $this->translator;
  379. 379:     }
  380. 380:  
  381. 381:  
  382. 382:  
  383. 383:     /********************* submission ****************d*g**/
  384. 384:  
  385. 385:  
  386. 386:  
  387. 387:     /**
  388. 388:      * Tells if the form was submitted.
  389. 389:      * @return ISubmitterControl|FALSE submittor control
  390. 390:      */
  391. 391:     public function isSubmitted()
  392. 392:     {
  393. 393:         if ($this->submittedBy === NULL{
  394. 394:             $this->processHttpRequest();
  395. 395:         }
  396. 396:  
  397. 397:         return $this->submittedBy;
  398. 398:     }
  399. 399:  
  400. 400:  
  401. 401:  
  402. 402:     /**
  403. 403:      * Sets the submittor control.
  404. 404:      * @param  ISubmitterControl 
  405. 405:      * @return void 
  406. 406:      */
  407. 407:     public function setSubmittedBy(ISubmitterControl $by NULL)
  408. 408:     {
  409. 409:         $this->submittedBy = $by === NULL FALSE $by;
  410. 410:     }
  411. 411:  
  412. 412:  
  413. 413:  
  414. 414:     /**
  415. 415:      * Detects form submission and loads HTTP values.
  416. 416:      * @param  IHttpRequest  optional request object
  417. 417:      * @return void 
  418. 418:      */
  419. 419:     public function processHttpRequest($httpRequest NULL)
  420. 420:     {
  421. 421:         $this->submittedBy = FALSE;
  422. 422:  
  423. 423:         if ($httpRequest === NULL{
  424. 424:             $httpRequest $this->getHttpRequest();
  425. 425:         }
  426. 426:         $httpRequest->setEncoding($this->encoding);
  427. 427:  
  428. 428:         if (strcasecmp($this->getMethod()'post'=== 0{
  429. 429:             if (!$httpRequest->isMethod('post')) return;
  430. 430:             $data Tools::arrayMergeTree($httpRequest->getPost()$httpRequest->getFiles());
  431. 431:  
  432. 432:         else {
  433. 433:             if (!$httpRequest->isMethod('get')) return;
  434. 434:             $data $httpRequest->getQuery();
  435. 435:         }
  436. 436:  
  437. 437:         $tracker $this->getComponent(self::TRACKER_IDFALSE);
  438. 438:         if ($tracker{
  439. 439:             if (!isset($data[self::TRACKER_ID]|| $data[self::TRACKER_ID!== $tracker->getValue()) return;
  440. 440:  
  441. 441:         else {
  442. 442:             if (!count($data)) return;
  443. 443:         }
  444. 444:  
  445. 445:         $this->submittedBy = TRUE;
  446. 446:         $this->loadHttpData($data);
  447. 447:         $this->submit();
  448. 448:     }
  449. 449:  
  450. 450:  
  451. 451:  
  452. 452:     /**
  453. 453:      * Fires submit/click events.
  454. 454:      * @return void 
  455. 455:      */
  456. 456:     protected function submit()
  457. 457:     {
  458. 458:         if (!$this->isSubmitted()) {
  459. 459:             return;
  460. 460:  
  461. 461:         elseif ($this->submittedBy instanceof ISubmitterControl{
  462. 462:             if (!$this->submittedBy->getValidationScope(|| $this->isValid()) {
  463. 463:                 $this->submittedBy->click();
  464. 464:                 $this->onSubmit($this);
  465. 465:             else {
  466. 466:                 $this->submittedBy->onInvalidClick($this->submittedBy);
  467. 467:                 $this->onInvalidSubmit($this);
  468. 468:             }
  469. 469:  
  470. 470:         elseif ($this->isValid()) {
  471. 471:             $this->onSubmit($this);
  472. 472:  
  473. 473:         else {
  474. 474:             $this->onInvalidSubmit($this);
  475. 475:         }
  476. 476:     }
  477. 477:  
  478. 478:  
  479. 479:  
  480. 480:     /********************* data exchange ****************d*g**/
  481. 481:  
  482. 482:  
  483. 483:  
  484. 484:     /**
  485. 485:      * Fill-in with default values.
  486. 486:      * @param  array|Traversable values used to fill the form
  487. 487:      * @param  bool     erase other controls?
  488. 488:      * @return void 
  489. 489:      */
  490. 490:     public function setDefaults($values$erase FALSE)
  491. 491:     {
  492. 492:         if (!$this->isSubmitted()) {
  493. 493:             $this->setValues($values$erase);
  494. 494:         }
  495. 495:     }
  496. 496:  
  497. 497:  
  498. 498:  
  499. 499:     /**
  500. 500:      * Fill-in the form with HTTP data. Doesn't check if form was submitted.
  501. 501:      * @param  array    user data
  502. 502:      * @return void 
  503. 503:      */
  504. 504:     protected function loadHttpData(array $data)
  505. 505:     {
  506. 506:         $cursor $data;
  507. 507:         $iterator $this->getComponents(TRUE);
  508. 508:         foreach ($iterator as $name => $control{
  509. 509:             $sub $iterator->getSubIterator();
  510. 510:             if (!isset($sub->cursor)) {
  511. 511:                 $sub->cursor $cursor;
  512. 512:             }
  513. 513:             if ($control instanceof IFormControl && !$control->isDisabled()) {
  514. 514:                 $control->loadHttpData($sub->cursor);
  515. 515:                 if ($control instanceof ISubmitterControl && (!is_object($this->submittedBy|| $control->isSubmittedBy())) {
  516. 516:                     $this->submittedBy $control;
  517. 517:                 }
  518. 518:             }
  519. 519:             if ($control instanceof INamingContainer// going deeper
  520. 520:                 if (isset($sub->cursor[$name]&& is_array($sub->cursor[$name])) {
  521. 521:                     $cursor $sub->cursor[$name];
  522. 522:                 else {
  523. 523:                     unset($cursor);
  524. 524:                     $cursor NULL;
  525. 525:                 }
  526. 526:             }
  527. 527:         }
  528. 528:         $this->isPopulated = TRUE;
  529. 529:     }
  530. 530:  
  531. 531:  
  532. 532:  
  533. 533:     /**
  534. 534:      * Was form populated by setDefaults() or processHttpRequest() yet?
  535. 535:      * @return bool 
  536. 536:      */
  537. 537:     public function isPopulated()
  538. 538:     {
  539. 539:         return $this->isPopulated;
  540. 540:     }
  541. 541:  
  542. 542:  
  543. 543:  
  544. 544:     /**
  545. 545:      * Fill-in with values.
  546. 546:      * @param  array|Traversable values used to fill the form
  547. 547:      * @param  bool     erase other controls?
  548. 548:      * @return void 
  549. 549:      */
  550. 550:     public function setValues($values$erase FALSE)
  551. 551:     {
  552. 552:         if ($values instanceof Traversable{
  553. 553:             $values iterator_to_array($values);
  554. 554:  
  555. 555:         elseif (!is_array($values)) {
  556. 556:             throw new InvalidArgumentException("Values must be an array, " gettype($values." given.");
  557. 557:         }
  558. 558:  
  559. 559:         $cursor $values;
  560. 560:         $iterator $this->getComponents(TRUE);
  561. 561:         foreach ($iterator as $name => $control{
  562. 562:             $sub $iterator->getSubIterator();
  563. 563:             if (!isset($sub->cursor)) {
  564. 564:                 $sub->cursor $cursor;
  565. 565:             }
  566. 566:             if ($control instanceof IFormControl{
  567. 567:                 if ((is_array($sub->cursor|| $sub->cursor instanceof ArrayAccess&& array_key_exists($name$sub->cursor)) {
  568. 568:                     $control->setValue($sub->cursor[$name]);
  569. 569:  
  570. 570:                 elseif ($erase{
  571. 571:                     $control->setValue(NULL);
  572. 572:                 }
  573. 573:             }
  574. 574:             if ($control instanceof INamingContainer{
  575. 575:                 if ((is_array($sub->cursor|| $sub->cursor instanceof ArrayAccess&& isset($sub->cursor[$name])) {
  576. 576:                     $cursor $sub->cursor[$name];
  577. 577:                 else {
  578. 578:                     unset($cursor);
  579. 579:                     $cursor NULL;
  580. 580:                 }
  581. 581:             }
  582. 582:         }
  583. 583:         $this->isPopulated = TRUE;
  584. 584:     }
  585. 585:  
  586. 586:  
  587. 587:  
  588. 588:     /**
  589. 589:      * Returns the values submitted by the form.
  590. 590:      * @return array 
  591. 591:      */
  592. 592:     public function getValues()
  593. 593:     {
  594. 594:         if (!$this->isPopulated{
  595. 595:             throw new InvalidStateException('Form was not populated yet. Call method isSubmitted() or setDefaults().');
  596. 596:         }
  597. 597:  
  598. 598:         $values array();
  599. 599:         $cursor $values;
  600. 600:         $iterator $this->getComponents(TRUE);
  601. 601:         foreach ($iterator as $name => $control{
  602. 602:             $sub $iterator->getSubIterator();
  603. 603:             if (!isset($sub->cursor)) {
  604. 604:                 $sub->cursor $cursor;
  605. 605:             }
  606. 606:             if ($control instanceof IFormControl && !$control->isDisabled(&& !($control instanceof ISubmitterControl)) {
  607. 607:                 $sub->cursor[$name$control->getValue();
  608. 608:             }
  609. 609:             if ($control instanceof INamingContainer{
  610. 610:                 $cursor $sub->cursor[$name];
  611. 611:                 $cursor array();
  612. 612:             }
  613. 613:         }
  614. 614:         unset($values[self::TRACKER_ID]$values[self::PROTECTOR_ID]);
  615. 615:         return $values;
  616. 616:     }
  617. 617:  
  618. 618:  
  619. 619:  
  620. 620:     /********************* validation ****************d*g**/
  621. 621:  
  622. 622:  
  623. 623:  
  624. 624:     /**
  625. 625:      * Is form valid?
  626. 626:      * @return bool 
  627. 627:      */
  628. 628:     public function isValid()
  629. 629:     {
  630. 630:         if ($this->valid === NULL{
  631. 631:             $this->validate();
  632. 632:         }
  633. 633:         return $this->valid;
  634. 634:     }
  635. 635:  
  636. 636:  
  637. 637:  
  638. 638:     /**
  639. 639:      * Performs the server side validation.
  640. 640:      * @return void 
  641. 641:      */
  642. 642:     public function validate()
  643. 643:     {
  644. 644:         if (!$this->isPopulated{
  645. 645:             throw new InvalidStateException('Form was not populated yet. Call method isSubmitted() or setDefaults().');
  646. 646:         }
  647. 647:  
  648. 648:         $controls $this->getControls();
  649. 649:  
  650. 650:         $this->valid TRUE;
  651. 651:         foreach ($controls as $control{
  652. 652:             if (!$control->getRules()->validate()) {
  653. 653:                 $this->valid FALSE;
  654. 654:             }
  655. 655:         }
  656. 656:     }
  657. 657:  
  658. 658:  
  659. 659:  
  660. 660:     /**
  661. 661:      * Adds error message to the list.
  662. 662:      * @param  string  error message
  663. 663:      * @return void 
  664. 664:      */
  665. 665:     public function addError($message)
  666. 666:     {
  667. 667:         if (!in_array($message$this->errorsTRUE)) {
  668. 668:             $this->errors[$message;
  669. 669:             $this->valid FALSE;
  670. 670:         }
  671. 671:     }
  672. 672:  
  673. 673:  
  674. 674:  
  675. 675:     /**
  676. 676:      * Returns validation errors.
  677. 677:      * @return array 
  678. 678:      */
  679. 679:     public function getErrors()
  680. 680:     {
  681. 681:         return $this->errors;
  682. 682:     }
  683. 683:  
  684. 684:  
  685. 685:  
  686. 686:     /**
  687. 687:      * @return bool 
  688. 688:      */
  689. 689:     public function hasErrors()
  690. 690:     {
  691. 691:         return (bool) $this->getErrors();
  692. 692:     }
  693. 693:  
  694. 694:  
  695. 695:  
  696. 696:     /**
  697. 697:      * @return void 
  698. 698:      */
  699. 699:     public function cleanErrors()
  700. 700:     {
  701. 701:         $this->errors array();
  702. 702:         $this->valid NULL;
  703. 703:     }
  704. 704:  
  705. 705:  
  706. 706:  
  707. 707:     /********************* rendering ****************d*g**/
  708. 708:  
  709. 709:  
  710. 710:  
  711. 711:     /**
  712. 712:      * Returns form's HTML element template.
  713. 713:      * @return Html 
  714. 714:      */
  715. 715:     public function getElementPrototype()
  716. 716:     {
  717. 717:         return $this->element;
  718. 718:     }
  719. 719:  
  720. 720:  
  721. 721:  
  722. 722:     /**
  723. 723:      * Sets form renderer.
  724. 724:      * @param  IFormRenderer 
  725. 725:      * @return void 
  726. 726:      */
  727. 727:     public function setRenderer(IFormRenderer $renderer)
  728. 728:     {
  729. 729:         $this->renderer $renderer;
  730. 730:     }
  731. 731:  
  732. 732:  
  733. 733:  
  734. 734:     /**
  735. 735:      * Returns form renderer.
  736. 736:      * @return IFormRenderer|NULL
  737. 737:      */
  738. 738:     final public function getRenderer()
  739. 739:     {
  740. 740:         if ($this->renderer === NULL{
  741. 741:             $this->renderer new ConventionalRenderer;
  742. 742:         }
  743. 743:         return $this->renderer;
  744. 744:     }
  745. 745:  
  746. 746:  
  747. 747:  
  748. 748:     /**
  749. 749:      * Renders form.
  750. 750:      * @return void 
  751. 751:      */
  752. 752:     public function render()
  753. 753:     {
  754. 754:         $args func_get_args();
  755. 755:         array_unshift($args$this);
  756. 756:         $s call_user_func_array(array($this->getRenderer()'render')$args);
  757. 757:  
  758. 758:         if (strcmp($this->encoding'UTF-8')) {
  759. 759:             echo mb_convert_encoding($s'HTML-ENTITIES''UTF-8');
  760. 760:         else {
  761. 761:             echo $s;
  762. 762:         }
  763. 763:     }
  764. 764:  
  765. 765:  
  766. 766:  
  767. 767:     /**
  768. 768:      * Renders form to string.
  769. 769:      * @return bool  can throw exceptions? (hidden parameter)
  770. 770:      * @return string 
  771. 771:      */
  772. 772:     public function __toString()
  773. 773:     {
  774. 774:         try {
  775. 775:             if (strcmp($this->encoding'UTF-8')) {
  776. 776:                 return mb_convert_encoding($this->getRenderer()->render($this)'HTML-ENTITIES''UTF-8');
  777. 777:             else {
  778. 778:                 return $this->getRenderer()->render($this);
  779. 779:             }
  780. 780:  
  781. 781:         catch (Exception $e{
  782. 782:             if (func_get_args(&& func_get_arg(0)) {
  783. 783:                 throw $e;
  784. 784:             else {
  785. 785:                 trigger_error($e->getMessage()E_USER_WARNING);
  786. 786:                 return '';
  787. 787:             }
  788. 788:         }
  789. 789:     }
  790. 790:  
  791. 791:  
  792. 792:  
  793. 793:     /********************* backend ****************d*g**/
  794. 794:  
  795. 795:  
  796. 796:  
  797. 797:     /**
  798. 798:      * @return IHttpRequest 
  799. 799:      */
  800. 800:     protected function getHttpRequest()
  801. 801:     {
  802. 802:         return class_exists('Environment'Environment::getHttpRequest(new HttpRequest;
  803. 803:     }
  804. 804:  
  805. 805:  
  806. 806:  
  807. 807:     /**
  808. 808:      * @return Session 
  809. 809:      */
  810. 810:     protected function getSession()
  811. 811:     {
  812. 812:         return Environment::getSession();
  813. 813:     }
  814. 814: