Skip to content

Instantly share code, notes, and snippets.

@peledies
Last active February 13, 2019 17:43
Show Gist options
  • Save peledies/e4cae24ef0cd7d19b89024940c90866d to your computer and use it in GitHub Desktop.
Save peledies/e4cae24ef0cd7d19b89024940c90866d to your computer and use it in GitHub Desktop.
<?php
/**
* Form Validation class
*
* @package default
* @author Deac Karns
**/
class FormValidator
{
protected $validation = true;
protected $required = [];
protected $post = null;
protected $error_bag = [
"code"=>null
, "error"=>""
, "errors"=>[]
];
function __construct($check, $die = true)
{
$this->setRequired($check);
$this->getInput();
$this->getFiles();
$this->checkRequired();
$this->generateResponse($die);
}
public function log(){
error_log(print_r($this,true));
}
private function getFiles(){
foreach ($_FILES as $key => $file) {
$this->post['files'][$key] = $file;
}
}
private function getInput(){
$post = file_get_contents('php://input');
$result = [];
mb_parse_str($post, $result);
if(empty($result)){
$result = $_POST;
}
$this->post = $result;
return $result;
}
private function checkRequired(){
if(count($this->required)){
foreach ($this->required as $key) {
switch ($key['test']) {
case 'empty':
if(empty($this->post[$key['key']])){
$this->error_bag['error'] = "Missing Required Parameters";
$this->error_bag['code'] = 400;
$this->validation = false;
$this->error_bag['errors'][$key['key']] = $key['key'].' can not be empty';
}
break;
case 'count':
if(count($this->post[$key['key']]) < 1){
$this->error_bag['error'] = "Missing Required Parameters";
$this->error_bag['code'] = 400;
$this->validation = false;
$this->error_bag['errors'][$key['key']] = $key['key'].' must have at least one record';
}
break;
case 'email':
if(!filter_var($this->post[$key['key']], FILTER_VALIDATE_EMAIL)){
$this->error_bag['error'] = "Missing Required Parameters";
$this->error_bag['code'] = 400;
$this->validation = false;
$this->error_bag['errors'][$key['key']] = '('.$this->post[$key['key']].') must be a valid email address';
}
break;
case 'file':
if(!array_key_exists($key['key'], $this->post['files'])){
$subtext = (isset($key['sub']))? " of type {$key['sub']}" : "";
$this->error_bag['error'] = "Missing Required Parameters";
$this->error_bag['code'] = 400;
$this->validation = false;
$this->error_bag['errors'][$key['key']] = "Must be a file$subtext";
}
if(array_key_exists($key['key'], $this->post['files']) && isset($key['sub'])){
$type = $this->post['files'][$key['key']]['type'];
if(strpos($type, $key['sub']) === FALSE){
$this->error_bag['error'] = "Missing Required Parameters";
$this->error_bag['code'] = 400;
$this->validation = false;
$this->error_bag['errors'][$key['key']] = "Must be a file of type {$key['sub']}";
}
}
break;
}
}
}
}
private function setRequired($required){
$check = [];
foreach ($required as $requirement) {
$sub = explode(':', $requirement);
$org = explode('|', $sub[0]);
$test['test'] = (count($org) == 1)? 'empty' : $org[1];
$test['key'] = $org[0];
if(isset($sub[1])){
$test['sub'] = $sub[1];
}
$check[] = $test;
}
$this->required = $check;
}
private function generateResponse($die){
if(!$this->validation && $die){
http_response_code(400);
header('Content-Type: application/json');
$this->error_bag['post'] = $this->post;
echo json_encode($this->error_bag);
die();
}else{
return $this;
}
}
}
// Example useage
/*
require('FormValidator.php');
$emailForm = new FormValidator([
'first_name'
, 'last_name|empty'
, 'email|email'
, 'logo|file:image/jpeg'
]);
*/
$emailForm->log();
@jesders
Copy link

jesders commented Feb 13, 2019

Deac,

I would suggest commenting out line 149 and on line 132 it is causing a parseerror when trying to send back the 200 response. With further inspection it wasn't being passed as a json object. echo json_encode($this); fixed it.

Also, on line 81 it should be $this->error_bag['errors'][$key['key']] = $key['key'].' must be a valid email address';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment