Example: How to use custom validator

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