Example: Manual form rendering and separated form and rules definition

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette\Forms manual form rendering.
  5. 5:  *
  6. 6:  * - separated form and rules definition
  7. 7:  * - manual form rendering
  8. 8:  */
  9. 9:  
  10. 10:  
  11. 11: require_once '../../Nette/loader.php';
  12. 12:  
  13. 13:  
  14. 14:  
  15. 16:  
  16. 17:  
  17. 18: $countries array(
  18. 19:     'Select your country',
  19. 20:     'Europe' => array(
  20. 21:         'CZ' => 'Czech Republic',
  21. 22:         'SK' => 'Slovakia',
  22. 23:         'GB' => 'United Kingdom',
  23. 24:     ),
  24. 25:     'CA' => 'Canada',
  25. 26:     'US' => 'United States',
  26. 27:     '?'  => 'other',
  27. 28: );
  28. 29:  
  29. 30: $sex array(
  30. 31:     'm' => 'male',
  31. 32:     'f' => 'female',
  32. 33: );
  33. 34:  
  34. 35:  
  35. 36:  
  36. 37: // Step 1: Define form
  37. 38: $form new Form;
  38. 39: $form->addText('name''Your name:'35);
  39. 40: $form->addText('age''Your age:'5);
  40. 41: $form->addRadioList('gender''Your gender:'$sex);
  41. 42: $form->addText('email''E-mail:'35)->setEmptyValue('@');
  42. 43:  
  43. 44: $form->addCheckbox('send''Ship to address');
  44. 45: $form->addText('street''Street:'35);
  45. 46: $form->addText('city''City:'35);
  46. 47: $form->addSelect('country''Country:'$countries)->skipFirst();
  47. 48:  
  48. 49: $form->addPassword('password''Choose password:'20);
  49. 50: $form->addPassword('password2''Reenter password:'20);
  50. 51: $form->addFile('avatar''Picture:');
  51. 52: $form->addHidden('userid');
  52. 53: $form->addTextArea('note''Comment:'305);
  53. 54:  
  54. 55: $form->addSubmit('submit1''Send');
  55. 56:  
  56. 57:  
  57. 58: // Step 1b: Define validation rules
  58. 59: $form['name']->addRule(Form::FILLED'Enter your name');
  59. 60:  
  60. 61: $form['age']->addRule(Form::FILLED'Enter your age');
  61. 62: $form['age']->addRule(Form::INTEGER'Age must be numeric value');
  62. 63: $form['age']->addRule(Form::RANGE'Age must be in range from %d to %d'array(10100));
  63. 64:  
  64. 65: // conditional rule: if is email filled, ...
  65. 66: $form['email']->addCondition(Form::FILLED)
  66. 67:     ->addRule(Form::EMAIL'Incorrect E-mail Address')// ... then check email
  67. 68:  
  68. 69: // another conditional rule: if is checkbox checked...
  69. 70: $form['send']->addCondition(Form::EQUALTRUE)
  70. 71:     // toggle div #sendBox
  71. 72:     ->toggle('sendBox');
  72. 73:  
  73. 74: $form['city']->addConditionOn($form['send']Form::EQUALTRUE)
  74. 75:     ->addRule(Form::FILLED'Enter your shipping address');
  75. 76:  
  76. 77: $form['country']->addConditionOn($form['send']Form::EQUALTRUE)
  77. 78:     ->addRule(Form::FILLED'Select your country');
  78. 79:  
  79. 80: $form['password']->addRule(Form::FILLED'Choose your password');
  80. 81: $form['password']->addRule(Form::MIN_LENGTH'The password is too short: it must be at least %d characters'3);
  81. 82:  
  82. 83: $form['password2']->addConditionOn($form['password']Form::VALID)
  83. 84:     ->addRule(Form::FILLED'Reenter your password')
  84. 85:     ->addRule(Form::EQUAL'Passwords do not match'$form['password']);
  85. 86:  
  86. 87:  
  87. 88:  
  88. 89: // Step 2: Check if form was submitted?
  89. 90: if ($form->isSubmitted()) {
  90. 91:  
  91. 92:     // Step 2c: Check if form is valid
  92. 93:     if ($form->isValid()) {
  93. 94:         echo '<h2>Form was submitted and successfully validated</h2>';
  94. 95:  
  95. 96:         $values $form->getValues();
  96. 97:         Debug::dump($values);
  97. 98:  
  98. 99:         // this is the end, my friend :-)
  99. 100:         if (empty($disableExit)) exit;
  100. 101:     }
  101. 102:  
  102. 103: else {
  103. 104:     // not submitted, define default values
  104. 105:     $defaults array(
  105. 106:         'name'    => 'John Doe',
  106. 107:         'userid'  => 231,
  107. 108:         'country' => 'CZ'// Czech Republic
  108. 109:     );
  109. 110:  
  110. 111:     $form->setDefaults($defaults);
  111. 113:  
  112. 114:  
  113. 115:  
  114. 116: // Step 3: Render form
  115. 117: ?>
  116. 118: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  117. 119: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  118. 120: <head>
  119. 121:     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  120. 122:     <meta http-equiv="content-language" content="en" />
  121. 123:  
  122. 124:     <title>Nette\Forms manual form rendering | Nette Framework</title>
  123. 125:  
  124. 126:     <style type="text/css">
  125. 127:     <!--
  126. 128:     .required {
  127. 129:         color: darkred
  128. 130:     }
  129. 131:  
  130. 132:     fieldset {
  131. 133:         padding: .5em;
  132. 134:         margin: .3em 0;
  133. 135:         background: #EAF3FA;
  134. 136:         border: 1px solid #b2d1eb;
  135. 137:     }
  136. 138:  
  137. 139:     input.button {
  138. 140:         font-size: 120%;
  139. 141:     }
  140. 142:  
  141. 143:     th {
  142. 144:         width: 8em;
  143. 145:         text-align: right;
  144. 146:     }
  145. 147:     -->
  146. 148:     </style>
  147. 149: </head>
  148. 150:  
  149. 151: <body>
  150. 152:     <h1>Nette\Forms manual form rendering</h1>
  151. 153:  
  152. 154:     <?php $form->render('begin'?>
  153. 155:  
  154. 156:     <?php if ($form->getErrors())?>
  155. 157:     <p>Opravte chyby:</p>
  156. 158:     <?php $form->render('errors'?>
  157. 159:     <?php endif ?>
  158. 160:  
  159. 161:     <fieldset>
  160. 162:         <legend>Personal data</legend>
  161. 163:         <table>
  162. 164:         <tr class="required">
  163. 165:             <th><?php echo $form['name']->label ?></th>
  164. 166:             <td><?php echo $form['name']->control ?></td>
  165. 167:         </tr>
  166. 168:         <tr class="required">
  167. 169:             <th><?php echo $form['age']->label ?></th>
  168. 170:             <td><?php echo $form['age']->control ?></td>
  169. 171:         </tr>
  170. 172:         <tr>
  171. 173:             <th><?php echo $form['gender']->label ?></th>
  172. 174:             <td><?php echo $form['gender']->control ?></td>
  173. 175:         </tr>
  174. 176:         <tr>
  175. 177:             <th><?php echo $form['email']->label ?></th>
  176. 178:             <td><?php echo $form['email']->control ?></td>
  177. 179:         </tr>
  178. 180:         </table>
  179. 181:     </fieldset>
  180. 182:  
  181. 183:  
  182. 184:     <fieldset>
  183. 185:         <legend>Shipping address</legend>
  184. 186:  
  185. 187:         <p><?php echo $form['send']->control?><?php echo $form['send']->label ?></p>
  186. 188:  
  187. 189:         <table id="sendBox">
  188. 190:         <tr>
  189. 191:             <th><?php echo $form['street']->label ?></th>
  190. 192:             <td><?php echo $form['street']->control ?></td>
  191. 193:         </tr>
  192. 194:         <tr class="required">
  193. 195:             <th><?php echo $form['city']->label ?></th>
  194. 196:             <td><?php echo $form['city']->control ?></td>
  195. 197:         </tr>
  196. 198:         <tr class="required">
  197. 199:             <th><?php echo $form['country']->label ?></th>
  198. 200:             <td><?php echo $form['country']->control ?></td>
  199. 201:         </tr>
  200. 202:         </table>
  201. 203:     </fieldset>
  202. 204:  
  203. 205:  
  204. 206:  
  205. 207:     <fieldset>
  206. 208:         <legend>Your account</legend>
  207. 209:         <table>
  208. 210:         <tr class="required">
  209. 211:             <th><?php echo $form['password']->label ?></th>
  210. 212:             <td><?php echo $form['password']->control ?></td>
  211. 213:         </tr>
  212. 214:         <tr class="required">
  213. 215:             <th><?php echo $form['password2']->label ?></th>
  214. 216:             <td><?php echo $form['password2']->control ?></td>
  215. 217:         </tr>
  216. 218:         <tr>
  217. 219:             <th><?php echo $form['avatar']->label ?></th>
  218. 220:             <td><?php echo $form['avatar']->control ?></td>
  219. 221:         </tr>
  220. 222:         <tr>
  221. 223:             <th><?php echo $form['note']->label ?></th>
  222. 224:             <td><?php echo $form['note']->control ?></td>
  223. 225:         </tr>
  224. 226:         </table>
  225. 227:     </fieldset>
  226. 228:  
  227. 229:     <div>
  228. 230:         <?php echo $form['userid']->control ?>
  229. 231:         <?php echo $form['submit1']->control ?>
  230. 232:     </div>
  231. 233:  
  232. 234:     <?php $form->render('end')?>
  233. 235: </body>
  234. 236: </html>