Example: Form definition using fluent interfaces

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette\Forms example 2
  5. 5:  *
  6. 6:  * - form definition using fluent interfaces
  7. 7:  * - form groups usage
  8. 8:  * - default rendering
  9. 9:  */
  10. 10:  
  11. 11:  
  12. 12: require '../../Nette/loader.php';
  13. 13:  
  14. 14:  
  15. 15:  
  16. 17:  
  17. 18:  
  18. 19: $countries array(
  19. 20:     'Select your country',
  20. 21:     'Europe' => array(
  21. 22:         'CZ' => 'Czech Republic',
  22. 23:         'FR' => 'France',
  23. 24:         'DE' => 'Germany',
  24. 25:         'GR' => 'Greece',
  25. 26:         'HU' => 'Hungary',
  26. 27:         'IE' => 'Ireland',
  27. 28:         'IT' => 'Italy',
  28. 29:         'NL' => 'Netherlands',
  29. 30:         'PL' => 'Poland',
  30. 31:         'SK' => 'Slovakia',
  31. 32:         'ES' => 'Spain',
  32. 33:         'CH' => 'Switzerland',
  33. 34:         'UA' => 'Ukraine',
  34. 35:         'GB' => 'United Kingdom',
  35. 36:     ),
  36. 37:     'AU' => 'Australia',
  37. 38:     'CA' => 'Canada',
  38. 39:     'EG' => 'Egypt',
  39. 40:     'JP' => 'Japan',
  40. 41:     'US' => 'United States',
  41. 42:     '?'  => 'other',
  42. 43: );
  43. 44:  
  44. 45: $sex array(
  45. 46:     'm' => 'male',
  46. 47:     'f' => 'female',
  47. 48: );
  48. 49:  
  49. 50:  
  50. 51:  
  51. 52: // Step 1: Define form with validation rules
  52. 53: $form new Form;
  53. 54:  
  54. 55: // group Personal data
  55. 56: $form->addGroup('Personal data')
  56. 57:     ->setOption('description''We value your privacy and we ensure that the information you give to us will not be shared to other entities.');
  57. 58:  
  58. 59: $form->addText('name''Your name:'35)
  59. 60:     ->addRule(Form::FILLED'Enter your name');
  60. 61:  
  61. 62: $form->addText('age''Your age:'5)
  62. 63:     ->addRule(Form::FILLED'Enter your age')
  63. 64:     ->addRule(Form::INTEGER'Age must be numeric value')
  64. 65:     ->addRule(Form::RANGE'Age must be in range from %d to %d'array(10100));
  65. 66:  
  66. 67: $form->addRadioList('gender''Your gender:'$sex);
  67. 68:  
  68. 69: $form->addText('email''E-mail:'35)
  69. 70:     ->setEmptyValue('@')
  70. 71:     ->addCondition(Form::FILLED// conditional rule: if is email filled, ...
  71. 72:         ->addRule(Form::EMAIL'Incorrect E-mail Address')// ... then check email
  72. 73:  
  73. 74:  
  74. 75: // group Shipping address
  75. 76: $form->addGroup('Shipping address')
  76. 77:     ->setOption('embedNext'TRUE);
  77. 78:  
  78. 79: $form->addCheckbox('send''Ship to address')
  79. 80:     ->addCondition(Form::EQUALTRUE// conditional rule: if is checkbox checked...
  80. 81:         ->toggle('sendBox')// toggle div #sendBox
  81. 82:  
  82. 83:  
  83. 84: // subgroup
  84. 85: $form->addGroup()
  85. 86:     ->setOption('container'Html::el('div')->id('sendBox'));
  86. 87:  
  87. 88: $form->addText('street''Street:'35);
  88. 89:  
  89. 90: $form->addText('city''City:'35)
  90. 91:     ->addConditionOn($form['send']Form::EQUALTRUE)
  91. 92:         ->addRule(Form::FILLED'Enter your shipping address');
  92. 93:  
  93. 94: $form->addSelect('country''Country:'$countries)
  94. 95:     ->skipFirst()
  95. 96:     ->addConditionOn($form['send']Form::EQUALTRUE)
  96. 97:         ->addRule(Form::FILLED'Select your country');
  97. 98:  
  98. 99:  
  99. 100: // group Your account
  100. 101: $form->addGroup('Your account');
  101. 102:  
  102. 103: $form->addPassword('password''Choose password:'20)
  103. 104:     ->addRule(Form::FILLED'Choose your password')
  104. 105:     ->addRule(Form::MIN_LENGTH'The password is too short: it must be at least %d characters'3);
  105. 106:  
  106. 107: $form->addPassword('password2''Reenter password:'20)
  107. 108:     ->addConditionOn($form['password']Form::VALID)
  108. 109:         ->addRule(Form::FILLED'Reenter your password')
  109. 110:         ->addRule(Form::EQUAL'Passwords do not match'$form['password']);
  110. 111:  
  111. 112: $form->addFile('avatar''Picture:')
  112. 113:     ->addCondition(Form::FILLED)
  113. 114:         ->addRule(Form::MIME_TYPE'Uploaded file is not image''image/*');
  114. 115:  
  115. 116: $form->addHidden('userid');
  116. 117:  
  117. 118: $form->addTextArea('note''Comment:'305);
  118. 119:  
  119. 120:  
  120. 121: // group for buttons
  121. 122: $form->addGroup();
  122. 123:  
  123. 124: $form->addSubmit('submit1''Send');
  124. 125:  
  125. 126:  
  126. 127:  
  127. 128: // Step 2: Check if form was submitted?
  128. 129: if ($form->isSubmitted()) {
  129. 130:  
  130. 131:     // Step 2c: Check if form is valid
  131. 132:     if ($form->isValid()) {
  132. 133:         echo '<h2>Form was submitted and successfully validated</h2>';
  133. 134:  
  134. 135:         $values $form->getValues();
  135. 136:         Debug::dump($values);
  136. 137:  
  137. 138:         // this is the end, my friend :-)
  138. 139:         if (empty($disableExit)) exit;
  139. 140:     }
  140. 141:  
  141. 142: else {
  142. 143:     // not submitted, define default values
  143. 144:     $defaults array(
  144. 145:         'name'    => 'John Doe',
  145. 146:         'userid'  => 231,
  146. 147:         'country' => 'CZ'// Czech Republic
  147. 148:     );
  148. 149:  
  149. 150:     $form->setDefaults($defaults);
  150. 152:  
  151. 153:  
  152. 154:  
  153. 155: // Step 3: Render form
  154. 156: ?>
  155. 157: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  156. 158: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  157. 159: <head>
  158. 160:     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  159. 161:     <meta http-equiv="content-language" content="en" />
  160. 162:  
  161. 163:     <title>Nette\Forms example 2 | Nette Framework</title>
  162. 164:  
  163. 165:     <style type="text/css">
  164. 166:     <!--
  165. 167:     .required {
  166. 168:         color: darkred
  167. 169:     }
  168. 170:  
  169. 171:     fieldset {
  170. 172:         padding: .5em;
  171. 173:         margin: .3em 0;
  172. 174:         background: #EAF3FA;
  173. 175:         border: 1px solid #b2d1eb;
  174. 176:     }
  175. 177:  
  176. 178:     input.button {
  177. 179:         font-size: 120%;
  178. 180:     }
  179. 181:  
  180. 182:     th {
  181. 183:         width: 8em;
  182. 184:         text-align: right;
  183. 185:     }
  184. 186:     -->
  185. 187:     </style>
  186. 188: </head>
  187. 189:  
  188. 190: <body>
  189. 191:     <h1>Nette\Forms example 2</h1>
  190. 192:  
  191. 193:     <?php echo $form ?>
  192. 194: </body>
  193. 195: </html>