Example: How to use naming containers

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette\Forms example 6
  5. 5:  *
  6. 6:  * - using naming containers
  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' => 'male',
  44. 45:     'f' => 'female',
  45. 46: );
  46. 47:  
  47. 48:  
  48. 49:  
  49. 50: // Step 1: Define form with validation rules
  50. 51: $form new Form;
  51. 52:  
  52. 53: // group First person
  53. 54: $form->addGroup('First person');
  54. 55: $sub $form->addContainer('first');
  55. 56: $sub->addText('name''Your name:'35);
  56. 57: $sub->addText('email''E-mail:'35);
  57. 58: $sub->addText('street''Street:'35);
  58. 59: $sub->addText('city''City:'35);
  59. 60: $sub->addSelect('country''Country:'$countries);
  60. 61:  
  61. 62: // group Second person
  62. 63: $form->addGroup('Second person');
  63. 64: $sub $form->addContainer('second');
  64. 65: $sub->addText('name''Your name:'35);
  65. 66: $sub->addText('email''E-mail:'35);
  66. 67: $sub->addText('street''Street:'35);
  67. 68: $sub->addText('city''City:'35);
  68. 69: $sub->addSelect('country''Country:'$countries);
  69. 70:  
  70. 71: // group for buttons
  71. 72: $form->addGroup();
  72. 73:  
  73. 74: $form->addSubmit('submit1''Send');
  74. 75:  
  75. 76:  
  76. 77:  
  77. 78: // Step 2: Check if form was submitted?
  78. 79: if ($form->isSubmitted()) {
  79. 80:  
  80. 81:     // Step 2c: Check if form is valid
  81. 82:     if ($form->isValid()) {
  82. 83:         echo '<h2>Form was submitted and successfully validated</h2>';
  83. 84:  
  84. 85:         $values $form->getValues();
  85. 86:         Debug::dump($values);
  86. 87:  
  87. 88:         // this is the end, my friend :-)
  88. 89:         if (empty($disableExit)) exit;
  89. 90:     }
  90. 91:  
  91. 92: else {
  92. 93: }
  93. 94:  
  94. 95:  
  95. 96:  
  96. 97: // Step 3: Render form
  97. 98: ?>
  98. 99: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  99. 100: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  100. 101: <head>
  101. 102:     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  102. 103:     <meta http-equiv="content-language" content="en" />
  103. 104:  
  104. 105:     <title>Nette\Forms example 6 | Nette Framework</title>
  105. 106:  
  106. 107:     <style type="text/css">
  107. 108:     <!--
  108. 109:     .required {
  109. 110:         color: darkred
  110. 111:     }
  111. 112:  
  112. 113:     fieldset {
  113. 114:         padding: .5em;
  114. 115:         margin: .3em 0;
  115. 116:         background: #EAF3FA;
  116. 117:         border: 1px solid #b2d1eb;
  117. 118:     }
  118. 119:  
  119. 120:     input.button {
  120. 121:         font-size: 120%;
  121. 122:     }
  122. 123:  
  123. 124:     th {
  124. 125:         width: 8em;
  125. 126:         text-align: right;
  126. 127:     }
  127. 128:     -->
  128. 129:     </style>
  129. 130: </head>
  130. 131:  
  131. 132: <body>
  132. 133:     <h1>Nette\Forms example 6</h1>
  133. 134:  
  134. 135:     <?php echo $form ?>
  135. 136: </body>
  136. 137: </html>