Example: Manual form rendering and separated form and rules definition

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