Posted By

ratzo on 01/16/11


Tagged

forms


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

mgerdt
vehler


Form Class


 / Published in: PHP
 

Please, feel free to suggest improvements

  1. <?php
  2.  
  3. /*
  4.  * phpForm class for create and validate forms
  5.  *
  6.  * @package phpForm
  7.  * @author Armando Monroy aka Ratzo
  8.  * @version 0.5
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  *
  11.  */
  12. class Form {
  13.  
  14. /*
  15. * Stores number of fields that failed validation
  16. * @var int
  17. */
  18. protected $errorCount = 0;
  19.  
  20. /*
  21. * Array containing all the field that failed validation and their error messages.
  22. * @var array
  23. */
  24. protected $errors = array();
  25.  
  26. /*
  27. * Array containing all the validation rules that should be applied.
  28. * @var array
  29. */
  30. protected $rules = array();
  31.  
  32. /*
  33. * Set the validation rules
  34. * @param array $rules
  35. */
  36. public function setRules($rules) {
  37.  
  38. if (!empty($rules)) {
  39. $this->rules = $rules;
  40. }
  41.  
  42. }
  43.  
  44. /*
  45. * Creates an html form tag
  46. *
  47. * @param string $action URL of the action
  48. * @param array $options Optional options like type
  49. *
  50. * @return string
  51. */
  52. public function create($action = null, $options = array()) {
  53.  
  54. if (empty($action)) {
  55. $action = $_SERVER['PHP_SELF'];
  56. }
  57.  
  58. if (!empty($options)) {
  59. $id = array_key_exists('id', $options) ? ' id ="' . $options['id'] . '"' : null;
  60. }
  61.  
  62. $out = '<form method="post" action="' . $action . '"';
  63. if (array_key_exists('type', $options) && ($options['type'] == 'file')) {
  64. $out .= ' enctype="multipart/form-data"';
  65. }
  66. $out .= $id . '>';
  67. echo $out;
  68. return true;
  69.  
  70. }
  71.  
  72. /*
  73. * Creates an html closing form tag
  74. *
  75. * @param string $message Optional parameter wich display a submit button if present
  76. */
  77. public function end($message = null) {
  78.  
  79. $out = null;
  80.  
  81. if (!empty($message)) {
  82. $out = '<p class="submit"><input type="submit" value="' . $message . '" /></p>';
  83. }
  84.  
  85. $out .= '</form>';
  86. echo $out;
  87. return true;
  88.  
  89. }
  90.  
  91. /*
  92. * Creates an html input tag
  93. *
  94. * @param string $name
  95. * @param array $options
  96. */
  97. public function input($name, $options = array()) {
  98.  
  99. if (array_key_exists($name, $this->errors)) {
  100. $out = '<p class="error">';
  101. } else {
  102. $out = '<p>';
  103. }
  104.  
  105. $type = 'text';
  106.  
  107. if (!empty($options)) {
  108. if (array_key_exists('type', $options)) { $type = $options['type']; }
  109. $id = array_key_exists('id', $options) ? ' id ="' . $options['id'] . '"' : ' id="' . $name . '"';
  110. if (array_key_exists('label', $options)) {
  111. $out .= '<label for="' . $name . '">' . $options['label'] . '</label>';
  112. }
  113. }
  114.  
  115. $out .= '<input type="' . $type . '" name="' . $name . '"' . $id;
  116. if (array_key_exists($name, $_POST)) {
  117. $out .= ' value="' . $_POST[$name] . '" ';
  118. }
  119. $out .= ' />';
  120. if (array_key_exists($name, $this->errors)) {
  121. $out .= '<span class="note">' . $this->errors[$name] . '</span>';
  122. }
  123. $out .= '</p>';
  124. echo $out;
  125. return true;
  126.  
  127. }
  128.  
  129. /*
  130. * Creates a html textarea tag
  131. *
  132. * @param string $name
  133. * @param array $options
  134. */
  135. public function textarea($name, $options = array()) {
  136.  
  137. if (array_key_exists($name, $this->errors)) {
  138. $out = '<p class="error">';
  139. } else {
  140. $out = '<p>';
  141. }
  142.  
  143. if (!empty($options)) {
  144. $id = array_key_exists('id', $options) ? ' id ="' . $options['id'] . '"' : ' id="' . $name . '"';
  145. if (array_key_exists('label', $options)) {
  146. $out .= '<label for="' . $name . '">' . $options['label'] . '</label>';
  147. }
  148. }
  149.  
  150. $out .= '<textarea cols="50" rows="10" name="' . $name . '"' . $id . '>';
  151. if (array_key_exists($name, $_POST)) {
  152. $out .= $_POST[$name];
  153. }
  154. $out .= '</textarea>';
  155. if (array_key_exists($name, $this->errors)) {
  156. $out .= '<span class="note">' . $this->errors[$name] . '</span>';
  157. }
  158. $out .= '</p>';
  159. echo $out;
  160. return true;
  161.  
  162. }
  163.  
  164. /*
  165. * Creates a html select tag
  166. *
  167. * @param string $name The name of the field
  168. * @param array $values An array containing the options of the select tag. Every element of the array needs to have two elements value and label
  169. * @param array $options An array with aditional options for the select tag
  170. */
  171. public function select($name, $values, $options = array()) {
  172.  
  173. if (array_key_exists($name, $this->errors)) {
  174. $out = '<p class="error">';
  175. } else {
  176. $out = '<p>';
  177. }
  178.  
  179. if (!empty($options)) {
  180. $id = array_key_exists('id', $options) ? ' id ="' . $options['id'] . '"' : ' id="' . $name . '"';
  181. if (array_key_exists('label', $options)) {
  182. $out .= '<label for="' . $name . '">' . $options['label'] . '</label>';
  183. }
  184. }
  185. $out .= '<select name="' . $name . '"' . $id . '>';
  186. foreach ($values as $option) {
  187. $out .= '<option value="' . $option['value'];
  188. if ($_POST[$name] == $option['value']) {
  189. $out .= '" selected="selected">';
  190. } else {
  191. $out .= '">';
  192. }
  193. $out .= $option['label'] . '</option>';
  194. }
  195.  
  196. $out .= '</select></p>';
  197. echo $out;
  198. return true;
  199.  
  200. }
  201.  
  202. // Validation Rules
  203.  
  204. /*
  205. * Validates if the field is not empty
  206. *
  207. * @param string $field
  208. * @param string $message
  209. */
  210. private function notEmpty($field = null, $message) {
  211.  
  212. if (!empty($field)) {
  213. if (empty($_POST[$field])) {
  214. $this->errorCount++;
  215. $this->errors[$field] = $message;
  216. }
  217. }
  218.  
  219. }
  220.  
  221. /*
  222. * Validates if the field contains only numbers
  223. *
  224. * @param string $field
  225. * @param string $message
  226. */
  227. private function isNumeric($field = null, $message) {
  228.  
  229. if (!empty($field)) {
  230. if (!ctype_digit($_POST[$field])) {
  231. $this->errorCount++;
  232. $this->errors[$field] = $message;
  233. }
  234. }
  235.  
  236. }
  237.  
  238. /*
  239. * Validates if the field contain only alphabetic characters
  240. *
  241. * @param string $field
  242. * @param string $message
  243. */
  244. private function isAlphabetic($field = null, $message) {
  245.  
  246. if (!empty($_POST[$field])) {
  247. if (!ctype_alpha($field)) {
  248. $this->errorCount++;
  249. $this->errors[$field] = $message;
  250. }
  251. }
  252.  
  253. }
  254.  
  255. /*
  256. * Validates if the field contain only alphanumeric characters
  257. *
  258. * @param string $field
  259. * @param string $message
  260. */
  261. private function isAlphaNumeric($field = null, $message) {
  262.  
  263. if (!empty($_POST[$field])) {
  264. if (!ctype_alnum($field)) {
  265. $this->errorCount++;
  266. $this->errors[$field] = $message;
  267. }
  268. }
  269.  
  270. }
  271.  
  272. /*
  273. * Validates if the field is a valid email addresss
  274. *
  275. * @param string $field
  276. * @param string $message
  277. */
  278. private function validEmail($field = null, $message) {
  279.  
  280. if (!empty($field)) {
  281. if (!filter_var($_POST[$field], FILTER_VALIDATE_EMAIL)) {
  282. $this->errorCount++;
  283. $this->errors[$field] = $message;
  284. }
  285. }
  286.  
  287. }
  288.  
  289. /*
  290. * Validates if the field is a valid URL
  291. *
  292. * @param string $field
  293. * @param string $message
  294. */
  295. private function validURL($field = null, $message) {
  296.  
  297. if (!empty($field)) {
  298. if (!filter_var($_POST[$field], FILTER_VALIDATE_URL)) {
  299. $this->errorCount++;
  300. $this->errors[$field] = $message;
  301. }
  302. }
  303.  
  304. }
  305.  
  306. /*
  307. * Validates if the uploaded file is a valid type file
  308. *
  309. * @param string $field
  310. * @param string $message
  311. * @param array $options
  312. */
  313. private function validType($field = null, $message, $options = array()) {
  314.  
  315. $type = $_FILES[$field]['type'];
  316.  
  317. if (!empty($field)) {
  318. if (!in_array($type, $options)) {
  319. $this->errorCount++;
  320. $this->errors[$field] = $message;
  321. }
  322. }
  323.  
  324. }
  325.  
  326. /*
  327. * Executes all the validation rules
  328. */
  329. private function validate() {
  330.  
  331. if (!empty($this->rules)) {
  332. foreach ($this->rules as $field => $rule) {
  333. if (!empty($rule['rule'])) {
  334. if (!empty($rule['options'])) {
  335. $this->{$rule['rule']}($field, $rule['message'], $rule['options']);
  336. } else {
  337. $this->{$rule['rule']}($field, $rule['message']);
  338. }
  339. }
  340.  
  341. }
  342. }
  343.  
  344. }
  345.  
  346. /*
  347. * Returns true if all the fields pass validation
  348. *
  349. * @return true|false
  350. */
  351. public function passValidation() {
  352.  
  353. $this->validate();
  354.  
  355. if ($this->errorCount == 0) {
  356. return true;
  357. }
  358.  
  359. return false;
  360.  
  361. }
  362.  
  363. }
  364.  
  365. ?>

Report this snippet  

You need to login to post a comment.