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