Example: How to use custom validator

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette\Forms example 5
  5. 5:  *
  6. 6:  * - custom validator usage
  7. 7:  */
  8. 8:  
  9. 9:  
  10. 10: require '../../Nette/loader.php';
  11. 11:  
  12. 12:  
  13. 13:  
  14. 15:  
  15. 16:  
  16. 17:  
  17. 18: // Step 0: Define custom validator
  18. 19: function myValidator($item$arg)
  19. 20: {
  20. 21:     return $item->getValue($arg === 0;
  21. 22: }
  22. 23:  
  23. 24:  
  24. 25:  
  25. 26: // Step 1: Define form with validation rules
  26. 27: $form new Form;
  27. 28:  
  28. 29: $form->addText('num1''Multiple of 8:')
  29. 30:     ->addRule('myValidator''First number must be %d multiple'8);
  30. 31:  
  31. 32: $form->addText('num2''Not multiple of 5:')
  32. 33:     ->addRule(~'myValidator''Second number must not be %d multiple'5)// negative
  33. 34:  
  34. 35:  
  35. 36: $form->addSubmit('submit1''Send');
  36. 37:  
  37. 38:  
  38. 39:  
  39. 40: // Step 2: Check if form was submitted?
  40. 41: if ($form->isSubmitted()) {
  41. 42:  
  42. 43:     // Step 2c: Check if form is valid
  43. 44:     if ($form->isValid()) {
  44. 45:         echo '<h2>Form was submitted and successfully validated</h2>';
  45. 46:  
  46. 47:         $values $form->getValues();
  47. 48:         Debug::dump($values);
  48. 49:  
  49. 50:         // this is the end, my friend :-)
  50. 51:         if (empty($disableExit)) exit;
  51. 52:     }
  52. 53:  
  53. 54: else {
  54. 55:     // not submitted, define default values
  55. 56:     $defaults array(
  56. 57:         'num1'    => '5',
  57. 58:         'num2'    => '5',
  58. 59:     );
  59. 60:  
  60. 61:     $form->setDefaults($defaults);
  61. 62: }
  62. 63:  
  63. 64:  
  64. 65:  
  65. 66: // Step 3: Render form
  66. 67: ?>
  67. 68: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  68. 69: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  69. 70: <head>
  70. 71:     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  71. 72:     <meta http-equiv="content-language" content="en" />
  72. 73:  
  73. 74:     <title>Nette\Forms example 5 | Nette Framework</title>
  74. 75:  
  75. 76:     <style type="text/css">
  76. 77:     <!--
  77. 78:     .required {
  78. 79:         color: darkred
  79. 80:     }
  80. 81:  
  81. 82:     fieldset {
  82. 83:         padding: .5em;
  83. 84:         margin: .3em 0;
  84. 85:         background: #EAF3FA;
  85. 86:         border: 1px solid #b2d1eb;
  86. 87:     }
  87. 88:  
  88. 89:     input.button {
  89. 90:         font-size: 120%;
  90. 91:     }
  91. 92:  
  92. 93:     th {
  93. 94:         width: 8em;
  94. 95:         text-align: right;
  95. 96:     }
  96. 97:     -->
  97. 98:     </style>
  98. 99: </head>
  99. 100:  
  100. 101: <body>
  101. 102:     <h1>Nette\Forms example 5</h1>
  102. 103:  
  103. 104:     <?php echo $form ?>
  104. 105: </body>
  105. 106: </html>