Example: Form definition using fluent interfaces

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