Skip to content

Instantly share code, notes, and snippets.

@thepsion5
Created June 20, 2014 15:59
Show Gist options
  • Save thepsion5/c8c1bae57c3563938db4 to your computer and use it in GitHub Desktop.
Save thepsion5/c8c1bae57c3563938db4 to your computer and use it in GitHub Desktop.
Simple contact form example
<?php
class ContactFormHandler
{
public function __construct(ContactFormTemplate $template, ContactFormValidator $validator, ContactFormMailer $mailer)
{
$this->template = $template;
$this->validator = $validator;
$this->mailer = $mailer;
}
public function display()
{
return $this->template->render();
}
public function process(array $input)
{
$this->template->setOldInput($input);
if(!$this->validator->validate($input)) {
$errors = $this->validator->getErrors();
$this->template->setErrors($errors);
} else {
$sent = $this->mailer->sendContactEmail($input['email'], $input['name'], $input['subject'], $input['message']);
if(!$sent) {
$this->template->setErrors(['' => 'Unable to send your contact email. Please try again.']);
}
}
return $this->template->render();
}
}
<?php
class ContactFormMailer()
{
protected $receiver = '[email protected]';
public function sendContactEmail($fromEmail, $fromName, $subject, $message)
{
$fromName = strip_tags($fromName);
$fromEmail = strip_tags($fromEmail);
$subject = htmlspecialchars($subject);
$message = htmlspecialchars($message);
$headers = "From: $fromEmail";
return mail($this->receiver, $subject, $message, $headers);
}
}
}
<?php
class ContactFormTemplate
{
protected $oldInput = [];
protected $errors = []
public function __construct(array $errors = [], array $oldInput = [])
{
$this->setErrors($errors);
$this->setOldInput($oldInput);
}
public function setOldInput(array $oldInput)
{
$this->oldInput = $oldInput;
return $this;
}
public function setErrors(array $errors)
{
$this->errors = $errors;
return $this;
}
public function render()
{
//I'm too lazy to write this part
}
}
<?php
class ContactFormValidator()
{
protected $errors = [];
protected $input = [];
protected $ruleset = [
'email' => [true, 4, FILTER_SANITIZE_EMAIL],
'name' => [true, 2, null],
'subject' => [true, 2, null],
'body' => [true, 10, null]
]
public function validate(array $input)
{
$this->input = $input;
$this->errors = [];
foreach($this->ruleset as $field => $rules) {
list($required, $minLength, $filterType) = $rules;
$this->validateField($field, $required, $minLength, $filterType);
}
return $this->isValid();
}
public function isValid()
{
return is_empty($this->errors);
}
protected function getInput($field)
{
return ( $this->hasInput($field) ) ? $this->input['email'] : null;
}
protected function hasInput($field) {
return isset($this->input[$field]);
}
protected function addError($field, $message)
{
if(!isset($this->errors[$field])) {
$this->errors[$field] = [];
}
$this->errors[$field][] = $message;
}
protected function validateField($field, $required = true, $minLength = 2, $filterType = null)
{
$value = $this->getInput($field);
$valid = true;
$error = null;
if($required && $value === null) {
$error = $field, 'This field is required.';
} elseif(strlen($value) < $minLength) {
$error = "This field must be at least [$minLength] characters long.";
} elseif($filterType !== null && !filter_var($field, $filterType)) {
$error = "This field must be formatted correctly.";
}
if($error) {
$this->addError($field, $error);
$valid = false;
}
return $valid;
}
}
<?php
$handler = new ContactFormHandler(new ContactFormTemplate, new ContactFormValidator, new ContactFormMailer);
$process = ($_SERVER['REQUEST_METHOD'] == 'POST');
echo ($process) ? $handler->display() : $handler->process($_POST);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment