1. <?php
  2. /**
  3. * Form Field Watcher and Validator
  4. *
  5. * Monitors form fields for changes and performs validation.
  6. * Logs errors to a file.
  7. */
  8. class FormWatcher {
  9. private $form_data = [];
  10. private $validation_rules = [];
  11. private $log_file = 'form_validation.log';
  12. public function __construct(array $validation_rules) {
  13. $this->validation_rules = $validation_rules;
  14. }
  15. /**
  16. * Registers a new form data field.
  17. * @param string $name
  18. * @param string $value
  19. */
  20. public function registerField(string $name, string $value) {
  21. $this->form_data[$name] = $value;
  22. }
  23. /**
  24. * Validates the form data.
  25. * @return array Array of errors. Empty array if no errors.
  26. */
  27. public function validate() {
  28. $errors = [];
  29. foreach ($this->validation_rules as $field => $rule) {
  30. $value = $this->form_data[$field] ?? ''; // Get value, default to empty string if not present
  31. if ($rule['type'] === 'required' && empty($value)) {
  32. $errors[] = $field . ': This field is required.';
  33. } elseif ($rule['type'] === 'email' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
  34. $errors[] = $field . ': Invalid email address.';
  35. } elseif ($rule['type'] === 'min_length' && strlen($value) < $rule['length']) {
  36. $errors[] = $field . ': Must be at least ' . $rule['length'] . ' characters.';
  37. }
  38. // Add more validation types here as needed.
  39. }
  40. return $errors;
  41. }
  42. /**
  43. * Logs validation errors to a file.
  44. * @param array $errors
  45. */
  46. private function logErrors(array $errors) {
  47. $log_message = date('Y-m-d H:i:s') . ' - Validation Errors:\n';
  48. foreach ($errors as $error) {
  49. $log_message .= $error . "\n";
  50. }
  51. file_put_contents($this->log_file, $log_message, FILE_APPEND);
  52. }
  53. /**
  54. * Process form submission.
  55. * @param array $post_data
  56. */
  57. public function processSubmission(array $post_data) {
  58. // Register form fields from the submitted data
  59. foreach ($post_data as $key => $value) {
  60. $this->registerField($key, $value);
  61. }
  62. // Validate the form data
  63. $errors = $this->validate();
  64. // Log any errors
  65. if (!empty($errors)) {
  66. $this->logErrors($errors);
  67. } else {
  68. // Form is valid, process the data
  69. // ... your form processing logic here ...
  70. echo "Form submitted successfully!\n";
  71. }
  72. }
  73. }
  74. // Example Usage:
  75. // Define validation rules
  76. $validation_rules = [
  77. 'name' => ['type' => 'required'],
  78. 'email' => ['type' => 'email'],
  79. 'message' => ['type' => 'min_length', 'length' => 10],
  80. ];
  81. // Create a FormWatcher instance
  82. $form_watcher = new FormWatcher($validation_rules);
  83. // Simulate form submission
  84. $form_data = $_POST; // Simulate form data from $_POST
  85. // Process the form submission
  86. $form_watcher->processSubmission($form_data);
  87. ?>

Add your comment