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