Example: Custom form rendering

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