Part on Validator Class finished and Part 2 finished

This commit is contained in:
Patryk Hegenberg 2022-12-20 21:23:29 +01:00
parent ef90228439
commit 20aee0812f
2 changed files with 17 additions and 5 deletions

14
Validator.php Normal file
View file

@ -0,0 +1,14 @@
<?php
class Validator
{
public static function string($value, $min = 1, $max = INF)
{
$value = trim($value);
return strlen($value) >= $min && strlen($value) <= $max;
}
public static function email($value)
{
return filter_var($value, FILTER_VALIDATE_EMAIL);
}
}

View file

@ -1,4 +1,5 @@
<?php <?php
require('Validator.php');
$heading = 'Create Note'; $heading = 'Create Note';
$username = 'appUser'; $username = 'appUser';
$password = 'password'; $password = 'password';
@ -8,11 +9,8 @@ $db = new Database($config['database'], $username, $password);
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$errors = []; $errors = [];
if (strlen($_POST['body'] === 0)) { if (! Validator::string($_POST['body'], 1, 1000)) {
$errors['body'] = 'A body is required.'; $errors['body'] = 'A body of no more than 1000 characters is required.';
}
if (strlen($_POST['body'] > 1000)) {
$errors['body'] = 'The body cann not be more than 1000 characters.';
} }
if (empty($errors)) { if (empty($errors)) {