Source for file FormControl.php

Documentation is available at FormControl.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__'/../../Component.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../../Forms/IFormControl.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Base class that implements the basic functionality common to form controls.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Forms
  34. 34:  *
  35. 35:  * @property-read Form $form 
  36. 36:  * @property-read mixed $control 
  37. 37:  * @property-read mixed $label 
  38. 38:  * @property-read string $htmlName 
  39. 39:  * @property   string $htmlId 
  40. 40:  * @property-read array $options 
  41. 41:  * @property   ITranslator $translator 
  42. 42:  * @property   mixed $value 
  43. 43:  * @property-read Html $controlPrototype 
  44. 44:  * @property-read Html $labelPrototype 
  45. 45:  * @property-read Rules $rules 
  46. 46:  * @property-read array $errors 
  47. 47:  * @property   bool $disabled 
  48. 48:  * @property   bool $rendered 
  49. 49:  * @property   bool $required 
  50. 50: */
  51. 51: abstract class FormControl extends Component implements IFormControl
  52. 52: {
  53. 53:     /** @var string */
  54. 54:     public static $idMask 'frm%s-%s';
  55. 55:  
  56. 56:     /** @var string textual caption or label */
  57. 57:     public $caption;
  58. 58:  
  59. 59:     /** @var mixed unfiltered control value */
  60. 60:     protected $value;
  61. 61:  
  62. 62:     /** @var Html  control element template */
  63. 63:     protected $control;
  64. 64:  
  65. 65:     /** @var Html  label element template */
  66. 66:     protected $label;
  67. 67:  
  68. 68:     /** @var array */
  69. 69:     private $errors array();
  70. 70:  
  71. 71:     /** @var bool */
  72. 72:     private $disabled FALSE;
  73. 73:  
  74. 74:     /** @var string */
  75. 75:     private $htmlId;
  76. 76:  
  77. 77:     /** @var string */
  78. 78:     private $htmlName;
  79. 79:  
  80. 80:     /** @var Rules */
  81. 81:     private $rules;
  82. 82:  
  83. 83:     /** @var ITranslator */
  84. 84:     private $translator TRUE// means autodetect
  85. 85:  
  86. 86:     /** @var array user options */
  87. 87:     private $options array();
  88. 88:  
  89. 89:  
  90. 90:  
  91. 91:     /**
  92. 92:      * @param  string  caption
  93. 93:      */
  94. 94:     public function __construct($caption NULL)
  95. 95:     {
  96. 96:         $this->monitor('Nette\Forms\Form');
  97. 97:         parent::__construct();
  98. 98:         $this->control = Html::el('input');
  99. 99:         $this->label = Html::el('label');
  100. 100:         $this->caption = $caption;
  101. 101:         $this->rules new Rules($this);
  102. 102:     }
  103. 103:  
  104. 104:  
  105. 105:  
  106. 106:     /**
  107. 107:      * This method will be called when the component becomes attached to Form.
  108. 108:      * @param  IComponent 
  109. 109:      * @return void 
  110. 110:      */
  111. 111:     protected function attached($form)
  112. 112:     {
  113. 113:         if (!$this->disabled && $form instanceof Form && $form->isAnchored(&& $form->isSubmitted()) {
  114. 114:             $this->htmlName NULL;
  115. 115:             $this->loadHttpData();
  116. 116:         }
  117. 117:     }
  118. 118:  
  119. 119:  
  120. 120:  
  121. 121:     /**
  122. 122:      * Overloaded parent setter. This method checks for invalid control name.
  123. 123:      * @param  IComponentContainer 
  124. 124:      * @param  string 
  125. 125:      * @return FormControl  provides a fluent interface
  126. 126:      */
  127. 127:     public function setParent(IComponentContainer $parent NULL$name NULL)
  128. 128:     {
  129. 129:         if ($name === 'submit'{
  130. 130:             throw new InvalidArgumentException("Name 'submit' is not allowed due to JavaScript limitations.");
  131. 131:         }
  132. 132:         return parent::setParent($parent$name);
  133. 133:     }
  134. 134:  
  135. 135:  
  136. 136:  
  137. 137:     /**
  138. 138:      * Returns form.
  139. 139:      * @param  bool   throw exception if form doesn't exist?
  140. 140:      * @return Form 
  141. 141:      */
  142. 142:     public function getForm($need TRUE)
  143. 143:     {
  144. 144:         return $this->lookup('Nette\Forms\Form'$need);
  145. 145:     }
  146. 146:  
  147. 147:  
  148. 148:  
  149. 149:     /**
  150. 150:      * Returns name of control within a Form & INamingContainer scope.
  151. 151:      * @return string 
  152. 152:      */
  153. 153:     public function getHtmlName()
  154. 154:     {
  155. 155:         if ($this->htmlName === NULL{
  156. 156:             $s '';
  157. 157:             $name $this->getName();
  158. 158:             $obj $this->lookup('Nette\Forms\INamingContainer'TRUE);
  159. 159:             while (!($obj instanceof Form)) {
  160. 160:                 $s "[$name]$s";
  161. 161:                 $name $obj->getName();
  162. 162:                 $obj $obj->lookup('Nette\Forms\INamingContainer'TRUE);
  163. 163:             }
  164. 164:             $this->htmlName "$name$s";
  165. 165:         }
  166. 166:         return $this->htmlName;
  167. 167:     }
  168. 168:  
  169. 169:  
  170. 170:  
  171. 171:     /**
  172. 172:      * Changes control's HTML id.
  173. 173:      * @param  string new ID, or FALSE or NULL
  174. 174:      * @return FormControl  provides a fluent interface
  175. 175:      */
  176. 176:     public function setHtmlId($id)
  177. 177:     {
  178. 178:         $this->htmlId $id;
  179. 179:         return $this;
  180. 180:     }
  181. 181:  
  182. 182:  
  183. 183:  
  184. 184:     /**
  185. 185:      * Returns control's HTML id.
  186. 186:      * @return string 
  187. 187:      */
  188. 188:     public function getHtmlId()
  189. 189:     {
  190. 190:         if ($this->htmlId === FALSE{
  191. 191:             return NULL;
  192. 192:  
  193. 193:         elseif ($this->htmlId === NULL{
  194. 194:             $this->htmlId sprintf(self::$idMask$this->getForm()->getName()$this->getHtmlName());
  195. 195:             $this->htmlId str_replace(array('['']')array('-''')$this->htmlId);
  196. 196:         }
  197. 197:         return $this->htmlId;
  198. 198:     }
  199. 199:  
  200. 200:  
  201. 201:  
  202. 202:     /**
  203. 203:      * Sets user-specific option.
  204. 204:      *
  205. 205:      * Common options:
  206. 206:      * - 'rendered' - indicate if method getControl() have been called
  207. 207:      * - 'required' - indicate if ':required' rule has been applied
  208. 208:      * - 'description' - textual or Html object description (recognized by ConventionalRenderer)
  209. 209:      *
  210. 210:      * @param  string key
  211. 211:      * @param  mixed  value
  212. 212:      * @return FormControl  provides a fluent interface
  213. 213:      */
  214. 214:     public function setOption($key$value)
  215. 215:     {
  216. 216:         if ($value === NULL{
  217. 217:             unset($this->options[$key]);
  218. 218:  
  219. 219:         else {
  220. 220:             $this->options[$key$value;
  221. 221:         }
  222. 222:         return $this;
  223. 223:     }
  224. 224:  
  225. 225:  
  226. 226:  
  227. 227:     /**
  228. 228:      * Returns user-specific option.
  229. 229:      * @param  string key
  230. 230:      * @param  mixed  default value
  231. 231:      * @return mixed 
  232. 232:      */
  233. 233:     final public function getOption($key$default NULL)
  234. 234:     {
  235. 235:         return isset($this->options[$key]$this->options[$key$default;
  236. 236:     }
  237. 237:  
  238. 238:  
  239. 239:  
  240. 240:     /**
  241. 241:      * Returns user-specific options.
  242. 242:      * @return array 
  243. 243:      */
  244. 244:     final public function getOptions()
  245. 245:     {
  246. 246:         return $this->options;
  247. 247:     }
  248. 248:  
  249. 249:  
  250. 250:  
  251. 251:     /********************* translator ****************d*g**/
  252. 252:  
  253. 253:  
  254. 254:  
  255. 255:     /**
  256. 256:      * Sets translate adapter.
  257. 257:      * @param  ITranslator 
  258. 258:      * @return FormControl  provides a fluent interface
  259. 259:      */
  260. 260:     public function setTranslator(ITranslator $translator NULL)
  261. 261:     {
  262. 262:         $this->translator $translator;
  263. 263:         return $this;
  264. 264:     }
  265. 265:  
  266. 266:  
  267. 267:  
  268. 268:     /**
  269. 269:      * Returns translate adapter.
  270. 270:      * @return ITranslator|NULL
  271. 271:      */
  272. 272:     final public function getTranslator()
  273. 273:     {
  274. 274:         if ($this->translator === TRUE{
  275. 275:             return $this->getForm(FALSE$this->getForm()->getTranslator(NULL;
  276. 276:         }
  277. 277:         return $this->translator;
  278. 278:     }
  279. 279:  
  280. 280:  
  281. 281:  
  282. 282:     /**
  283. 283:      * Returns translated string.
  284. 284:      * @param  string 
  285. 285:      * @return string 
  286. 286:      */
  287. 287:     public function translate($s)
  288. 288:     {
  289. 289:         $translator $this->getTranslator();
  290. 290:         return $translator === NULL $s $translator->translate($s);
  291. 291:     }
  292. 292:  
  293. 293:  
  294. 294:  
  295. 295:     /********************* interface IFormControl ****************d*g**/
  296. 296:  
  297. 297:  
  298. 298:  
  299. 299:     /**
  300. 300:      * Sets control's value.
  301. 301:      * @param  mixed 
  302. 302:      * @return FormControl  provides a fluent interface
  303. 303:      */
  304. 304:     public function setValue($value)
  305. 305:     {
  306. 306:         $this->value = $value;
  307. 307:         return $this;
  308. 308:     }
  309. 309:  
  310. 310:  
  311. 311:  
  312. 312:     /**
  313. 313:      * Returns control's value.
  314. 314:      * @return mixed 
  315. 315:      */
  316. 316:     public function getValue()
  317. 317:     {
  318. 318:         return $this->value;
  319. 319:     }
  320. 320:  
  321. 321:  
  322. 322:  
  323. 323:     /**
  324. 324:      * Sets control's default value.
  325. 325:      * @param  mixed 
  326. 326:      * @return FormControl  provides a fluent interface
  327. 327:      */
  328. 328:     public function setDefaultValue($value)
  329. 329:     {
  330. 330:         $form $this->getForm(FALSE);
  331. 331:         if (!$form || !$form->isAnchored(|| !$form->isSubmitted()) {
  332. 332:             $this->setValue($value);
  333. 333:         }
  334. 334:         return $this;
  335. 335:     }
  336. 336:  
  337. 337:  
  338. 338:  
  339. 339:     /**
  340. 340:      * Loads HTTP data.
  341. 341:      * @return void 
  342. 342:      */
  343. 343:     public function loadHttpData()
  344. 344:     {
  345. 345:         $path explode('['strtr(str_replace(']'''$this->getHtmlName())'.''_'));
  346. 346:         $this->setValue(ArrayTools::get($this->getForm()->getHttpData()$path));
  347. 347:     }
  348. 348:  
  349. 349:  
  350. 350:  
  351. 351:     /**
  352. 352:      * Disables or enables control.
  353. 353:      * @param  bool 
  354. 354:      * @return FormControl  provides a fluent interface
  355. 355:      */
  356. 356:     public function setDisabled($value TRUE)
  357. 357:     {
  358. 358:         $this->disabled = (bool) $value;
  359. 359:         return $this;
  360. 360:     }
  361. 361:  
  362. 362:  
  363. 363:  
  364. 364:     /**
  365. 365:      * Is control disabled?
  366. 366:      * @return bool 
  367. 367:      */
  368. 368:     public function isDisabled()
  369. 369:     {
  370. 370:         return $this->disabled;
  371. 371:     }
  372. 372:  
  373. 373:  
  374. 374:  
  375. 375:     /********************* rendering ****************d*g**/
  376. 376:  
  377. 377:  
  378. 378:  
  379. 379:     /**
  380. 380:      * Generates control's HTML element.
  381. 381:      * @return Html 
  382. 382:      */
  383. 383:     public function getControl()
  384. 384:     {
  385. 385:         $this->setOption('rendered'TRUE);
  386. 386:         $control clone $this->control;
  387. 387:         $control->name $this->getHtmlName();
  388. 388:         $control->disabled $this->disabled;
  389. 389:         $control->id $this->getHtmlId();
  390. 390:         return $control;
  391. 391:     }
  392. 392:  
  393. 393:  
  394. 394:  
  395. 395:     /**
  396. 396:      * Generates label's HTML element.
  397. 397:      * @param  string 
  398. 398:      * @return Html 
  399. 399:      */
  400. 400:     public function getLabel($caption NULL)
  401. 401:     {
  402. 402:         $label clone $this->label;
  403. 403:         $label->for $this->getHtmlId();
  404. 404:         if ($caption !== NULL{
  405. 405:             $label->setText($this->translate($caption));
  406. 406:  
  407. 407:         elseif ($this->caption instanceof Html{
  408. 408:             $label->add($this->caption);
  409. 409:  
  410. 410:         else {
  411. 411:             $label->setText($this->translate($this->caption));
  412. 412:         }
  413. 413:         return $label;
  414. 414:     }
  415. 415:  
  416. 416:  
  417. 417:  
  418. 418:     /**
  419. 419:      * Returns control's HTML element template.
  420. 420:      * @return Html 
  421. 421:      */
  422. 422:     final public function getControlPrototype()
  423. 423:     {
  424. 424:         return $this->control;
  425. 425:     }
  426. 426:  
  427. 427:  
  428. 428:  
  429. 429:     /**
  430. 430:      * Returns label's HTML element template.
  431. 431:      * @return Html 
  432. 432:      */
  433. 433:     final public function getLabelPrototype()
  434. 434:     {
  435. 435:         return $this->label;
  436. 436:     }
  437. 437:  
  438. 438:  
  439. 439:  
  440. 440:     /**
  441. 441:      * Sets 'rendered' indicator.
  442. 442:      * @param  bool 
  443. 443:      * @return FormControl  provides a fluent interface
  444. 444:      * @deprecated
  445. 445:      */
  446. 446:     public function setRendered($value TRUE)
  447. 447:     {
  448. 448:         $this->setOption('rendered'$value);
  449. 449:         return $this;
  450. 450:     }
  451. 451:  
  452. 452:  
  453. 453:  
  454. 454:     /**
  455. 455:      * Does method getControl() have been called?
  456. 456:      * @return bool 
  457. 457:      * @deprecated
  458. 458:      */
  459. 459:     public function isRendered()
  460. 460:     {
  461. 461:         return !empty($this->options['rendered']);
  462. 462:     }
  463. 463:  
  464. 464:  
  465. 465:  
  466. 466:     /********************* rules ****************d*g**/
  467. 467:  
  468. 468:  
  469. 469:  
  470. 470:     /**
  471. 471:      * Adds a validation rule.
  472. 472:      * @param  mixed      rule type
  473. 473:      * @param  string     message to display for invalid data
  474. 474:      * @param  mixed      optional rule arguments
  475. 475:      * @return FormControl  provides a fluent interface
  476. 476:      */
  477. 477:     public function addRule($operation$message NULL$arg NULL)
  478. 478:     {
  479. 479:         $this->rules->addRule($operation$message$arg);
  480. 480:         return $this;
  481. 481:     }
  482. 482:  
  483. 483:  
  484. 484:  
  485. 485:     /**
  486. 486:      * Adds a validation condition a returns new branch.
  487. 487:      * @param  mixed     condition type
  488. 488:      * @param  mixed      optional condition arguments
  489. 489:      * @return Rules      new branch
  490. 490:      */
  491. 491:     public function addCondition($operation$value NULL)
  492. 492:     {
  493. 493:         return $this->rules->addCondition($operation$value);
  494. 494:     }
  495. 495:  
  496. 496:  
  497. 497:  
  498. 498:     /**
  499. 499:      * Adds a validation condition based on another control a returns new branch.
  500. 500:      * @param  IFormControl form control
  501. 501:      * @param  mixed      condition type
  502. 502:      * @param  mixed      optional condition arguments
  503. 503:      * @return Rules      new branch
  504. 504:      */
  505. 505:     public function addConditionOn(IFormControl $control$operation$value NULL)
  506. 506:     {
  507. 507:         return $this->rules->addConditionOn($control$operation$value);
  508. 508:     }
  509. 509:  
  510. 510:  
  511. 511:  
  512. 512:     /**
  513. 513:      * @return Rules 
  514. 514:      */
  515. 515:     final public function getRules()
  516. 516:     {
  517. 517:         return $this->rules;
  518. 518:     }
  519. 519:  
  520. 520:  
  521. 521:  
  522. 522:     /**
  523. 523:      * Makes control mandatory.
  524. 524:      * @param  string  error message
  525. 525:      * @return FormControl  provides a fluent interface
  526. 526:      * @deprecated
  527. 527:      */
  528. 528:     final public function setRequired($message NULL)
  529. 529:     {
  530. 530:         $this->rules->addRule(':Filled'$message);
  531. 531:         return $this;
  532. 532:     }
  533. 533:  
  534. 534:  
  535. 535:  
  536. 536:     /**
  537. 537:      * Is control mandatory?
  538. 538:      * @return bool 
  539. 539:      * @deprecated
  540. 540:      */
  541. 541:     final public function isRequired()
  542. 542:     {
  543. 543:         return !empty($this->options['required']);
  544. 544:     }
  545. 545:  
  546. 546:  
  547. 547:  
  548. 548:     /**
  549. 549:      * New rule or condition notification callback.
  550. 550:      * @param  Rule 
  551. 551:      * @return void 
  552. 552:      */
  553. 553:     public function notifyRule(Rule $rule)
  554. 554:     {
  555. 555:         if (is_string($rule->operation&& strcasecmp($rule->operation':filled'=== 0{
  556. 556:             $this->setOption('required'TRUE);
  557. 557:         }
  558. 558:     }
  559. 559:  
  560. 560:  
  561. 561:  
  562. 562:     /********************* validation ****************d*g**/
  563. 563:  
  564. 564:  
  565. 565:  
  566. 566:     /**
  567. 567:      * Equal validator: are control's value and second parameter equal?
  568. 568:      * @param  IFormControl 
  569. 569:      * @param  mixed 
  570. 570:      * @return bool 
  571. 571:      */
  572. 572:     public static function validateEqual(IFormControl $control$arg)
  573. 573:     {
  574. 574:         $value = (string) $control->getValue();
  575. 575:         foreach ((is_array($arg$arg array($arg)) as $item{
  576. 576:             if ($item instanceof IFormControl{
  577. 577:                 if ($value === (string) $item->valuereturn TRUE;
  578. 578:  
  579. 579:             else {
  580. 580:                 if ($value === (string) $itemreturn TRUE;
  581. 581:             }
  582. 582:         }
  583. 583:         return FALSE;
  584. 584:     }
  585. 585:  
  586. 586:  
  587. 587:  
  588. 588:     /**
  589. 589:      * Filled validator: is control filled?
  590. 590:      * @param  IFormControl 
  591. 591:      * @return bool 
  592. 592:      */
  593. 593:     public static function validateFilled(IFormControl $control)
  594. 594:     {
  595. 595:         return (string) $control->getValue(!== ''// NULL, FALSE, '' ==> FALSE
  596. 596:     }
  597. 597:  
  598. 598:  
  599. 599:  
  600. 600:     /**
  601. 601:      * Valid validator: is control valid?
  602. 602:      * @param  IFormControl 
  603. 603:      * @return bool 
  604. 604:      */
  605. 605:     public static function validateValid(IFormControl $control)
  606. 606:     {
  607. 607:         return $control->rules->validate(TRUE);
  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:         }
  622. 622:         $this->getForm()->addError($message);
  623. 623:     }
  624. 624:  
  625. 625:  
  626. 626:  
  627. 627:     /**
  628. 628:      * Returns errors corresponding to control.
  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->errors;
  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:     }
  655. 655: