Part on Validator Class finished and Part 2 finished
This commit is contained in:
parent
ef90228439
commit
20aee0812f
2 changed files with 17 additions and 5 deletions
14
Validator.php
Normal file
14
Validator.php
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
require('Validator.php');
|
||||
$heading = 'Create Note';
|
||||
$username = 'appUser';
|
||||
$password = 'password';
|
||||
|
|
@ -8,11 +9,8 @@ $db = new Database($config['database'], $username, $password);
|
|||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$errors = [];
|
||||
|
||||
if (strlen($_POST['body'] === 0)) {
|
||||
$errors['body'] = 'A body is required.';
|
||||
}
|
||||
if (strlen($_POST['body'] > 1000)) {
|
||||
$errors['body'] = 'The body cann not be more than 1000 characters.';
|
||||
if (! Validator::string($_POST['body'], 1, 1000)) {
|
||||
$errors['body'] = 'A body of no more than 1000 characters is required.';
|
||||
}
|
||||
if (empty($errors)) {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue