♻️ Rework how the validation are done
This commit is contained in:
parent
7a759bdee6
commit
de28da8c8c
5 changed files with 150 additions and 5 deletions
|
@ -5,6 +5,8 @@ $controlrow = mysqli_fetch_array($controlquery);
|
|||
|
||||
$router = new Router();
|
||||
$renderer = new Renderer();
|
||||
$messages = new Messages();
|
||||
$postData = new PostData();
|
||||
|
||||
// Application des différents namespace utilisés par le systeme
|
||||
$router->registerNamespace('admin', 2, false);
|
||||
|
|
70
kernel/helpers/validators.php
Normal file
70
kernel/helpers/validators.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
class Validator {
|
||||
private static function getValue($array, $keys) {
|
||||
$key = $keys[0];
|
||||
return $array[$key];
|
||||
}
|
||||
|
||||
private static function getValues($array, $keys) {
|
||||
$list = [];
|
||||
foreach ($keys as $key) {
|
||||
$list[] = $array[$key];
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function required($name, $array, $keys) {
|
||||
$value = Validator::getValue($array, $keys);
|
||||
if ($value == "" || $value == null || trim($value) == "") {
|
||||
return "{$name} field is required";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function mail($name, $array, $keys) {
|
||||
$value = Validator::getValue($array, $keys);
|
||||
if (!is_email($value)) {
|
||||
return "{$name} field must be valid";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function equals($name, $array, $keys) {
|
||||
$values = Validator::getValues($array, $keys);
|
||||
$match = true;
|
||||
$firstValue = $values[0];
|
||||
|
||||
foreach ($values as $value) {
|
||||
if ($value !== $firstValue) {
|
||||
$match = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$match) {
|
||||
return "{$name} fields must match";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function alphanumeric($name, $array, $keys) {
|
||||
$value = Validator::getValue($array, $keys);
|
||||
if (preg_match("/[^A-z0-9_\-]/", $value)==1) { // Thanks to "Carlos Pires" from php.net!
|
||||
return "{$name} fields must be alphanumeric";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function unique($name, $array, $keys, $database) {
|
||||
$value = Validator::getValue($array, $keys);
|
||||
$table = $database["table"];
|
||||
$dbField = $database["field"];
|
||||
|
||||
$query = doquery("SELECT ${dbField} FROM {{table}} WHERE ${dbField}='$value' LIMIT 1", $table);
|
||||
if (mysqli_num_rows($query) > 0) {
|
||||
return "{$name} already taken, it must be unique";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -29,11 +29,18 @@ if (DEBUG_MODE) {
|
|||
error_reporting(E_ERROR);
|
||||
}
|
||||
|
||||
include(PATH_KERNEL . 'lib.php');
|
||||
include(PATH_KERNEL . 'cookies.php');
|
||||
include(PATH_KERNEL . 'renderer.php');
|
||||
include(PATH_KERNEL . 'namespace.php');
|
||||
include(PATH_KERNEL . 'router.php');
|
||||
// Defined folders
|
||||
define('PATH_HELPERS', PATH_KERNEL . 'helpers' . DS);
|
||||
|
||||
require(PATH_HELPERS . 'validators.php');
|
||||
|
||||
require(PATH_KERNEL . 'lib.php');
|
||||
require(PATH_KERNEL . 'cookies.php');
|
||||
require(PATH_KERNEL . 'renderer.php');
|
||||
require(PATH_KERNEL . 'namespace.php');
|
||||
require(PATH_KERNEL . 'router.php');
|
||||
require(PATH_KERNEL . 'messages.php');
|
||||
require(PATH_KERNEL . 'postdata.php');
|
||||
|
||||
|
||||
$starttime = getmicrotime();
|
||||
|
|
59
kernel/postdata.php
Normal file
59
kernel/postdata.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
class PostData {
|
||||
public $errors = [];
|
||||
public $datas = [];
|
||||
|
||||
private $fields = [];
|
||||
private $nonCleanDatas;
|
||||
private $validators;
|
||||
|
||||
public function __construct() {
|
||||
$this->nonCleanDatas = $_POST;
|
||||
$this->validators = new Validator();
|
||||
}
|
||||
|
||||
public function addField($name, $rules, $fieldName, $database = []) {
|
||||
$this->addFields($name, $rules, [$fieldName], $database);
|
||||
}
|
||||
|
||||
public function addFields($name, $rules, $fieldNames, $database = []) {
|
||||
$this->fields[] = ["name"=>$name, "rules"=>$rules, "fieldNames"=>$fieldNames, "database"=>$database];
|
||||
}
|
||||
|
||||
public function validate() {
|
||||
foreach ($this->fields as $field) {
|
||||
$this->validateField($field);
|
||||
$this->getDataFromField($field);
|
||||
}
|
||||
return (count($this->errors) == 0);
|
||||
}
|
||||
|
||||
public function getField($field) {
|
||||
return $this->datas[$field];
|
||||
}
|
||||
|
||||
private function validateField($field) {
|
||||
$rules = $field["rules"];
|
||||
if (count($rules) > 0) {
|
||||
foreach ($rules as $rule) {
|
||||
$this->applyValidation($rule, $field["name"], $this->nonCleanDatas, $field["fieldNames"], $field["database"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getDataFromField($field) {
|
||||
$fieldNames = $field["fieldNames"];
|
||||
foreach ($fieldNames as $name) {
|
||||
$this->datas[$name] = $this->nonCleanDatas[$name];
|
||||
}
|
||||
}
|
||||
|
||||
private function applyValidation($rule, $name, $array, $keys, $database) {
|
||||
$error = call_user_func(array($this->validators, $rule), $name, $this->nonCleanDatas, $keys, $database);
|
||||
if ($error != null) {
|
||||
echo($error);
|
||||
$this->errors[] = $error;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -57,4 +57,11 @@ class Renderer {
|
|||
$this->prepare("content", $content);
|
||||
$this->render("simplepage.twig");
|
||||
}
|
||||
|
||||
public function addPostFields($fields) {
|
||||
global $postData;
|
||||
foreach ($fields as $field) {
|
||||
$this->prepare("field_{$field}", $postData->getField($field));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue