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