/ Published in: PHP
Please, feel free to suggest improvements
Expand |
Embed | Plain Text
<?php /* * phpForm class for create and validate forms * * @package phpForm * @author Armando Monroy aka Ratzo * @version 0.5 * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ class Form { /* * Stores number of fields that failed validation * @var int */ protected $errorCount = 0; /* * Array containing all the field that failed validation and their error messages. * @var array */ /* * Array containing all the validation rules that should be applied. * @var array */ /* * Set the validation rules * @param array $rules */ public function setRules($rules) { $this->rules = $rules; } } /* * Creates an html form tag * * @param string $action URL of the action * @param array $options Optional options like type * * @return string */ $action = $_SERVER['PHP_SELF']; } } $out = '<form method="post" action="' . $action . '"'; $out .= ' enctype="multipart/form-data"'; } $out .= $id . '>'; echo $out; return true; } /* * Creates an html closing form tag * * @param string $message Optional parameter wich display a submit button if present */ $out = null; $out = '<p class="submit"><input type="submit" value="' . $message . '" /></p>'; } $out .= '</form>'; echo $out; return true; } /* * Creates an html input tag * * @param string $name * @param array $options */ $out = '<p class="error">'; } else { $out = '<p>'; } $type = 'text'; $out .= '<label for="' . $name . '">' . $options['label'] . '</label>'; } } $out .= '<input type="' . $type . '" name="' . $name . '"' . $id; $out .= ' value="' . $_POST[$name] . '" '; } $out .= ' />'; $out .= '<span class="note">' . $this->errors[$name] . '</span>'; } $out .= '</p>'; echo $out; return true; } /* * Creates a html textarea tag * * @param string $name * @param array $options */ $out = '<p class="error">'; } else { $out = '<p>'; } $out .= '<label for="' . $name . '">' . $options['label'] . '</label>'; } } $out .= '<textarea cols="50" rows="10" name="' . $name . '"' . $id . '>'; $out .= $_POST[$name]; } $out .= '</textarea>'; $out .= '<span class="note">' . $this->errors[$name] . '</span>'; } $out .= '</p>'; echo $out; return true; } /* * Creates a html select tag * * @param string $name The name of the field * @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 * @param array $options An array with aditional options for the select tag */ $out = '<p class="error">'; } else { $out = '<p>'; } $out .= '<label for="' . $name . '">' . $options['label'] . '</label>'; } } $out .= '<select name="' . $name . '"' . $id . '>'; foreach ($values as $option) { $out .= '<option value="' . $option['value']; if ($_POST[$name] == $option['value']) { $out .= '" selected="selected">'; } else { $out .= '">'; } $out .= $option['label'] . '</option>'; } $out .= '</select></p>'; echo $out; return true; } // Validation Rules /* * Validates if the field is not empty * * @param string $field * @param string $message */ private function notEmpty($field = null, $message) { $this->errorCount++; $this->errors[$field] = $message; } } } /* * Validates if the field contains only numbers * * @param string $field * @param string $message */ private function isNumeric($field = null, $message) { $this->errorCount++; $this->errors[$field] = $message; } } } /* * Validates if the field contain only alphabetic characters * * @param string $field * @param string $message */ private function isAlphabetic($field = null, $message) { $this->errorCount++; $this->errors[$field] = $message; } } } /* * Validates if the field contain only alphanumeric characters * * @param string $field * @param string $message */ private function isAlphaNumeric($field = null, $message) { $this->errorCount++; $this->errors[$field] = $message; } } } /* * Validates if the field is a valid email addresss * * @param string $field * @param string $message */ private function validEmail($field = null, $message) { if (!filter_var($_POST[$field], FILTER_VALIDATE_EMAIL)) { $this->errorCount++; $this->errors[$field] = $message; } } } /* * Validates if the field is a valid URL * * @param string $field * @param string $message */ private function validURL($field = null, $message) { if (!filter_var($_POST[$field], FILTER_VALIDATE_URL)) { $this->errorCount++; $this->errors[$field] = $message; } } } /* * Validates if the uploaded file is a valid type file * * @param string $field * @param string $message * @param array $options */ $type = $_FILES[$field]['type']; $this->errorCount++; $this->errors[$field] = $message; } } } /* * Executes all the validation rules */ private function validate() { foreach ($this->rules as $field => $rule) { $this->{$rule['rule']}($field, $rule['message'], $rule['options']); } else { $this->{$rule['rule']}($field, $rule['message']); } } } } } /* * Returns true if all the fields pass validation * * @return true|false */ public function passValidation() { $this->validate(); if ($this->errorCount == 0) { return true; } return false; } } ?>
You need to login to post a comment.
