/tests/index.php

https://github.com/geekbuntu/php-formation · PHP · 168 lines · 124 code · 26 blank · 18 comment · 3 complexity · adf82ab79c74806ba8e899c93425473f MD5 · raw file

  1. <?php
  2. /**
  3. * Formation
  4. *
  5. * A class that builds form elements and then validates their input values for errors.
  6. * Once validated, a cleaned data result is returned, or an array of errors are returned.
  7. * Furthermore, the system is split into 2 classes, Form which deals with building,
  8. * and Formation that contains static methods for validation.
  9. *
  10. * @author Miles Johnson - http://milesj.me
  11. * @copyright Copyright 2006-2011, Miles Johnson, Inc.
  12. * @license http://opensource.org/licenses/mit-license.php - Licensed under The MIT License
  13. * @link http://milesj.me/code/php/formation
  14. */
  15. // Turn on error reporting
  16. error_reporting(E_ALL);
  17. function debug($var) {
  18. echo '<pre>'. print_r($var, true) .'</pre>';
  19. }
  20. // Include and initialize
  21. include_once '../formation/Formation.php';
  22. $form = new Form('User');
  23. $options = array(
  24. 'red' => 'Red', 'green' => 'Green', 'blue' => 'Blue', 'yellow' => 'Yellow',
  25. 'orange' => 'Orange', 'black' => 'Black', 'white' => 'White'
  26. );
  27. // Check to see if form is posted
  28. if ($form->process()) {
  29. $schema = array(
  30. 'text1' => array(
  31. 'notEmpty' => 'Text 1 is required',
  32. 'isAllChars' => 'Text 1 contains invalid characters'
  33. ),
  34. 'text2' => array(
  35. 'notEmpty' => 'Text 2 is required',
  36. 'isAlpha' => 'Text 2 may only contain letters'
  37. ),
  38. 'email' => array(
  39. 'notEmpty' => 'Email is required',
  40. 'isEmail' => 'Email is invalid'
  41. ),
  42. 'website' => array(
  43. 'isWebsite' => 'Website URL is invalid',
  44. 'required' => false
  45. ),
  46. 'password' => array(
  47. 'notEmpty' => 'Password is required',
  48. 'isAlnum' => 'Password may only be alpha-numeric',
  49. 'checkLength' => array('Password must be between 6-12 characters', 12, 6)
  50. ),
  51. 'decimal' => array(
  52. 'isDecimal' => 'Decimal must be 2 points',
  53. 'required' => false
  54. ),
  55. 'date' => array(
  56. 'notEmpty' => 'Date is required',
  57. 'isDate' => 'Date is invalid: mm/dd/yyyy'
  58. ),
  59. 'checkbox1' => array(
  60. 'notEmpty' => 'Single checkbox is required'
  61. ),
  62. 'file1' => array(
  63. 'isFile' => 'File 1 is required',
  64. 'maxFilesize' => 'Filesize is too large'
  65. ),
  66. 'file2' => array(
  67. 'required' => false,
  68. 'isExt' => 'Invalid extension',
  69. 'maxWidth' => array('Image width is too large', 100)
  70. )
  71. );
  72. // Validate form and pass
  73. if ($form->validates($schema)) {
  74. debug($form->clean());
  75. }
  76. } ?>
  77. <!DOCTYPE html>
  78. <html>
  79. <head>
  80. <title>Formation</title>
  81. <style>
  82. body { padding: 15px; font: normal 12px Arial, Tahoma, sans-serif; color: #000; }
  83. .input-error { border: 1px solid red; }
  84. </style>
  85. </head>
  86. <body>
  87. <?php // Form errors
  88. if ($errors = $form->getErrors()) { ?>
  89. <ul>
  90. <?php foreach ($errors as $error) { ?>
  91. <li><?php echo $error; ?></li>
  92. <?php } ?>
  93. </ul>
  94. <?php }
  95. // Create form
  96. echo $form->create(array('type' => 'file', 'legend' => 'Formation'));
  97. echo $form->hidden('hidden', array('value' => 'Hidden value')); ?>
  98. <p><?php echo $form->label('text1', 'Text 1: Required, Alpha-numeric, Punctuation'); ?><br>
  99. <?php echo $form->text('text1'); ?></p>
  100. <p><?php echo $form->label('text2', 'Text 2: Required, Alpha'); ?><br>
  101. <?php echo $form->text('text2'); ?></p>
  102. <p><?php echo $form->label('email', 'Email: Required, Email'); ?><br>
  103. <?php echo $form->text('email'); ?></p>
  104. <p><?php echo $form->label('website', 'Website: Optional'); ?><br>
  105. <?php echo $form->text('website'); ?></p>
  106. <p><?php echo $form->label('password', 'Password: Required, Alpha-numeric, Within range'); ?><br>
  107. <?php echo $form->password('password'); ?></p>
  108. <p><?php echo $form->label('decimal', 'Decimal: Optional, 2 points'); ?><br>
  109. <?php echo $form->text('decimal'); ?></p>
  110. <p><?php echo $form->label('date', 'Date: Required, mm/dd/yyyy'); ?><br>
  111. <?php echo $form->text('date'); ?></p>
  112. <p><?php echo $form->label('file1', 'File 1: Required, Filesize 5MB'); ?><br>
  113. <?php echo $form->file('file1'); ?></p>
  114. <p><?php echo $form->label('file2', 'File 2: Optional, Max width 100px'); ?><br>
  115. <?php echo $form->file('file2'); ?></p>
  116. <p><?php echo $form->label('select1', 'Select:'); ?><br>
  117. <?php echo $form->select('select1', $options, array('default' => 'green')); ?></p>
  118. <p><?php echo $form->label('select2', 'Select: Multiple'); ?><br>
  119. <?php echo $form->select('select2', $options, array('default' => 'blue', 'multiple' => true)); ?></p>
  120. <p><?php echo $form->label('checkbox1', 'Checkbox: Single, Required'); ?><br>
  121. <?php echo $form->checkbox('checkbox1', array('value' => 1)); ?></p>
  122. <p>
  123. <?php echo $form->label('checkbox2', 'Checkbox: Multiple'); ?><br>
  124. <?php foreach ($options as $key => $value) {
  125. echo $form->checkbox('checkbox2', array('value' => $key, 'multiple' => true)) .' '. $value;
  126. } ?>
  127. </p>
  128. <p>
  129. <?php echo $form->label('radio', 'Radio:'); ?><br>
  130. <?php foreach ($options as $key => $value) {
  131. echo $form->radio('radio', array('value' => $key, 'default' => 'black')) .' '. $value;
  132. } ?>
  133. </p>
  134. <p>
  135. <?php echo $form->submit('Submit'); ?>
  136. <?php echo $form->reset('Reset')?>
  137. <?php echo $form->button('Button', array('class' => 'button')); ?>
  138. </p>
  139. <?php // Close form
  140. echo $form->close(); ?>
  141. </body>
  142. </html>